views:

177

answers:

3

Each of my users has a (possibly) different TZ defined in their .bashrc. I have a Perl script that displays date/time and want it to have it display with their profile time zone.

Does anyone know the best way to do this?

+2  A: 

A simple answer is to use

    use POSIX
    .... 
    print strftime("%H:%M %Z", localtime)"

But the zone returned by %Z varies from system to sysetm. I have got GMT Daylight Time and BST on two different platforms.

Edit The following is an experminet showing that (at least for me) %Z takes account ot TZ

    export TZ=''
    perl -MPOSIX -e 'print strftime("%H:%M %Z\n", localtime)'
    export TZ=America/Los_Angeles
    perl -MPOSIX -e 'print strftime("%H:%M %Z\n", localtime)'

Produces output

    16:45 UTC
    09:45 PDT
justintime
%Z returns the server timezone. I am looking for a way to return the TZ set in the user's /.bashrc or /.bash_profile. Do you know of a way to do that? Sorry if my original message was unclear.
John
It returns the users timezone for me (debian). What odes the script above give for you.
justintime
Same output as you...so I am getting closer...If I put this into my perl script: $ENV{'TZ'}="America/Los Angeles"; it returns the correct TZ.Now, my perl script will run for any user, and I know the value of $CURRENT_USER which corresponds to /home/<user>/... how can I pull out that user's TZ variable from basrc or bash_profile?
John
A: 

Whats a "profile" in this context?

hpavc
In /home/<user>/.bashrc I have the following line:export TZ="/usr/share/zoneinfo/Europe/Paris"Another user @ /home/<user2>/.bashrc has:export TZ="/usr/share/zoneinfo/Europe/London"I'd like my perl script to use that TZ for its display.
John
+3  A: 

You can use the wonderful module DateTime for this:

use strict; use warnings;
use DateTime;

# extract timezone name from file in env variable
(my $tz = $ENV{TZ}) =~ s#^/usr/share/zoneinfo/##;
my $now = DateTime->now(time_zone => $tz);

print "The current time is: " . $now->strftime('%F %T %Z') . "\n";

When I run this with TZ="/usr/share/zoneinfo/Europe/Paris", I see:

The current time is: 2010-04-19 20:09:03 CEST

As for extracting the timezone data from the user's file: a quick solution is to store that user's TZ configuration in a separate file (which .bashrc could source), and you can parse it manually in the CGI (however this gets into a different topic: How can I best store per-user configurations for a CGI?)

# /home/<user>/.tz should have the content: export TZ="/usr/share/zoneinfo/timezonename"
open(my $tz_fh, "/home/${user}/.tz");
chomp(my $tz_data = <$tz_fh>);
close $tz_fh;
(my $tz_path) = $tz_data =~ m/^export TZ\s*=\s*"(.+)"$/;

# now $tz_path contains what $ENV{TZ} would contain in the earlier code snippet.
Ether
Cool -- and it runs on mine machien too... the question is how can it dynamically read from a user's profile? If the script is running under the "apache" user, and it grabs the $CURRENT_USER, how can I tell the script to pull the CURRENT_USER's environment variable?
John
Ah, I misunderstood -- I thought it was running under the user's environment, in which case the user's .bashrc would already be loaded. In that case, the best thing might be to store that user's TZ configuration in a separate file (which .bashrc could source), and you can parse manually in the CGI: I'll update my answer.
Ether
Wow, awesome! This is nice...
John