Making Rails time zone aware attributes and Chronic play well together
I came up with this monkeypatch to Time.zone.parse, so that it uses the Chronic time parsing library instead of Time.parse:
# requires technoweenie’s fork of the Chronic time parsing library
# http://github.com/technoweenie/chronic/tree/master
def parse(str)
Chronic.time_class = self
Chronic.parse(str, :now => now)
end
end
…with this in place, I can assign natural language date/time strings to model attributes, and the values will be parsed in the current Time.zone:
=> #<Event id: nil, title: nil, starts_at: "2008-09-18 22:00:00", created_at: nil, updated_at: nil>
>> e.starts_at
=> Thu, 18 Sep 2008 17:00:00 CDT -05:00
7 Comments