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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |