aris
August 30, 2012, 2:32am
1
I’m working on an external interface where a partner website is giving
us date time as strings.
dt = DateTime.parse( “Wed, 29 Aug 2012 17:26:44 -0700” )
=> Wed, 29 Aug 2012 17:26:44 -0700
dt.strftime( “%z” )
=> “-0700”
What I have to do is extract the UTC time zone. So in the prior example
I need an integer of -7. (UTC-7) in this scenario.
Is there some easy way of doing this?
thanks!
On Aug 29, 2012, at 5:32 PM, Dan Q. [email protected] wrote:
Is there some easy way of doing this?
What do you do for “Wed, 29 Aug 2012 17:26:44 +0630” (Myanmar and Cocos
Islands) or other half-hour time zones?
zone_offset = df.strftime(“%z”).to_i 10
“UTF%+i” % (zone_offset / 100)
On Wed, Aug 29, 2012 at 5:32 PM, Dan Q. [email protected] wrote:
I’m working on an external interface where a partner website is giving
dt = DateTime.parse( “Wed, 29 Aug 2012 17:26:44 -0700” )
=> Wed, 29 Aug 2012 17:26:44 -0700
dt.strftime( “%z” )
=> “-0700”
What I have to do is extract the UTC time zone. So in the prior example
I need an integer of -7. (UTC-7) in this scenario.
1.9.3p194 :013 > dt.zone
=> “-07:00”
1.9.3p194 :014 > dt.zone.to_i
=> -7
1.9.3p194 :015 >
HTH,
Hassan S. wrote in post #1073849:
OH. thanks!!!
On Wed, Aug 29, 2012 at 5:52 PM, Dan Q. [email protected] wrote:
OH. thanks!!!
Note: that was simplistic, and my main point, poorly made, was that
dt.methods
shows you a method ‘zone’ which you can use.
And as Eric pointed out, to_i doesn’t help with irregular timezones,
e.g. Kathmandu where it’s GMT +05:45
I was going to suggest converting to a Time via DateTime#to_time, so
you could use Time#utc_offset / 3600, but ‘to_time’ doesn’t preserve
the timezone. There is a Datetime#offset that you can use, but i
returns a Rational that’s a fraction, so you can do something like
this -
1.9.3p194 :018 > dt = DateTime.parse( “Wed, 29 Aug 2012 17:26:44 -0700”
)
=> #<DateTime: 2012-08-29T17:26:44-07:00
((2456170j,1604s,0n),-25200s,2299161j)>
1.9.3p194 :019 > dt.offset.numerator
=> -7
That seems a bit wonky though.