Forum Moderators: coopster & phranque

Message Too Old, No Replies

Need better way to add time.

         

NevadaSam

7:40 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



I use this sub routine to return dates and times. I am wondering if there is an easier way to do this. This gives me what I need but you can see how I seem to repeat a step. The results I need are:

$datestamp - example: 20060608153125
$pdate - example: 06-08-2006

I then repeat the code to add $day - example 14
to get:

$edate - example: 06-22-2006
$edatestamp - example: 20060222

#!/usr/bin/perl

# untitled4.pl

my $days = 14;

sub date_stamp {
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
$year = $year + 1900;
$mon = ($mon + 1);
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10){$hour = "0$hour";}
if ($min < 10){$min = "0$min";}
if ($sec < 10){$sec = "0$sec";}
$datestamp = $year.$mon.$mday.$hour.$min.$sec;
$pdate = ("$mon\-$mday\-$year");

my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time+86400*$days);
$year = $year + 1900;
$mon = ($mon + 1);
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10){$hour = "0$hour";}
if ($min < 10){$min = "0$min";}
if ($sec < 10){$sec = "0$sec";}

$edatestamp = $year.$mon.$mday;
$edate = ("$mon\-$mday\-$year");
return $datestamp, $pdate, $edatestamp, $edate;
} #end date_stamp

This code works but if you know a better way of doing it please show me.

coopster

11:00 pm on Jun 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, NevadaSam.

Have you considered using the POSIX [search.cpan.org] module? It includes quite a few nice functions to help you manipulate date and time. CPAN also has quite a few other Date modules [search.cpan.org] that may be of interest.

NevadaSam

12:00 am on Jun 9, 2006 (gmt 0)

10+ Year Member



Thank you, coopster.

I have only been doing Perl for a little over 2 months. I haven't heard of POSIX yet, but I have heard CPAN alot and I will look at the web site for date modules.

MichaelBluejay

5:52 am on Jun 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When manipulating dates, I find it's often easier to use Epoch seconds. The Epoch seconds are the number of seconds since the Perl epoch, something like January 1, 1900, or something like that. For example:

$now = time; # assigns the current value in epoch seconds to variable '$now'

Then to add four days to it, you just add four days worth of seconds:

$then = $now + 4*24*60*60;

Now to convert that into human-readable form...

($second, $minute, $hour, $day, $month, $year, $weekday, $dayOfYear, $IsDST) = localtime($then);

Note a few quirks about these values. First, the month range is 0-11, not 1-12, so you'll need to add 1 to the number of months. And the value for the year has 1900 subtracted from it. So the value for this year would be returned as 106, so you'd need to add 1900 to it.

Finally, if you have a date that you need to convert *to* epoch seconds, you do so like this:

use Time::Local;
$epoch = timelocal($sec, $min, $hours, $mday, $mon, $year);

You need to feed it Perl-time-style values. That is, feed it a $mon that's in the range 0-11, and a year that has 1900 subtracted from it.

Is all this awesome or what?

KevinADC

6:33 am on Jun 9, 2006 (gmt 0)

10+ Year Member




something like January 1, 1900

it's actually January 1, 1970 for most systems, but Macs go all the way back to 1904 for some reason.

veesah

4:38 am on Jun 12, 2006 (gmt 0)

10+ Year Member



POSIX function strftime is what you want.

It maps to C function strftime provided by the C library

use POSIX;
$pdate = strftime("%m-%d-%Y",localtime);
$datestamp = strftime("%Y%m%d%H%M%S",localtime);
... and so on