I have this code in my bot for getting time stamps using either local, gm, or another time zone:
Code:
sub timestamps {<br /> # The requested type.<br /> my ($for,$stamp,$zone) = (shift,shift,shift);<br /><br /> $for ||= "local";<br /><br /> # Time types:<br /> # local == Your local time.<br /> # gm == Greenwich Metric Time.<br /> # zone == A user's time zone (if you included $zone, it would<br /> # do that time zone anyway).<br /><br /> if ($for =~ /^gm/i) {<br /> $zone = 'GMT';<br /> }<br /><br /> # Go for backwards compatibility.<br /> $stamp =~ s/<month_name>/Month/ig;<br /> $stamp =~ s/<month_abbrev>/Mon/ig;<br /> $stamp =~ s/<day_name>/Weekday/ig;<br /> $stamp =~ s/<day_abbrev>/Day/ig;<br /> $stamp =~ s/<day_month>/d/ig;<br /> $stamp =~ s/<day_year>//ig;<br /> $stamp =~ s/<year_full>/yyyy/ig;<br /> $stamp =~ s/<year_short>/yy/ig;<br /> $stamp =~ s/<hour_12>/H/ig;<br /> $stamp =~ s/<hour_24>/h/ig;<br /> $stamp =~ s/<hour_ext>/AM/ig;<br /> $stamp =~ s/(<minutes>|<min>)/mm/ig;<br /> $stamp =~ s/(<seconds>|<secs>)/ss/ig;<br /><br /> # Modules used for getting date/time.<br /> use Time::Format qw(time_format);<br /> use Time::Zone;<br /><br /> # Format the time.<br /> my $gm = time();<br /> if (defined $zone) {<br /> my $offset = tz_offset($zone);<br /> $gm += $offset;<br /> }<br /><br /> my $format = time_format ($stamp,$gm);<br /> return $format;<br />}
Anyway, what I've found after doing some debugging, is that time() is relative. It gives a time according to my computer, not GM time.
Output:
Quote:
Local Date: Wednesday, April 20 2005 Local Time: 3:20:17 GM Time: 3:20:17 Your Time: 10:20:17
The local time and GM time should be different, and local time and "Your Time" should be the same.
I did some debugging. When the time zone was GMT, Time::Zone returned an offset of 0, so it didn't change the value of $gm which is why it gave the exact same time. When it went to get my time (EST), Time::Zone returned an offset of -18000 which it took from $gm, making it 5 hours previous to my own time.
Is there a way to get time() according to GMT, without having to do gmtime() and convert the values it returns into a value of time()?
Any help would be appreciated!
Also, my code with debug prints had this:
Code:
# Format the time.<br />my $gm = time();<br />print "Debug // Now Time = $gm\n";<br />print "Debug // Localtime: " . localtime($gm) . "\n";<br />if (defined $zone) {<br /> my $offset = tz_offset($zone);<br /> $gm += $offset;<br /> print "Debug // Using Timezone\n"<br /> . "\tOffset = $offset\n"<br /> . "\tNew Time = $gm\n";<br />}<br />print "Debug // Format Time = $gm\n";<br />print "Debug // Localtime: " . localtime($gm) . "\n\n====================\n\n";<br />my $format = time_format ($stamp,$gm);<br />return $format;
Output:
Code:
Debug // Now Time = 1114024817<br />Debug // Localtime: Wed Apr 20 15:20:17 2005<br />Debug // Format Time = 1114024817<br />Debug // Localtime: Wed Apr 20 15:20:17 2005<br /><br />====================<br /><br />Debug // Now Time = 1114024817<br />Debug // Localtime: Wed Apr 20 15:20:17 2005<br />Debug // Using Timezone<br /> Offset = 0<br /> New Time = 1114024817<br />Debug // Format Time = 1114024817<br />Debug // Localtime: Wed Apr 20 15:20:17 2005<br /><br />====================<br /><br />Debug // Now Time = 1114024817<br />Debug // Localtime: Wed Apr 20 15:20:17 2005<br />Debug // Using Timezone<br /> Offset = -18000<br /> New Time = 1114006817<br />Debug // Format Time = 1114006817<br />Debug // Localtime: Wed Apr 20 10:20:17 2005<br /><br />====================
It did: local time first, Then GM time (offset = 0) then my time (offset = -18000) _________________ Current Site (2008) http://www.cuvou.com/
I don't have the time or knowledge to help you, but just a picky English thing...
Quote:
gm == Greenwich Metric Time.
It's actually Greenwich Mean Time. [right][snapback]47893[/snapback][/right]
Meh, leave the comments alone.
I've found a temporary solution for it, using Time::Local. There's probably a much easier function, but this is what I came up with:
Code:
# Find the time() according to GMT.<br />my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time());<br />my $gm = Time::Local::timelocal ($sec,$min,$hour,$mday,$mon,$year);<br />$gm = time() unless defined $zone;
So if $zone isn't defined, it will go back to local time() anyway. But that gets the GMT time/date and turns it back to a value of time() using Time::Local. _________________ Current Site (2008) http://www.cuvou.com/
Joined: 06 Jan 2004 Posts: 562 Location: Netherlands
Posted: Sun Apr 24, 2005 12:59 pm Post subject:
Code:
my $gmtime = scalar(gmtime())
Who said you needed to convert? In array context (or rather, non-scalar context) it will return a list. When putting it in scalar() it will return a unix time stamp.