Time + time.local

D

d c

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...
http://www.timeanddate.com/library/abbreviations/timezones/

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
(e-mail address removed)
+81 (0)80-2037-1965

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

Skye Shaw!@#$

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

Unless you've `zic`ed 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
 

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

Forum statistics

Threads
473,913
Messages
2,570,027
Members
46,421
Latest member
WaylonBran

Latest Threads

Top