[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

parsing a time in a specific timezone

Paul Brannan

2/23/2005 5:05:00 PM

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




5 Answers

Malte Milatz

2/23/2005 8:18:00 PM

0

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

djberg96

2/23/2005 8:48:00 PM

0

Malte Milatz wrote:
> 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

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

Malte Milatz

2/23/2005 8:58:00 PM

0

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

Steven Jenkins

2/23/2005 9:34:00 PM

0

Paul Brannan wrote:
> 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



Navindra Umanee

2/23/2005 9:52:00 PM

0

Steven Jenkins <steven.jenkins@ieee.org> wrote:
> 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.