Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Date Handling in Perl

by fishmonger (Chaplain)
on Jul 11, 2012 at 14:43 UTC ( #981134=note: print w/replies, xml ) Need Help??

in reply to Date Handling in Perl

I'd use localtime and strftime.
use strict; use warnings; use POSIX qw(strftime); my $hr = (localtime)[2]; my $minus = $hr < 10 ? 36000 : 0; my $dir = strftime("/something/something/%Y/%m/%d", localtime(time - $ +minus));

Replies are listed 'Best First'.
Re^2: Date Handling in Perl
by joeymac (Acolyte) on Jul 11, 2012 at 15:43 UTC

    Thanks. I have been playing with your method, but I'm not completely sure what this bit is doing:

    $hr < 10 ? 36000 : 0

    It seems to be some pattern matching that I am not completely familiar with. I was trying to tweak it to see if I could make it print today and/or yesterday in the final position of the directory path before I add it to my operational code, but I can only ever get it to print today (/07/11).

      That is using the ternary operator to set the amount of time we want to subtract when creating the datestamp. You can read up on the ternary operator in 'perldoc perlop'.

      36000 is the number of seconds in 10 hours. Another monk used 60*60*24 which == 86400 and is the number of seconds in 24 hours.

      I was going to use the 86400 value, but in this case 36000 should be sufficient to roll the datestamp back to the previous day. If the hour is grater than 10, then there's no need to adjust the datestamp, so $minus is set to 0.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://981134]
and a kettle whistles...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-05-25 19:20 GMT
Find Nodes?
    Voting Booth?