Time#zone

F

Farrel Lifson

I'm running Ruby 1.8 on XP (ruby 1.8.1 (2003-12-25) [i386-mswin32]).
Currently Time#zone returns a long form of the timezone for me :
irb(main):001:0> Time.now.zone
=> "South Africa Standard Time"

On Linux (ruby 1.8.1 (2004-05-02) [i686-linux-gnu]) I get the
shortened time zone code which is what I would like to get on XP:
irb(main):001:0> Time.now.zone
=> "SAST"

I've looked through Time#methods and I can't see a way to get the
shortened code on XP without doing some processing on the result
(probably just get the first letter of every word) which is not
something I'd like to do as the script will be run on both Linux and
XP and I'd like to have it as clean as possible with no OS specific
exceptions.

Thanks,
Farrel
 
A

Ara.T.Howard

I'm running Ruby 1.8 on XP (ruby 1.8.1 (2003-12-25) [i386-mswin32]).
Currently Time#zone returns a long form of the timezone for me :
irb(main):001:0> Time.now.zone
=> "South Africa Standard Time"

On Linux (ruby 1.8.1 (2004-05-02) [i686-linux-gnu]) I get the
shortened time zone code which is what I would like to get on XP:
irb(main):001:0> Time.now.zone
=> "SAST"

I've looked through Time#methods and I can't see a way to get the
shortened code on XP without doing some processing on the result
(probably just get the first letter of every word) which is not
something I'd like to do as the script will be run on both Linux and
XP and I'd like to have it as clean as possible with no OS specific
exceptions.

Thanks,
Farrel

i don't have a windows box handy, but this should work

~ > ruby -e 'print(Time.now.strftime("%z\n"))'
-0600

eg. use rfc numeric time zone offest codes instead of the abbreviated ones.

you could also try this

~ > ruby -e 'print(Time.now.strftime("%Z\n"))'
MDT

and play with with the env vars TZ and LC_TIME (man strftime). but i doubt
you'll get a uniform alpha time zone desc as my man page says

%Z The time zone or name or abbreviation.

eg. abbreviations may not not seem to be specified by the spec.

using

ENV['LC_TIME'] = 'C'
p Time.now.zone

or

ENV['LC_TIME'] = 'POSIX'
p Time.now.zone

__may__ affect output.


-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
F

Farrel Lifson

i don't have a windows box handy, but this should work
~ > ruby -e 'print(Time.now.strftime("%z\n"))'
-0600

eg. use rfc numeric time zone offest codes instead of the abbreviated ones.

I could use that but I would prefer to use the alphanumeric codes if possible.
you could also try this

~ > ruby -e 'print(Time.now.strftime("%Z\n"))'
MDT
Nope that don't work on XP:
irb(main):001:0> Time.now.strftime("%Z")
=> "South Africa Standard Time"

ENV['LC_TIME'] = 'C'
p Time.now.zone

or

ENV['LC_TIME'] = 'POSIX'
p Time.now.zone

__may__ affect output.

Also doesn't seem to work on XP:
irb(main):001:0> ENV["LC_TIME"]="C"
=> "C"
irb(main):002:0> Time.now.zone
=> "South Africa Standard Time"
irb(main):003:0> ENV["LC_TIME"]="POSIX"
=> "POSIX"
irb(main):004:0> Time.now.zone
=> "South Africa Standard Time"
irb(main):005:0>

I assume from this it seems that the Ruby interpreter gets the time
zone string (whether long or abbreviated) from the underlying OS? I'd
like to create a date string that is as close as possible to time
specification as set in RFC 822
(http://www.freesoft.org/CIE/RFC/822/39.htm). It would be nice if the
default Time#to_s returned a 822 compliant string though...

I guess I'm just going to have to go with either using the uct offset
or doing some processing.
 
A

Austin Ziegler

Nope that don't work on XP:
irb(main):001:0> Time.now.strftime("%Z")
=> "South Africa Standard Time"
I assume from this it seems that the Ruby interpreter gets the time
zone string (whether long or abbreviated) from the underlying OS? I'd
like to create a date string that is as close as possible to time
specification as set in RFC 822
(http://www.freesoft.org/CIE/RFC/822/39.htm). It would be nice if the
default Time#to_s returned a 822 compliant string though...

I guess I'm just going to have to go with either using the uct offset
or doing some processing.

Without Ruby having this information inbuilt, there's no way that it
can do this. IMO, Ruby shouldn't have this information inbuilt, it
should get the information from the OS. However, you could try this:

Time.now.zone.gsub(/([A-Z])\w+\b\s?/, '\1')
Time.now.zone.split.map { |e| e[0].chr }.join("")

There may be better ways to get the first letter of each word.
(Unfortunately, this is also language and locale specific).
Alternatively, you could include the ISO timezone definitions in your
class and convert between the UTC offset and the timezone definitions
-- but that's an iffy proposition (you can't tell regarding Indiana,
which switches between EST and CST instead of going on EDT).

-austin
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top