Time + time.local

Hi -

I cant seem to get localtime to work, can anyone share some gotchas?

check environment is setup

[dc@fox:~]$ echo $TZ
Japan/Tokyo

[dc@fox:~]$ irb
irb(main):001:0> ENV[‘TZ’]
=> “Japan/Tokyo”

so the env is coming through ok… or is it?

irb(main):021:0> u=Time.now.utc
=> Tue Jan 04 06:21:25 UTC 2011

irb(main):019:0> t1=Time.now.localtime
=> Tue Jan 04 06:21:02 +0000 2011
irb(main):020:0> t1.zone
=> “UTC”

so it seems zone is being ignored

is there any way to override/forcibly set the timezone?

irb(main):006:0> t1.localtime
=> Tue Jan 04 06:12:56 +0000 2011

doesnt change, i guess cos zone is locked

irb(main):009:0> t1.localtime(“+09:00”)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):9:in `localtime’
from (irb):9
from :0

according to docs this should work:

http://www.ruby-doc.org/core/classes/Time.html#M000346

ok, trying to set the timezones

irb(main):001:0> Time.new.zone
=> “UTC”
irb(main):002:0> ENV[“TZ”]=“JST”
=> “JST”
irb(main):003:0> Time.new.zone
=> “UTC”
irb(main):004:0> ENV[“TZ”]=“PST”
=> “PST”
irb(main):005:0> Time.new.zone
=> “UTC”
irb(main):006:0> ENV[“TZ”]=“PST+8”
=> “PST+8”
irb(main):007:0> Time.new.zone
=> “PST”

ftw PST and JST get ignored, but “PST+8” gets accepted as PST?
ok i guess… we need a +X so hacking gives:

irb(main):003:0> ENV[“TZ”]=“JST+0”
=> “JST+0”
irb(main):004:0> Time.new.zone
=> “JST”
irb(main):005:0> Time.now.localtime
=> Tue Jan 04 06:41:55 +0000 2011

ok, but the time hasnt changed, so JST is still not recognized…

then as PST:

irb(main):008:0> Time.now
=> Mon Jan 03 22:32:51 -0800 2011
irb(main):009:0> Time.now.utc
=> Tue Jan 04 06:32:55 UTC 2011

irb(main):011:0> Time.now.localtime
=> Mon Jan 03 22:33:01 -0800 2011
irb(main):012:0>

so any Time.now defaults to localtime?

[dc@fox:~]$ export TZ=JST
[dc@fox:~]$ echo $TZ
JST
[dc@fox:~]$ irb
irb(main):001:0> ENV[‘TZ’]
=> “JST”
irb(main):002:0> Time.now.zone
=> “UTC”
irb(main):003:0>

but cannot be passed via the shell.

so by conclusion it seems i can get PST and GMT timezones, but not
japan.
also the calculation methods to convert timezones seem broken.

do i need to use other libs to deal with timezones effectively?

tx,

/dc


$B!!(BDavid ‘DC’ Collier
Pikkle $B3t<02q<R(B
$B<hDyLr<RD9(B
[email protected]
+81 (0)80-2037-1965

$BJg=8Cf(B! http://pikkle.com/company/hiring

On Jan 3, 10:45pm, d c [email protected] wrote:

irb(main):001:0> ENV[‘TZ’]
=> “Japan/Tokyo”

so the env is coming through ok… or is it?

irb(main):021:0> u=Time.now.utc
=> Tue Jan 04 06:21:25 UTC 2011

Unless you’ve ziced otherwise, you’ll need to use the right zone
name. Else Ruby will fall back to UTC.

[sshaw@localhost trunk]$ TZ=“Galaxy/Andromeda” ruby -e’p
Time.now.localtime’
Tue Jan 04 07:48:42 +0000 2011
[sshaw@localhost trunk]$ TZ=“Galaxy/Andromeda” ruby -e’p
Time.now.localtime.zone’
“Galaxy/Andromeda”
[sshaw@localhost trunk]$ grep -i galaxy /usr/share/zoneinfo/zone.tab
[sshaw@localhost trunk]$ grep -i ‘japan|tokyo’ /usr/share/zoneinfo/
zone.tab
JP +353916+1394441 Asia/Tokyo
[sshaw@localhost trunk]$ TZ=“Asia/Tokyo” ruby -e’p Time.now.localtime’
Tue Jan 04 16:45:55 +0900 2011
[sshaw@localhost trunk]$ strings /usr/share/zoneinfo/Japan | grep -i
jst
JST-9
[sshaw@localhost trunk]$ TZ=“JST-9” ruby -e’p Time.now.localtime’
Tue Jan 04 16:53:04 +0900 2011

-Skye

2011-01-04 15:45 d c [email protected]:

I cant seem to get localtime to work, can anyone share some gotchas?

Ruby uses a C-level function, localtime(), to obtain the timezone
information .
localtime() uses the timezone OS provides.

POSIX defines a form of TZ environment variable.
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html

According to this definition, JST-9 represent the timezone of Japan.

% TZ=JST-9 ./ruby -ve ‘t = Time.now; p [t, t.zone]’
ruby 1.9.3dev (2011-01-01 trunk 30441) [i686-linux]
[2011-01-05 13:25:36 +0900, “JST”]

If OS uses tzcode by Arthur David Olson,
TZ format is extended for the location’s full name.

irb(main):009:0> t1.localtime(“+09:00”)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):9:in `localtime’
from (irb):9
from :0

according to docs this should work:

class Time - RDoc Documentation

time.localtime(utc_offset) is available since Ruby 1.9.2.