Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

A date calculation

by crawfordr (Acolyte)
on Dec 18, 2007 at 21:30 UTC ( #657745=perlquestion: print w/replies, xml ) Need Help??

crawfordr has asked for the wisdom of the Perl Monks concerning the following question:

The following date calculation is being run from a unix script. It is calculating the incorrect date. We use the calculation to determine a date fro our billing system. The code has been calculating the date correctly until today when it was run to calculate a January 2008 date.
Here is the code:
billdate=`perl -MPOSIX -e 'print strftime( "%y%m", localtime( time + 1 +123200 ) )'`01

It is currently producing 071201 and it should be calculating 080101.

We need this relatively quickly. I would very much appreciate any help whatsoever. You guys are great.
Thanks again

Edit: g0n - code tags and formatting

Replies are listed 'Best First'.
Re: A date calculation
by FunkyMonk (Chancellor) on Dec 18, 2007 at 21:47 UTC
    1123200 = 13 * 24 * 60 * 60, ie 13 days from now, 31st December 2007. You use strftime to print the year and month: 0712 and tag an 01 on the end: 071201.

    It looks like your proglet is doing what you're telling it to do. Or, am I missing something?

      Hello, Thanks for the response. I have a question, how do we get 13 * 24 * 60 * 60 from 1123200. I was not sure how that was done. Thanks.
        13*24*60*60 is (days * hours/day * minutes/hour * seconds/min). So your script is taking the current date/time, adding 13 days to it, and then printing the two digit month and year of that date/time. Since 13 days from now is still December 2007, you're getting 0712 (instead of the 0801 you evidently expected).
        time is in seconds, so he is just converting 1123200 seconds into days
        11123200 / 60 sec / 60 min / 24 hours = 13 days.
        Or 13 days * 24 hours * 60 min * 60 sec = 1123200 seconds.
        13 days * 24 hours * 60 minutes * 60 seconds = 1123200 seconds

        Now I know I'm missing something:(

Re: A date calculation
by moritz (Cardinal) on Dec 18, 2007 at 21:56 UTC
    If you want quick answers, you could help us by proper formating your question, thus making it easier to answer.

    From your code it seems you're trying to add 13 days to the current date, and get the month and year of that date.

    I recommend Date::Simple for calculations like this, it is fairly stable and has a really simple interface.

Re: A date calculation
by aquarium (Curate) on Dec 19, 2007 at 01:45 UTC
    or the more traditional approach
    sleep 1123200; billdate= strftime( "%y%m", localtime( time ) ) . '01';
    just need to figure out how to make the first line of code run faster
    the hardest line to type correctly is: stty erase ^H
        i never i know....scary
        the hardest line to type correctly is: stty erase ^H
Re: A date calculation
by chrism01 (Friar) on Dec 19, 2007 at 01:15 UTC
    As per funkymonk's orig post, it's trying to calc the first of the (next) mth, by adding enough days to get to the end of the curr mth (calc in seconds). It's actually an off-by-one error (in this case off by 1 day).
    As mentioned, you should use a Date:: module to avoid fiddly details like different mth lengths and leap yrs.
Re: A date calculation
by poolpi (Hermit) on Dec 19, 2007 at 09:37 UTC
    use DateTime; my $dt = DateTime->now(time_zone => 'UTC'); $dt->add( days => 13); print substr( $dt->year, -2 ), $dt->month < 10 ? '0' : '', $dt->month;


    Update :
    perl -MDateTime -e 'print unpack( "x2A2xA2", DateTime->now(time_zone => 'UTC') ->add(days => 13) );'



Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://657745]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2021-10-25 02:15 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (89 votes). Check out past polls.