I've found the following workaround to be reliable. Prevents my
datetime fields from being returned with unexpected +01 timezone
offset after being fetched from the database by datamapper:

  alias raw_start_date start_date
  alias raw_end_date   end_date
  def start_date; d = self.raw_start_date; DateTime.civil( d.year,
d.month, d.day, d.hour, d.min, 0, 0 ); end
  def end_date;   d = self.raw_end_date;   DateTime.civil( d.year,
d.month, d.day, d.hour, d.min, 0, 0 ); end

It's still a hack. Would love to hear about a better solution.
Incidentally the "ENV['TZ']='utc'" idea did not solve this issue. All
it did was offset my logger entries by one hour! DM still returned
datetime fields with an offset.

Cheers,
George

On Jul 6, 7:37 am, George <[email protected]> wrote:
> Ok, thanks snusnu, I might try the "ENV['TZ']='utc'" suggestion though
> I suppose it will affect things like created_at as well, which should
> be saved with the actual zoned time.
>
> Does it not seem odd that, as per my example, the datetime value being
> returned by DM is different after it has been saved. DM may not be
> causing it but to me the effect is tantamount to a bug.
>
> It pains me to do it but I wondered if a hack like this might work
> around the problem:
>
>   alias raw_start_date start_date
>   def start_date; get_datetime_property_of( @trip, :start_date ); end
>
>   def get_datetime_property_of( model, prop, raw_prop = nil )
>
>     raw_prop ||= "raw_#{ prop }"
>     raw_date   = model.method(raw_prop).call
>
>     # Assume datetime is fine if it has not been saved yet:
>     if !raw_date || model.attribute_dirty?(prop)
>
>       return raw_date
>
>     # Allow for offset when reading datetime property from database:
> (DM adds offset)
>     # TODO: Cringe and deny all knowledge of this.
>     else
>
>       seconds_offset = raw_date.to_time.utc_offset
>       return (raw_date.to_time.utc + seconds_offset ).to_datetime
>
>     end
>
>   end
>
> On Jul 5, 2:20 pm, Martin Gamsjaeger <[email protected]> wrote:
>
> > George,
>
> > DM does nothing particular with timezones, so basically, like you
> > said, the TZ on your server is used. One way to force everything to be
> > considered UTC is to do ENV['TZ] = 'utc' before you start your
> > program. Also, you could try out Daniel's brandnew ZonedTypes [1] and
> > see if they work for you. I know the project is very young, so it
> > might not yet be fully there.
>
> > cheers
> > snusnu
>
> > [1]http://github.com/hassox/dm-zone-types
>
> > On Mon, Jul 5, 2010 at 13:52, George <[email protected]> 
> > wrote:
> > > Also tried the same test with a Time property. The same thing
> > > happened.
>
> > > On Jul 5, 12:01 pm, George <[email protected]> wrote:
> > >> After setting and saving a DateTime property, it is always returned
> > >> with +01 hour offset.
>
> > >> Is this by design? It makes comparison of datetime fields rather
> > >> tiresome.
> > >> (The +1 hour does match the timezone offset of the server but it is
> > >> not the time I asked datamapper to store!)
> > >> How should I handle datetimes better so I don't hit this problem?
>
> > >> Here's an example:
> > >>   trip.start_date = DateTime::civil( 2010, 5, 1, 13, 15, 0, 0 )
> > >>   trip.start_date.to_s.should == "2010-05-01T13:15:00+00:00" # PASS
> > >>   trip.save.should be_true # PASS
> > >>   trip.reload
> > >>   trip.start_date.to_s.should == "2010-05-01T13:15:00+00:00" # FAIL
>
> > >>   expected: "2010-05-01T13:15:00+00:00",
> > >>   got: "2010-05-01T13:15:00+01:00"
>
> > >> Incidentally the value that is stored in the database does exclude the
> > >> offset, so it seems to be added my DM when reading the data.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups 
> > > "DataMapper" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to 
> > > [email protected].
> > > For more options, visit this group 
> > > athttp://groups.google.com/group/datamapper?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to