Gossamer Forum
Home : General : Perl Programming :

localtime's time-of-the-month

Quote Reply
localtime's time-of-the-month
This is bizarre...My full blown script gets an epoch time value for the current date/time and then subtracts a variable number of seconds from it and then touches a file with the old timestamp on it. I'm hitting a snag when the resulting date/time is in the previous month from the current time. That is, if I start with 08:00 on June 1, 2006 and subtract 9 hours worth of seconds, I end up with 23:00 on May 30th instead of 23:00 on May 31.

Here's some sample code that you walk-through and try to demonstrate the problem.

###
perl -e '
($seconds, $minutes, $hours, $day_of_month, $month, $year,
$wday, $yday, $isdst) = localtime(1151756508);
{ if ($seconds<10) { $seconds = join("",0,$seconds); } }
{ if ($minutes<10) { $minutes = join("",0,$minutes); } }
{ if ($hours<10) { $hours = join("",0,$hours); } }
{ if ($day_of_month<10) { $day_of_month = join("",0,$day_of_month); } }
{ if ($month<10) { $month = join("",0,$month); } }
$yr=$year - 100;
{ if ($yr<10) { $yr = join("",0,$yr); } }

$stamp = "$yr$month$day_of_month - $hours$minutes";
print "stamp = $stamp\n";
'
###

This returns a baseline date of: stamp = 060601 - 0821 because 1151756508 is the epoch time (# of seconds between 1/1/1970 and desired date) for 08:21am of June 1st of this year. Fair enough?

Now, this should mean that you can manipulate the resulting date/time stamp output by adding to or subtracting seconds from the baseline value. So if we want stamp = 060601 - 0021, we can subtract 8 hrs worth of seconds from 1151756508 . So, 1151756508- (8*60*60) = 1151727708, correct?

We test this with:
###
perl -e '
($seconds, $minutes, $hours, $day_of_month, $month, $year,
$wday, $yday, $isdst) = localtime(1151727708);
{ if ($seconds<10) { $seconds = join("",0,$seconds); } }
{ if ($minutes<10) { $minutes = join("",0,$minutes); } }
{ if ($hours<10) { $hours = join("",0,$hours); } }
{ if ($day_of_month<10) { $day_of_month = join("",0,$day_of_month); } }
{ if ($month<10) { $month = join("",0,$month); } }
$yr=$year - 100;
{ if ($yr<10) { $yr = join("",0,$yr); } }

$stamp = "$yr$month$day_of_month - $hours$minutes";
print "stamp = $stamp\n";
'
###

All good so far, right? Result on this one is: stamp = 060601 - 0021

The problem comes in with trying to subtract 9 hrs from the original baseline value. So, 1151727708- (9*60*60) = 1151724108

For some reason, the result comes back as: stamp = 060530 - 2321 but it should be stamp = 060531 - 2321.

Any ideas? This seems to happen at the end of the months.

Quote Reply
Re: [dlucks] localtime's time-of-the-month In reply to
It helps to read the documentation
Quote:
$mon is the month itself, in
the range 0..11 with 0 indicating January and 11 indicating
December.

Likewise, 5 is for June and 6 is for July. And if you don't know off the top of your head, a quick glance at a calendar will tell you that there are only 30 days in June. If, however, you adjust your month appropriately, it might work as expected.

(If your code were clearer, I would have figured that out immediately. An example of where I'd clean up would be to remove those anonymous code blocks and just replace all of those conditionals with a single 'sprintf'.)