parsing a time in a specific timezone

P

Paul Brannan

I can use Time.parse to parse a timestamp in the local timezone, e.g.:

irb(main):001:0> require 'time'
=> true
irb(main):002:0> t = Time.parse('20050103-14:31:26')
=> Mon Jan 03 14:31:26 EST 2005

But how can I parse the above timestamp in a timezone other than the
local timezone (e.g. I want the method to assume that the timestamp is
in UTC)?

Paul
 
M

Malte Milatz

Paul Brannan:
irb(main):002:0> t = Time.parse('20050103-14:31:26')
=> Mon Jan 03 14:31:26 EST 2005

But how can I parse the above timestamp in a timezone other than the
local timezone

It's as easy as that:

VERSION
=> "1.8.2"
Time.parse('20050103-14:31:26 PST')
=> Mon Jan 03 23:31:26 CET 2005

In my example, there is an automatic conversion from Pacific Standard Time
to Central European Time.

Malte
 
D

Daniel Berger

Malte said:
Paul Brannan:

It's as easy as that:

VERSION
=> "1.8.2"
Time.parse('20050103-14:31:26 PST')
=> Mon Jan 03 23:31:26 CET 2005

In my example, there is an automatic conversion from Pacific Standard Time
to Central European Time.

Malte

I took his question to mean that he didn't want the automatic
conversion. He's wants a Time object in UTC.

The easiest solution seems to just be to slap the time zone
abbreviation at the end of the string:

Time.parse('20050103-14:31:26' << ' UTC')
=> Mon Jan 03 14:31:26 UTC 2005

HTH

Dan
 
M

Malte Milatz

Daniel Berger:
I took his question to mean that he didn't want the automatic
conversion. He's wants a Time object in UTC.

Ah. I don't know whether I really understood his question now, but then it
seems he wants Time#utc.

Malte
 
S

Steven Jenkins

Paul said:
I can use Time.parse to parse a timestamp in the local timezone, e.g.:

irb(main):001:0> require 'time'
=> true
irb(main):002:0> t = Time.parse('20050103-14:31:26')
=> Mon Jan 03 14:31:26 EST 2005

But how can I parse the above timestamp in a timezone other than the
local timezone (e.g. I want the method to assume that the timestamp is
in UTC)?

ENV['TZ'] = 'UTC'

or even easier, append a Z to the string:

$ irb
irb(main):001:0> require 'time'
=> true
irb(main):002:0> Time.parse('20050103-14:31:26')
=> Mon Jan 03 14:31:26 PST 2005
irb(main):003:0> Time.parse('20050103-14:31:26Z')
=> Mon Jan 03 14:31:26 UTC 2005

Steve
 
N

Navindra Umanee

Steven Jenkins said:
or even easier, append a Z to the string:

Or just the full timezone name (appending "UTC", "EDT", "PST" will all
have the expected effect).

Cheers,
Navin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,727
Members
45,664
Latest member
Phil79581

Latest Threads

Top