couchrest_model/lib/couchrest/monkeypatches.rb

26 lines
865 B
Ruby
Raw Normal View History

2008-09-12 06:09:39 +02:00
# this file must be loaded after the JSON gem
class Time
2008-09-26 23:31:29 +02:00
# This date format sorts lexicographically
# and is compatible with Javascript's new Date(time_string) constructor.
# Note this this format stores all dates in UTC so that collation
# order is preserved. (There's no longer a need to set ENV['TZ'] = 'UTC'
# in your application.)
2008-09-12 06:09:39 +02:00
def to_json(options = nil)
2008-09-26 23:31:29 +02:00
u = self.utc
%("#{u.strftime("%Y/%m/%d %H:%M:%S +0000")}")
2008-09-12 06:09:39 +02:00
end
2008-09-26 23:31:29 +02:00
# Decodes the JSON time format to a UTC time.
# Based on Time.parse from ActiveSupport. ActiveSupport's version
# is more complete, returning a time in your current timezone,
# rather than keeping the time in UTC. YMMV.
2008-09-12 06:09:39 +02:00
# def self.parse string, fallback=nil
# d = DateTime.parse(string).new_offset
# self.utc(d.year, d.month, d.day, d.hour, d.min, d.sec)
# rescue
# fallback
# end
end