From: "Ara.T.Howard" Date: 2004-05-14T12:43:50+09:00 Subject: Re: Bug in Marshal? On 13 May 2004, matt wrote: > With the usual deep_copy: > > # Deep copy > class Object > def deep_copy > Marshal.load(Marshal.dump(self)) > end > end > > I get the following (irb 0.9(02/07/03)) > > irb(main):001:0> fmt = "%m-%d-%Y %H:%M" > => "%m-%d-%Y %H:%M" > irb> original = Time.gm(2004, 04, 25, 22, 56) > => Sun Apr 25 22:56:00 UTC 2004 > irb> copy = original.deep_copy > => Sun Apr 25 18:56:00 Eastern Daylight Time 2004 > irb> [original, copy].map {|t| t.strftime(fmt)} > => ["04-25-2004 22:56", "04-25-2004 18:56"] > irb> original.hour > => 22 > irb> copy.hour > => 18 > irb> original.hour <=> copy.hour > => 1 > irb> original <=> copy > => 0 > > Ruh Roh! i think this actually makes sense if you consider that a Time object is only storing the seconds since 1970 and the environment (TZ) affect how it's interpreted: ~ > TZ=America/Boston ruby -e 'p Time.now' Fri May 14 03:25:38 America/Boston 2004 ~ > TZ=America/Denver ruby -e 'p Time.now' Thu May 13 21:25:44 MDT 2004 consider that you might Marshal a Time object into some file, and that file gets shipped to africa - you definitely want some sort of 'absolute' time in there. however, there's no problem if, when the file is loaded back in, the Time object respects the locale settings - under the hood the time is still the same: irb(main):001:0> original = Time.gm(2004, 04, 25, 22, 56) => Sun Apr 25 22:56:00 UTC 2004 irb(main):002:0> copy = Marshal.load(Marshal.dump(original)) => Sun Apr 25 16:56:00 MDT 2004 irb(main):003:0> original.to_f => 1082933760.0 irb(main):004:0> copy.to_f => 1082933760.0 if we compare hours we are comparing ints, if we compare seconds since 1970 in either int or float we get and accurate comparison - which Time#<=> shows with the 0 result. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | URL :: http://www.ngdc.noaa.gov/stp/ | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done ===============================================================================