Gossamer Forum
Home : General : Perl Programming :

another time zone question

Quote Reply
another time zone question
i've read all the posts but there's got to be an easy solution to my problem!

my server is on pacific time. i'm on eastern, both America. i have the following code:
Code:

@stats = stat "$auth_dir/$file";
use POSIX 'strftime';
my $date = strftime("%m/%d/%Y %H:%M:%S",localtime($stats[9]));
i want to display the time in MY timezone Eastern, instead of GMT or Pacific. help? thanks
Quote Reply
Re: [delicia] another time zone question In reply to
You could try:

http://search.cpan.org/...DateTime/TimeZone.pm
Code:
use DateTime;
use DateTime::TimeZone;

my $tz = DateTime::TimeZone->new( name => 'America/Chicago' );

my $dt = DateTime->now();
my $offset = $tz->offset_for_datetime($dt);

The list of timezones can be found here:

http://search.cpan.org/.../TimeZone/Catalog.pm

Hopefully that helps :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] another time zone question In reply to
i found lots of info on getting current time in my timezone. but where do i put $stats[9] to convert the time the file was created to my time zone?
Quote Reply
Re: [delicia] another time zone question In reply to
There is a saying - don't reinvent the wheel ;) Just use the DateTime module (standard on servers), as this will handle that for you.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] another time zone question In reply to
I don't want to reinvent the wheel. I just font know how to roll it! I can convert current time to my time. The file was written up to six hours earlier and $stat [9] is the time it was created. How do I convert that time to my time zone? Hope your answer will make me feel even more stupid!
Quote Reply
Re: [delicia] another time zone question In reply to
Code:
use DateTime;
my $dt = DateTime->now( time_zone => 'Europe/Paris' ); # set this to the Timezone you want!
print "FOO: " . $dt->ymd() . " - " . $dt->hms() . "\n";

To do it with the timestamp of a file, just do something like:

Code:
my $dt = DateTime->now(
year => 2003,
month => 10,
day => 26,
hour => 1,
minute => 30,
second => 0,
time_zone => 'America/Chicago',
);

... and pass in the params (I *think* it cares if you have 05 - ie a leading 0, so be sure to trim that off)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Jan 19, 2018, 8:44 AM
Quote Reply
Re: [Andy] another time zone question In reply to
i couldn't figure out how to pass $stat[9] to the time function, but this is working:
Code:
use POSIX 'strftime';
use POSIX qw(tzset);
$ENV{TZ} = 'America/New_York';
@stats = stat "$auth_dir/$file";
my $date = strftime("%m/%d/%Y %H:%M:%S",localtime($stats[9]));
more research to see if changes will be needed for daylight savings time!