Rie! <[EMAIL PROTECTED]> writes: > Does anybody out there know how to change UTC to PST? > > I've been banging my heads on the wall since yesterday and what I know > is only set time to PST instead of UTC. Please help.. > > m:x arie$ script/console > Loading development environment (Rails 2.0.2) >>> Time.zone > => #<TimeZone:0x211c608 @utc_offset=-28800, @name="Pacific Time (US & Canada) > "> >>> Time.zone.now > => Thu, 27 Mar 2008 03:37:05 PDT -07:00 >>> Time.now > => Thu Mar 27 18:37:21 +0800 2008 >>> a = Time.now > => Thu Mar 27 18:41:20 +0800 2008 >>> a.to_pst > I only need to show that "a" in PST instead of UTC. Any ideas?
Ruby's Time class understands only two timezones: UTC and local. Local timezone is determined throught the value of the TZ environment variable. It is a very simple class meant as a thin wrapper of the number of seconds since UNIX epoch. There are several alternatives for doing timezone manipulations: 1. DateTime. It's too complex for me. If you know how to use it, good for you. 2. http://tzinfo.rubyforge.org/. Simple to use, but it uses its own timezone database instead of the system's. So, while it can be used portably, it also requires maintenance because timezone information changes. 3. Rails' TimeZone class. Not much experience here. The web site above says that it does not support daylight savings time which makes it useless. 4. And lastly my favourite: http://www.a-k-r.org/ruby-tzfile/. It has a simple interface, and uses the system's timezone information. The disadvantage is it uses its own Time class which means various enhancements to Ruby's Time class is not usable. Still, porting enhancements for Ruby's Time to TZInfo's Time is straightforward enough. As an example, below is my port of the Time#iso8601 enhancement. irb> require 'tzfile-instrument.rb' true irb> PacificTime = TZFile.create("US/Pacific") PacificTime irb> EasternTime = TZFile.create("US/Eastern") EasternTime irb> PacificTime.at(EasternTime.now.to_i) Fri Mar 28 13:00:06 PDT 2008 (1206734406+0 -07:00:00 US/Pacific DST) irb> EasternTime.now Fri Mar 28 16:00:15 EDT 2008 (1206734415+0 -04:00:00 US/Eastern DST) irb> PacificTime.at(EasternTime.now.to_i).iso8601 "2008-03-28T13:00:37-07:00" $ cat tzfile-instrument.rb require 'tzfile' module TZFile class Time def iso8601_tzoffset t = current_data.tt.gmtoff sign = t < 0 ? '-' : '+' t = t.abs t, sec = t.divmod(60) hour, min = t.divmod(60) return sprintf("%s%02d:%02d", sign, hour, min) end def iso8601 strftime("%Y-%m-%dT%H:%M:%S")+iso8601_tzoffset end end end YS.

