in reply to Date Time problem.
There are three time functions included within Perl itself:
1. localtime()
2. gmtime()
3. time() - makes an epoch second value for "now", can be used by either gmtime() or localtime()
The module Time::Local contains the inverse of those functions:
1. timelocal()
2. timegm()
3. (there is no "inverse" of time() per sea)
The operating system keeps track of "time" based upon a continually incrementing integer number of seconds since a specific start date/time. This is called the "epoch time" (returned time() value of zero). For Unix and Windows this is 00:00:00 Jan 1, 1970. I seem to remember that some versions of Apple's OS'es use a different value for this "epoch date/time". The point being is that this "epoch time" value is not transportable in a general sense between platforms. Converting an "epoch time" to a text string is a good way to ensure portability.
The time() function produces the current value of this continuously incrementing number of "epoch seconds" since the start of the "epoch" and corresponds to "now". This number never decreases and is independent of daylight savings time. When we "set our clocks back one hour", this "seconds since epoch" number just keeps growing. Just because we set our clocks back one hour, that doesn't change the fact that more seconds are continuing to accrue since the "epoch time".
The general way to do "time math" is to convert to epoch seconds, do math in seconds and then convert back to a string. The DateTime module does it that way too and it can apply some fancy "correction values for leap seconds, etc.
Some simple example code...
#!usr/bin/perl -w use strict; my $one_day_secs = 24*60*60; my $today = time(); my $today_string = localtime($today); my $yesterday_string = localtime($today-$one_day_secs); print "today as epoch time: $today\n"; print "today: $today_string\n"; print "yesterday: $yesterday_string\n"; print time __END__ output: today as epoch time: 1252088732 today: Fri Sep 4 11:25:32 2009 yesterday: Thu Sep 3 11:25:32 2009
I recommend and use UTC, Zulu, GMT time in log files (that basically is all the same thing).
Update: the OP's idea of adding zero is a GREAT idea. The idea is that default alpha sort will produce correct answers. A simple substitution to add a zero if only a single digit appears to suffice.
#!/usr/bin/perl -w use strict; my $a = "1"; $a =~ s/^(\d)$/0$1/; print $a;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Date Time problem.
by ikegami (Patriarch) on Sep 05, 2009 at 02:39 UTC | |
by Marshall (Canon) on Sep 05, 2009 at 04:00 UTC | |
by ikegami (Patriarch) on Sep 05, 2009 at 04:53 UTC | |
by Marshall (Canon) on Sep 05, 2009 at 06:35 UTC | |
by ikegami (Patriarch) on Sep 05, 2009 at 07:17 UTC | |
|