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

Hi, there:

Please forgive me if this is a very old question. :)

I am trying to put an element on the front page of my website which records the visitor's localtime. The function localtime returns the server-side's time which is not I want. Are there some nice methods or modules to fit this need? Really appreciate your time and suggestions..:)

Thanks,
LH
  • Comment on How to get client-side's localtime from the server-side

Replies are listed 'Best First'.
Re: How to get client-side's localtime from the server-side
by GrandFather (Saint) on Aug 06, 2007 at 00:25 UTC

    All the "nice methods or modules" run server side. Unless you know the client's time zone you are pretty much stuffed.

    For a site where a log in is required you can ask for, and store, the user's time zone.

    If you are a Javascript junkie then take a look at http://www.willmaster.com/possibilities/archives/wmp19990831001.shtml for a technique for determining a client's time zone.


    DWIM is Perl's answer to Gödel
Re: How to get client-side's localtime from the server-side
by andreas1234567 (Vicar) on Aug 06, 2007 at 08:04 UTC
    Geo::IP looks up country by IP Address, and from there you might be able to guess the time zone. There is both a free (gratis) and paid country database:

    www.maxmind.com/app/geoip_country

    use strict; use warnings; use Geo::IP; use DateTime; use DateTime::TimeZone; my $gi = Geo::IP->new(GEOIP_STANDARD); my $country_code = $gi->country_code_by_addr('24.24.24.24'); print qq{Country:\t$country_code}; my $dt = DateTime->now; foreach my $tz (DateTime::TimeZone->names_in_country($country_code)) { $dt->set_time_zone( $tz ); print $tz . qq{\t} . $dt->hms(); } __END__ $ perl -l 630749.pl Country: US America/New_York 04:19:57 America/Detroit 04:19:57 America/Kentucky/Louisville 04:19:57 America/Kentucky/Monticello 04:19:57 America/Indiana/Indianapolis 04:19:57 America/Indiana/Knox 03:19:57 America/Indiana/Winamac 04:19:57 America/Indiana/Marengo 04:19:57 America/Indiana/Vevay 04:19:57 America/Chicago 03:19:57 America/Indiana/Vincennes 03:19:57 America/Indiana/Petersburg 03:19:57 America/Menominee 03:19:57 America/North_Dakota/Center 03:19:57 America/North_Dakota/New_Salem 03:19:57 America/Denver 02:19:57 America/Boise 02:19:57 America/Shiprock 02:19:57 America/Phoenix 01:19:57 America/Los_Angeles 01:19:57 America/Anchorage 00:19:57 America/Juneau 00:19:57 America/Yakutat 00:19:57 America/Nome 00:19:57 America/Adak 23:19:57 Pacific/Honolulu 22:19:57
    --
    Andreas
Re: How to get client-side's localtime from the server-side
by Cody Pendant (Prior) on Aug 06, 2007 at 08:55 UTC
    Of course it can't be done on the server.

    You can get it with JavaScript and pass it to the server in a number of ways if that's what you want.

    Just off the top of my head, you could set a cookie with JavaScript when a page loads, and read it back in your cgi script. You could do it with Ajax, using an XMLHTTPRequest which talks to a script on your server in the background. You could hack a redirect so that foo.html redirects to bar.html?localtime=<their localtime>.

    All of those are dependent on their having the right browser and JavaScript turned on, etc.



    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...
Re: How to get client-side's localtime from the server-side
by Anonymous Monk on Aug 06, 2007 at 00:27 UTC
    Cannot be accomplished.
      How about getting client IP (which can be done), converting that to approximate geographical location, and from there to some approximation of time zone. At least one can figure out whether to say "good evening" or "good morning", based on this crude idea.

        You could do that, but it's important to understand that it's a heuristic and not a real concrete method.

        Another heuristic might be to look at the language preference information you get from the browser. You can guess that "en-US" is probably one of four time zones.

        Still another would be a wild guess based on the server's local time. You can guess that most of your hits at any time of day come from time zones that are in "waking hours" at that time (i.e., the Australians are active at a different time than the Americans).

        Quite a few sites I've been to (including this one) ask me my time zone as part of my user preferences. This is evidence enough for me that there isn't a good and easy solution to this problem.

Re: How to get client-side's localtime from the server-side
by Your Mother (Archbishop) on Aug 06, 2007 at 21:29 UTC

    This does not “record” the user’s time and, like others have said, it’s not possible to get the localtime from the user perfectly b/c you rely upon the user’s stuff being set right. If location specific display is your only goal then this is a nice trick, needs JS, to keep times neutered in the DB and let the clientside display what it believes to be the correct local version: Perl + JavaScript to display "correct" local times.