Class: Time

Inherits:
Object show all
Defined in:
lib/couchrest/monkeypatches.rb,
lib/couchrest/mixins/properties.rb

Overview

This file must be loaded after the JSON gem and any other library that beats up the Time class.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mktime_with_offset(string) ⇒ Object

returns a local time value much faster than Time.parse



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/couchrest/mixins/properties.rb', line 6

def self.mktime_with_offset(string)
  string =~ /(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2}) ([\+\-])(\d{2})/
  # $1 = year
  # $2 = month
  # $3 = day
  # $4 = hours
  # $5 = minutes
  # $6 = seconds
  # $7 = time zone direction
  # $8 = tz difference
  # utc time with wrong TZ info: 
  time = mktime($1, RFC2822_MONTH_NAME[$2.to_i - 1], $3, $4, $5, $6, $7)
  tz_difference = ("#{$7 == '-' ? '+' : '-'}#{$8}".to_i * 3600)
  time + tz_difference + zone_offset(time.zone) 
end

Instance Method Details

#to_json(options = nil) ⇒ Object

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.)



13
14
15
16
# File 'lib/couchrest/monkeypatches.rb', line 13

def to_json(options = nil)
  u = self.getutc
  %("#{u.strftime("%Y/%m/%d %H:%M:%S +0000")}")
end