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";


In reply to Date Handling in Perl by joeymac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.