~ubuntu-branches/ubuntu/wily/tdiary/wily

« back to all changes in this revision

Viewing changes to vendor/json_pure-1.6.6/lib/json/add/time.rb

  • Committer: Package Import Robot
  • Author(s): Hideki Yamane
  • Date: 2013-05-19 16:14:01 UTC
  • mfrom: (12.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130519161401-hf5oyr8g8a94fsew
Tags: 3.2.2-2
Upload to unstable 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
 
  require 'json'
3
 
end
4
 
 
5
 
# Time serialization/deserialization
6
 
class Time
7
 
 
8
 
  # Deserializes JSON string by converting time since epoch to Time
9
 
  def self.json_create(object)
10
 
    if usec = object.delete('u') # used to be tv_usec -> tv_nsec
11
 
      object['n'] = usec * 1000
12
 
    end
13
 
    if instance_methods.include?(:tv_nsec)
14
 
      at(object['s'], Rational(object['n'], 1000))
15
 
    else
16
 
      at(object['s'], object['n'] / 1000)
17
 
    end
18
 
  end
19
 
 
20
 
  # Returns a hash, that will be turned into a JSON object and represent this
21
 
  # object.
22
 
  def as_json(*)
23
 
    {
24
 
      JSON.create_id => self.class.name,
25
 
      's'            => tv_sec,
26
 
      'n'            => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
27
 
    }
28
 
  end
29
 
 
30
 
  # Stores class name (Time) with number of seconds since epoch and number of
31
 
  # microseconds for Time as JSON string
32
 
  def to_json(*args)
33
 
    as_json.to_json(*args)
34
 
  end
35
 
end