User Control Panel
Advertisements

HELP US, HELP YOU!

GMT time()?

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl
View unanswered posts
Author Message
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Wed Apr 20, 2005 7:20 pm    Post subject: Reply with quote

Well first I'll explain what I'm trying to do:

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/
Back to top
darkmonkey
The Merovingian
The Merovingian


Joined: 18 Apr 2004
Posts: 2557
Location: London, England
Reputation: 39.3Reputation: 39.3Reputation: 39.3Reputation: 39.3
votes: 7

PostPosted: Wed Apr 20, 2005 7:48 pm    Post subject: Reply with quote

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.

_________________
~ Josh
[ Need bot hosting on a dedicated server? PM me. ]
Back to top
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Wed Apr 20, 2005 7:51 pm    Post subject: Reply with quote

QUOTE(darkmonkey @ Apr 20 2005, 03:48 PM)
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. Razz

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/
Back to top
Siebe
God Like
God Like


Joined: 06 Jan 2004
Posts: 562
Location: Netherlands
Reputation: 39.8Reputation: 39.8Reputation: 39.8Reputation: 39.8

PostPosted: Sun Apr 24, 2005 12:59 pm    Post subject: Reply with quote

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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group