Julian said:
On an off-topic note: you should really use the internationally
standardized ISO date format[1], which is YYYY-MM-DD. Most other
date formats, like MM-DD-YY, are either confusing or ambiguous, or
both.
Even if you can't use the ISO format in your application
immediately, you should plan using it in the future and advocate it
towards friends, colleagues, and bosses. ;-)
[1]
http://www.cl.cam.ac.uk/~mgk25/iso-time.html
That comment made me reconsider the way I have been dealing with dates
in an internationalized program I'm maintaining. I concluded that
there is no need to struggle with 'Jan' or 'January' etc., creating a
need to translate the months.
I was able to reduce my need to three types of time representations:
1) A pure time stamp
2) Today's date
3) The date X days ago
So I wrote this code:
our $time = time;
sub timestamp {
my $fmt = (shift or '');
my $days = (shift or 0);
my @t = (gmtime($time - $days * 86400))[0..5];
return (sprintf "%d-%02d-%02d", $t[5] += 1900, ++$t[4], $t[3])
. ($fmt eq 'date' ? '' : sprintf "T%02d-%02d-%02dZ", @t[2,1,0]);
}
timestamp() returns 2003-07-22T20-28-04Z
timestamp('date') returns 2003-07-22
timestamp('date', 5) returns 2003-07-17
That's all. Thanks for making me think, Julian.
