Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Time Zones and Users

by Torgo (Beadle)
on Jan 10, 2002 at 22:19 UTC ( #137793=perlquestion: print w/replies, xml ) Need Help??

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

OK. I know that someone, somewhere MUST have done this all before, but it ain't in the cookbook and damned if I can find it on the net.

I'm writing a time-sensitive application (a calendar/scheduler sort of thing) which may have users from time zones different than the server's time zone logging in. Each user has their own preferences, and I can easily add a dropdown or something that lets them select what time zone they're in.

Here's the big question: How do I go about converting epoch GMtime to a user's arbitrary timezone? A real timezone, accounting for daylight savings time, or absence thereof. Oh, another kicker is that it has to work on both UNIX and Windows servers (stop laughing!) so dependancy on /usr/share/zoneinfo is also a no-no.

--
I write the code while the master is away!

Replies are listed 'Best First'.
Re: Time Zones and Users
by count0 (Friar) on Jan 10, 2002 at 22:32 UTC
    I've never done this, so I can't walk you through it... But here's a ray of light:

    The Time-modules package from CPAN will definitely help (and specifically Time::Timezone).
      The problem with Time::Timezone is that it relies on the $ENV{TZ} variable, which (as far as I can tell) relies on the /usr/share/zoneinfo files.

      --
      I write the code while the master is away!
        From Time::Zone (which is the same as Time::Timezone iirc):
        $TZ = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'GMT' ) : '' unless $TZ;
        Where $TZ is taken as the first argument (to the functions needing a base timezone). =)

        So, in other words, you can pass it a value and it does not rely on there being a TZ environment variable.
Re: Time Zones and Users
by TStanley (Canon) on Jan 10, 2002 at 22:32 UTC
    A quick search on CPAN turned up several choices such as Time::Timezone and Time::Zone. For a complete list of what I found, you can go here.

    TStanley
    --------
    "Suppose you were an idiot... And suppose you were a
    member of Congress... But I repeat myself." -- Mark Twain

Re: Time Zones and Users
by thor (Priest) on Jan 11, 2002 at 10:23 UTC
    At my company, we run several instances of a scheduler program all around the world. All any of them care about is 'what is my local time?'. The user makes the adjustment. For instance, I'm in Minneapolis, MN and the scheduler that I'm looking at is in Sydney, Australia. I have to keep track of the time difference (right now, it is 17 hours I think). I think that this is the way to go. If you will be doing anything local to the server, you should be doing everything on the servers time.

    Of course, if it really matters to you, you could do some version of an rsh to the remote server to execute a timelocal((localtime())[0,1,2,3,4,5]) and store it in a variable. Do the same thing on the client side, and find the difference between the two. Pass the difference into gmtime to get the difference in hours and minutes (not all timezones are an integer number of hours apart, so the minutes are necessary).

    thor
Re: Time Zones and Users
by belg4mit (Prior) on Jan 11, 2002 at 10:56 UTC
    Try this:
    perl -e 'print scalar localtime; $ENV{TZ}="PST"; print scalar localtim +e;'
    Assuming you aren't in PST. and then you might just local the TZ within a block when you do the calculations etc.

    PD> Corollary, let the OS and perl internal magic do as much work for you as it can.

    --
    perl -pe "s/\b;([st])/'\1/mg"

      Hmmm. That's a good method, but it doesn't seem to work for Windows servers. Yeah, a crime against nature, but it's a requirement, and it's making this simple issue into a total beast.

      Thor, your idea of just setting a +/- X hours is nice, but doesn't take into account Daylight Savings Time. A lot of the world and some parts of America (Arizona, east Indiana) don't deal with it at all. If you do DST in your area, Check the current time on this site. If you set your options when our clocks were forward, it'll be off by one hour. That's fine for a realtime site like this, but no good for a scheduling app.

      --
      I write the code while the master is away!
        Ummm, I never said anything about +/- X hours...

        --
        perl -pe "s/\b;([st])/'\1/mg"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2022-05-19 01:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?