Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^3: Getting previous dates from the current date

by Smylers (Pilgrim)
on Mar 29, 2005 at 17:23 UTC ( #443196=note: print w/replies, xml ) Need Help??

in reply to Re^2: Getting previous dates from the current date
in thread Getting previous dates from the current date

simple math is all you need.

Except that the mathematics of date-handling is rarely simple. In this case you've made the error of assuming there are 24 hours in the day.

In the UK, for example, the clocks have just moved forwards by an hour for British Summer Time: Sunday only had 23 hours in it.

Most of the time your code will work just fine. But if it happens to be run in the first hour of a day then it will yield the wrong output, skipping a day.

For example, if I ran it at 00:17 early tomorrow (Wednesday) morning with $NUM_DAYS set to 2 then it would correctly output:

Mon Mar 28 00:17:00 2005

But setting $NUM_DAYS to 3 would yield:

Sat Mar 26 23:17:00 2005

The original poster just wanted dates; if you truncate the time portions off the above then you've ended up skipping a day, jumping straight from Monday to Saturday!

Now it would be possible to fix this, but it isn't worth trying when so many hours of hard thinking have already been put into getting these sorts of things write by the creators of the DateTime module and friends.

Do not try to do arithmetic with dates. You will get it wrong.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://443196]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-08-16 12:45 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (167 votes). Check out past polls.