Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Adding to a date using Time::localtime

by Anonymous Monk
on Feb 21, 2007 at 19:08 UTC ( #601406=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I would like to add 14 days to my days of the month based on today's date. I am susing the code below. Any tricks to account for dates falling at the end of the month. Like when I execute this, I get 2/35/2007. Any ideas what approach I should take. Would epoch time be better? Thanks.
use Time::localtime; my $tm=localtime; my ($hour,$min,$sec,$day,$month,$year)=($tm->hour,$tm->min,$tm->sec,$t +m->mday, $tm->mon, $tm->year); my $input = sprintf("%d/%02d/%04d", $tm->mday, $tm->mon+1, $tm->year+1900); my @today = split /\//,$input; my $due_date = sprintf("%d/%02d/%04d", $tm->mon+1, $tm->mday+14, $tm->year+1900); my @due_date = split /\//,$due_date; $due_date = "$due_date[0]/$due_date[1]/$due_date[2]"; print $due_date;

Replies are listed 'Best First'.
Re: Adding to a date using Time::localtime
by philcrow (Priest) on Feb 21, 2007 at 19:18 UTC
    DateTime makes this fairly easy:
    use DateTime; my $dt = DateTime->now; $dt->add( days => 14 );
    Update: Changed to use now.


Re: Adding to a date using Time::localtime
by sweetblood (Parson) on Feb 21, 2007 at 19:23 UTC
    print scalar localtime (time + (86400 * 14));


      No, not all days have 86400 seconds. (Update: Specifically, in places with Daylight Saving Time, some days are longer and some are shorter. The parent's code will result in the calculated date being off by a day when executed during certain hours of the year. )

      If you were limited to core Perl, you'd could do something like the following:

      use POSIX qw( strftime ); use Time::Local qw( timegm_nocheck ); # Get date. my ($y, $m, $d) = (localtime())[5, 4, 3]; $m += 1; $y += 1900; # Add to date. $d += 14; # Canonize date. ($y, $m, $d) = (gmtime(timegm_nocheck(0,0,0, $d, $m-1, $y)))[5, 4, 3]; $m += 1; $y += 1900; print(strftime("%x", 0,0,0, $d, $m-1, $y-1900), "\n");

      Note: I like keeping $y, $m and $d as a human readable numbers. You could shorten the above by leaving $m 0-based and $y 1900-based.

      Note: I don't recommend this approach. Take a minute to install one of the date modules.

        not all days are the same length.

        Please explain!

        Are you talking about a Leap_second? If Wikipedia is to believed, there have been 23 leap seconds since 1972. Since the OP is only trying to find what date is 14 days into the future, sweetblood's method is only going to have a problem on one second of a day with a leap second. That is, 23 out of the last 1093273318 seconds, as I write this, or one in 47.5 million (y'know, approximately).

        I routinely say $ONE_DAY = 24 * 60 * 60. Maybe I'm sloppy, or I just haven't ever written an application where it matters. I suspect if I ever had a problem, I didn't notice or didn't care.

Re: Adding to a date using Time::localtime
by Rhandom (Curate) on Feb 23, 2007 at 16:08 UTC
    The idea to use gmtime/timegm is a good one. Another simple idea is to go from noon of the source day. There is no daylight offset that will ever give you trouble if you go from noon.
    perl -e 'use Time::Local qw(timelocal); print "".localtime(timelocal(0 +,0,12,23,1,2006)+86400*124)."\n"' # prints Tue Jun 27 13:00:00 2006

    Of course this all assumes that you aren't worried about the hours - but there are easy ways to handle those also. You just retain the hours,mins,secs from before the day conversion and replace the hours,mins,secs of the converted date.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://601406]
Approved by philcrow
Front-paged by andyford
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2017-01-19 07:59 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (169 votes). Check out past polls.