Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Convert EST to GMT

by Anonymous Monk
on Nov 04, 2011 at 19:14 UTC ( [id://935999]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have a file that shows date and time in EST, but I need to convert this to GMT. The date in the file is in this format: 2011-01-07 08:59:54.000 I already have it parsed so that date(by year/month/date) and time (hour/min/sec/milli) is separated. But I would need to mostly convert the hour to show GMT. What would be the best way to go about this? I know that most of the time the difference is 5 hours, but I need to have it account for the day light savings etc.. Thanks!

Replies are listed 'Best First'.
Re: Convert EST to GMT
by CountZero (Bishop) on Nov 04, 2011 at 20:57 UTC
    use Modern::Perl; use DateTime; my $dt = DateTime->new( year => 2011, month => 1, day => 7, hour => 8, minute => 59, second => 54, time_zone => 'America/New_York', ); say $dt->set_time_zone('GMT');
    output: 2011-01-07T13:59:54

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Convert EST to GMT
by ikegami (Patriarch) on Nov 04, 2011 at 21:20 UTC

    DateTime::Format::Strptime

    use strict; use warnings; use feature qw( say ); use DateTime::Format::Strptime qw( ); my $format = DateTime::Format::Strptime->new( pattern => '%F %T.%3N', locale => 'en', time_zone => 'local', on_error => 'croak', ); my $dt = $format->parse_datetime('2011-01-07 08:59:54.123'); $dt->set_time_zone('UTC'); say $format->format_datetime($dt); # 2011-01-07 13:59:54.123

    It's not clear what you mean by 'EST'. Here are the time zones you might be interested in using:

    • 'local' is the same as 'America/New_York' if your machine uses that time zone.
    • 'America/New_York' will actually factor in DST, meaning it will use UTC-4 if the date is in summer and UTC-5 if the date is in winter. Note that your format is ambiguous one hour each year if this is its time zone.
    • '-500' will use UTC-5 for all dates.
    • 'EST' is an ambiguous alias for '-500' (as Australia also has an EST). It's probably best to avoid it.
Re: Convert EST to GMT
by JavaFan (Canon) on Nov 04, 2011 at 20:57 UTC
    Some pedantic notes: 1) Transforming from EST to GMT is easy: they always differ 5 hours. But you probably have a time that is either in EST or EDT. 2) EST is an ambiguous name, it could mean Eastern Standard Time, from either North America, or Australia, or it could mean European Summer Time. 3) Without knowing whether the timezone is EST or EDT, there's one hour a year in which you can cannot uniquely map the time to GMT. 4) EDT lasts longer than EST, so most of the time the difference is actually 4 hours. 5) I prefer to use UTC over GMT.
Re: Convert EST to GMT
by Anonymous Monk on Nov 04, 2011 at 19:53 UTC
Re: Convert EST to GMT
by DrHyde (Prior) on Nov 08, 2011 at 10:27 UTC

    EST is ambiguous - it can be either UTC-5 or UTC+10. Yes Americans, Australians have clocks too!

    Assuming that you mean the Yankee version, then EST is *always* 5 hours off from GMT. ALWAYS. If you're interested in daylight "saving" (which, of course, doesn't exist) then you are also interested in EDT and BST.

    This all gets very complicated, so you should just use DateTime and use unambiguous timezones like Europe/London and America/New_York.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found