Class: ROC::Time

Inherits:
Base
  • Object
show all
Includes:
ROC::Types::ScalarType
Defined in:
lib/roc/objects/time.rb

Direct Known Subclasses

Lock

Instance Attribute Summary

Attributes inherited from Base

#key, #options

Instance Method Summary collapse

Methods included from ROC::Types::ScalarType

#clobber, #inspect, #setex

Methods included from ROC::Types::MethodGenerators

#deserializing_method, #nonserializing_method, #serializing_and_deserializing_method, #serializing_method, #zero_arg_method

Methods inherited from Base

#clobber, delegate_methods, #initialize, #method_missing, #respond_to?, #seed

Methods included from ROC::Types::AllTypes

#eval

Constructor Details

This class inherits a constructor from ROC::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ROC::Base

Instance Method Details

#deserialize(val) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/roc/objects/time.rb', line 42

def deserialize(val)
  if val.nil?
    nil
  else
    parts = val.split('.')
    t = if ::Time.now.respond_to?(:nsec)
          ::Time.at(parts[0].to_i, (parts[1].to_i / 1000))
        else
          ::Time.at(parts[0].to_i, parts[1].to_i)
        end
    if defined?(@offset)
      if ::Time.now.method(:localtime).arity > 0
        t.localtime(@offset)
      end
    end
    t
  end
end

#localtime(offset = nil) ⇒ Object

implement (if posible) destructive methods that would otherwise raise



23
24
25
26
27
28
29
30
# File 'lib/roc/objects/time.rb', line 23

def localtime(offset=nil)
  if (0 == ::Time.now.method(:localtime).arity) && !offset.nil?
    raise ArgumentError, "1.8 Time#localtime doesn't take an arg"
  else
    @offset = offset
  end
  self
end

#serialize(val) ⇒ Object

implementing scalar type required methods ##



34
35
36
37
38
39
40
# File 'lib/roc/objects/time.rb', line 34

def serialize(val)
  if ::Time.now.respond_to?(:nsec)
    val.to_i.to_s + '.' + val.nsec.to_s ##strait to_f loses precision
  else
    val.to_i.to_s + '.' + val.usec.to_s ##strait to_f loses precision
  end
end

#to_sObject



17
18
19
# File 'lib/roc/objects/time.rb', line 17

def to_s
  self.to_time.to_s
end

#to_timeObject



8
9
10
11
12
13
14
15
# File 'lib/roc/objects/time.rb', line 8

def to_time
  v = self.value
  if v.nil?
    ::Time.at(0)
  else
    v
  end
end