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

Dear Monks,

I need to display the time for a specific time zone on a PC no matter where the PC is physically located in the world. For example if the resource time zone is in California and the PC is in New York, I want to show the time in California on the PC. I tried using 'DateTime':

my $dt = DateTime->from_epoch( epoch => time() ); $dt = set_time_zone('America/Los_Angeles'); my $pctime = $dt->epoch();
But this returns the time for the server location.

I can manually calculate it for the US, but would like to have it work for any PC in the world. Any help appreciated.

Note: Just so you understand what I'm doing, I'm sending the time in milliseconds to the PC for display by a 'javascript' routine.

Regards...Ed

"Well done is better than well said." - Benjamin Franklin

Replies are listed 'Best First'.
Re: Display Specific Time on Any PC.
by kennethk (Abbot) on Mar 24, 2014 at 15:00 UTC
    Essentially, you need to find the time zone of a client computer; since this isn't embedded in the HTTP protocol, the only way Perl can obtain the data is essentially to ask the client. This means your solution is going to be JavaScript, not Perl.

    See this solution on Stack overflow.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thanks kennethk,

      I may be over simplifying the solution to this problem, but the examples could be anywhere. Server in Japan, resource in Hawaii, PC in Rome. What I currently believe is the server in Japan must send the seconds from the epoch for the resource in Hawaii to the PC in milliseconds.

      Does that make sense?

      UPDATE: My assumption is wrong! I need to include the PC time in the algorithm.

      Regards...Ed

      "Well done is better than well said." - Benjamin Franklin

        I'm having trouble following your spec. You have two ideas here:
        • A timestamp in epoch
        • A mapping to the local time
        Some kind of interrogation has to occur in order to reveal what any given machine believes the answer to any question is. It seems like it should be as simple as having your server parrot a timestamp from your resource in Hawaii to the PC in Rome, and then you could have Rome convert that in JavaScript for display. Alternatively, you could interrogate the PC in Rome for a time zone, and format on the server. I'm not tracking where the issue is for you in implementing this.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Display Specific Time on Any PC.
by Your Mother (Archbishop) on Mar 24, 2014 at 22:26 UTC
Re: Display Specific Time on Any PC.
by ww (Archbishop) on Mar 24, 2014 at 17:48 UTC

    The code you posted doesn't return server time, or anything else related to TOD:

    Undefined subroutine &main::set_time_zone called at ....

    ... which reflects your use (in line 2) of the assignment  = between $dt and set_time.... rather than the correct "->".

    Update/afterthought (and maybe not all that well thought out): if, as you say, "(you) can manually calculate it for the US" why can't you do so for any known-computer-location ... or, for that matter, for any computer using an identifiable, standard TZ for its location?

    Is the reason that you won't know where the server providing the time is located? If so, how could you be sure you know where server providing time to a particular (but unspecified) computer is the US?

    I'm with ken here. Please un-confuse me [ :-) us, if relevant].


    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.

      Sorry ww,

      I can't directly copy/paste from my 'aixterm' emulator to the browser, so I mistyped the code.

      Regards...Ed

      "Well done is better than well said." - Benjamin Franklin