Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Date operations

by kidd (Curate)
on Aug 11, 2002 at 22:33 UTC ( #189344=perlquestion: print w/ replies, xml ) Need Help??
kidd has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks...

A couple of months ago I aked a question about how could I create expiration dates...
And that's were they suggested this solution:

use POSIX qw[strftime]; my $days = 30; my $newExpiration = strftime('%Y%m%d', localtime(time + 60 * 60 * 24 * + $days));

Now this solution has been very useful, and now I use it for everything...

But now I want to know if there is a way to make this same operation but with another date that it isnt the actual date...

Thanks

Comment on Date operations
Download Code
Re: Date operations
by grep (Monsignor) on Aug 11, 2002 at 22:49 UTC
    You should really think about using Date::Calc and/or Date::Manip. These might be overkill for this particular task but are lifesavers if you have to do anything else with dates.

    You could also look at Time::Local which does the opposite of localtime

    another (inelegant) solution would be to take the time() and add the seconds to get to the proper date from today

    localtime(time() + (60 * 60 * 24 * 4) + (60 * 60 * 24 * $days)) # add +4 days localtime(time() - (60 * 60 * 24 * 7) + (60 * 60 * 24 * $days)) # subt +ract 1 week


    grep
    Just me, the boy and these two monks, no questions asked.
      You should really think about using Date::Calc and/or Date::Manip. These might be overkill for this particular task but are lifesavers if you have to do anything else with dates.

      The reason I liked the current way Im making the operations is that I dont have to get or install any modules...wich are a pain in the a** to configure in my hosting service...

        pain in the a** to configure in my hosting service...

        One (including myself) might argue that recreating any portion of Date::Calc or Date::Manip is a pain in the a$$, since it is already correctly done for you.

        I think you will agree with me the first time you forget one of the rules for leapyears or your client says he/she wants 30 days to not including weekends ;-)

        You most likely have seen this already but, if it helps tachyon has a tutorial on this.



        grep
        Just me, the boy and these two monks, no questions asked.

        from the Perl CGI Programming FAQ:

        (if you don't) have access to install these modules with the other Perl library files, you can still use it by placing the module in a convenient place and adding the following to your script: BEGIN { unshift (@INC, "/your/dir/favorite/place"); }
Re: Date operations
by mojotoad (Monsignor) on Aug 11, 2002 at 23:48 UTC
    If it's just dates, rather than time of day, then anchor off of noon rather than midnight with your formulae. Your calculations will potentially be short by one day during spans around Daylight Savings Time transitions if you are adding "days as seconds", 60*60*24*$days -- the DST transition happens at midnight.

    Matt

    Update: As AM said, 2am. Another option is to do your calculations in gmtime and convert to localtime when your finished.

      The DST transition happens at 02:00
      But your calculations will potentially be off by one day at midnight
Re: Date operations
by greenFox (Vicar) on Aug 12, 2002 at 02:52 UTC
    The time() function you are using returns a time in Epoch seconds (for Unix systems the number of seconds since the first of January 1970). So to work with another date you only need first to convert it to Epoch seconds. Time::Local will do this for you-

    use Time::Local; use POSIX qw[strftime]; my $other_time = timelocal($sec,$min,$hours,$mday,$mon,$year); my $days = 30; my $otherExpiration = strftime('%Y%m%d', localtime($other_time + 60 * +60 * 24 * + $days));

    Don't forget that months are counted from 0-11. Depending on the accuracy required you can skip parsing the time and just call timelocal() with a fixed time (say midday) and the parsed date- this is OK for an emailed reminder but not so great for a battery replacement on a pacemaker :) If the date data you are working with has differing formats or accuracy is critical then your safest approach would be to use Date::Manip or Date::Calc.

    --
    Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://189344]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2014-04-21 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (496 votes), past polls