Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Time Zone conflict betwen clients in a web application

by py_201 (Acolyte)
on Dec 15, 2009 at 06:16 UTC ( [id://812825]=perlquestion: print w/replies, xml ) Need Help??

py_201 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have to enter user's time in my database and show it to them next time they login as last login time. How can i go about it in perl?
what i have thought about it is that i can take localtime of server and convert it to some standerd time and store it in database. then while retrieving the time, i can take the client time zone and convert my standerd time to that time zone and show it to user.
But the problem is how can i convert localtime to standerd time? and how can i get user's time zone automatically.
Thanks

Replies are listed 'Best First'.
Re: Time Zone conflict betwen clients in a web application
by ikegami (Patriarch) on Dec 15, 2009 at 07:11 UTC

    I have to enter user's time in my database and show it to them next time they login as last login time.

    You could use JavaScript to populate a hidden field with their localtime when they login.

    If you don't want to rely on JS or some other client-side application, you'll need to have the user select his timezone from a form like our Timezone Settings. Then, if you simply store the result of time on login, you can generate the user's last login time in the user's time zone using

    my $dt = DateTime->from_epoch( epoch => $epoch_time_stored_in_db, time_zone => $time_zone_stored_in_db, ); print $dt->strftime("Last login at %Y-%m-%d %H:%M:%S\n");
Re: Time Zone conflict betwen clients in a web application
by tokpela (Chaplain) on Dec 15, 2009 at 10:12 UTC

    You should be storing UTC time (also known as GMT) so that all of your timeframes are common across all timezones for consistency. Then, when you need to show the users their time - you can convert from GMT to localtime.

    You can get the gmtime in seconds by using the Time::Local module. Here is a snippet that you can use to show how to do the time conversions:

    use strict; use warnings; use Time::Local; my $time = time(); my $timegm = timegm(gmtime($time)); my $gmt = gmtime($time); my $timelocal = timelocal(localtime($time)); my $lmt = localtime($time); print "TIME: [$time]\n"; print "GMT: [$timegm]\n"; print "GMT AS TEXT: [$gmt]\n"; print "LOCALTIME: [$timelocal]\n"; print "LOCALTIME AS TEXT: [$lmt]\n";

    To get the localtime of GMT use the seconds value of the gmtime in localtime

    my $timelocal = timelocal(gmtime($timegm)); my $lmt = localtime($timegm);

Re: Time Zone conflict betwen clients in a web application
by Anonymous Monk on Dec 15, 2009 at 06:38 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://812825]
Approved by redgreen
Front-paged by redgreen
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-19 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found