Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Date Handling in Perl

by joeymac (Acolyte)
on Jul 11, 2012 at 14:12 UTC ( #981123=perlquestion: print w/ replies, xml ) Need Help??
joeymac has asked for the wisdom of the Perl Monks concerning the following question:

I am seeking advice on some of the best ways to handle dates using Perl. The situation I have is this: I am FTP pulling data from a remote server that recently restructured their directories to YYYY/MM/DD (i.e. 2012/07/11). I am currently using "back-tick" date to read in current time stats and change to directory accordingly. The "$currentHour < 10" bit must be included because of data latency. For example, the last file into "/2012/07/09/" on the remote server is put at approximately 0830 UTC on July 10. Also how is the best way to handle end-of-month/beginning-of-new-month changes, such as when $currentDay - 1 = 0 (the first of the month) because sometimes you then want it to = 30 and sometimes = 31 (or 28/29 for March 1st). I would prefer to use this code as a basis rather than needing to install ancillary Perl modules since I don't have sys admin permissions on the local machine I am using. Any advice or input would be GREATLY appreciated. Thanks wondrous monks!

#!/bin/perl use warnings; use strict; my $currentYear = `date +%Y`; chomp($currentYear); my $currentMonth = `date +%m`; chomp($currentMonth); my $currentDay = `date +%d`; chomp($currentDay); my $currentHour = `date +%H`; chomp ($currentHour); my $tempDay; if ($currentHour < 10 ) { $tempDay = $currentDay - 1; print "temp: $tempDay\n"; if ($tempDay =~ m/(\d\d)$/ ) { $tempDay = $tempDay; } elsif ($tempDay =~ m/(\d)$/) { $tempDay = '0'.$tempDay; } else { print "Unable to match day\n"; } } my $directoryDay = $tempDay; my $dir = 'something/something/'.$currentYear.'/'.$currentMonth.'/'.$d +irectoryDay; print "\n"; print "month: $currentMonth\n"; print "day : $currentDay\n"; print "dir : $dir\n\n";

Comment on Date Handling in Perl
Download Code
Re: Date Handling in Perl
by aitap (Deacon) on Jul 11, 2012 at 14:35 UTC
    You may want to try localtime and sprintf:
    my ($hour) = (localtime)[2]; $hour > 10 ? $_=0 : $_=60*60*24; # seconds in the day my ($mday,$mon,$year) = (localtime(time-$_))[3..5]; my $dir = sprintf('something/something/%04d/%02d/%02d',$year+1900,$mon +,$mday);
    Sorry if my advice was wrong.

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

      $hour > 10 ? $_=0 : $_=60*60*24; # seconds in the day

      It seems to be some pattern matching that I am not completely familiar with. Do you mind elaborating a little? 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 yesterday (/07/10).

        The code is better written as:

        $_ = $hour > 10 ? 0 : 60*60*24; # seconds in the day my ($mday,$mon,$year) = (localtime(time-$_))[3..5];

        Using $_ here is just laziness of not declaring a new variable. The first line returns 0 if hour <= 10, and 86400 otherwise. It is then used to modify the time() function to either get today's date (when 0) or yesterday's (when 86400).

        (I do wonder where that corruption of the ternary operator comes from. It makes no sense at all. Unless someone is teaching it as a "shorthand if"...)

        Oh, it looks like some mistake I've done, and I can't understand what's wrong (look at the Re: Date Handling in Perl - it's similar).

        I tried to use Conditional Operator to put the time (in seconds) to subtract in the $_ variable. It can be done the other way: my $subt = 0; $hour > 10 || $subt = 60*60*24; (in this case, you'll need to use $subt instead of $_)

        Sorry if my advice was wrong.
        joeymac: That line is using the ternary conditional operator. It is essentially shorthand for an if-then-else conditional. Take a look here for more info: Conditional Operator.
Re: Date Handling in Perl
by Utilitarian (Vicar) on Jul 11, 2012 at 14:42 UTC
    perldoc -f localtime - For all your basic time manipulation needs.
    perldoc Date::Manip - For more complex manipulations with end of month and edge cases solved for you.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Date Handling in Perl
by fishmonger (Pilgrim) on Jul 11, 2012 at 14:43 UTC
    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));

      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.

Re: Date Handling in Perl
by cheekuperl (Monk) on Jul 11, 2012 at 15:03 UTC
    If you are not using any of the Perl modules, you will be better off writing your own tailored to your requirements.
    how is the best way to handle end-of-month/beginning-of-new-month changes, such as when $currentDay - 1 = 0 (the first of the month) because sometimes you then want it to = 30 and sometimes = 31 (or 28/29 for March 1st).
    I'd done something similar in the past. I had written my own lastDayofMonth routine that also checked leap year. This is because we know in advance all possible lastDayOfMonth values
    $lastDay = lastDayOfMonth($month); #returns a number indicating last d +ay ... sub lastDayOfMonth { my $month=shift; <sanity check here> return 29 if($month==2 and isLeapYear($currYear)); <and so on> }

      Most of the other posts here are using Unix timestamps to do the date arithmetic, and it is a good idea for this particular case (especially when Perl comes with the functions needed for it). It is not a good idea to write the millionth instance of manual date arithmetic code in existence, when the vast majority of them are riddled with bugs. (See: recent outages involving 29 February)

      (I generally only trust well-known modules or an SQL database if I need to do date arithmetic)

Re: Date Handling in Perl
by runrig (Abbot) on Jul 11, 2012 at 17:15 UTC
    Yet another way:
    use Time::Piece; my $t = Time::Piece->new(); # Roll back 12 hours (to previous day) if < 10am $t -= 12*60*60 if $t->hour() < 10; print $t->ymd("/"),"\n";
Re: Date Handling in Perl
by hbm (Hermit) on Jul 11, 2012 at 17:19 UTC

    For your end-of-month problem, can you do this, or similar, with your system date command?

    $ date --date=yesterday +%d 10
Re: Date Handling in Perl
by joeymac (Acolyte) on Jul 11, 2012 at 18:54 UTC

    I really appreciate everyone's advice. I even learned something about ternary conditional operators along the way! How I have addressed the issue for now is related to the suggestion by hbm:

    my $yesterDay = `date -d "1 day ago" +%d`;

    I'm keeping an eye on the Perl script and I am hoping this method will work. Thanx again!

      Are you using '1 day ago' for month and year too?

        From what I've read:

        date -d "1 day ago"

        should be smart enough to know that on the first day of the month, 1 day ago was 30|31 (or 28|29) of LAST month. Also true for January 1 (and knowing it was last month AND last year). I tested like this:

        bash-3.2$ date +%j 193 bash-3.2$ date -d "193 day ago " Sat Dec 31 14:54:22 UTC 2011
        Should this hold true?

      The worst part of that method is that it is not portable. Only the GNU version of date (found on most Linuxes) supports that syntax. For example, on FreeBSD, the equivalent syntax is date -v-1d +%d. On other operating systems, date(1) might not even support such arithmetic.

      I highly recommend keeping things Perl when doing so is easy if not trivial (as it is in this case).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-07-29 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls