Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/property/core_ext/time.rb

Overview

We provide our own ‘to_json’ version because the default is just an alias for ‘to_s’ and we do not get the correct type back.

In order to keep speed up, we have done some compromizes: all time values are considered to be UTC: we do not encode the timezone. We also ignore micro seconds.

Constant Summary collapse

JSON_REGEXP =
/\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)\z/
JSON_FORMAT =
"%Y-%m-%d %H:%M:%S"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.json_create(serialized) ⇒ Object



10
11
12
13
14
# File 'lib/property/core_ext/time.rb', line 10

def self.json_create(serialized)
  if serialized['data'] =~ JSON_REGEXP
    Time.utc $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i
  end
end

Instance Method Details

#to_json(*args) ⇒ Object



16
17
18
# File 'lib/property/core_ext/time.rb', line 16

def to_json(*args)
  { 'json_class' => self.class.name, 'data' => strftime(JSON_FORMAT) }.to_json(*args)
end