Class: MotionRecord::Serialization::TimeSerializer
- Inherits:
-
BaseSerializer
- Object
- BaseSerializer
- MotionRecord::Serialization::TimeSerializer
- Defined in:
- lib/motion_record/serialization/time_serializer.rb
Constant Summary collapse
- ISO8601_PATTERN =
Pattern stolen from Ruby Time’s xmlschema method
/\A\s* (-?\d+)-(\d\d)-(\d\d) T (\d\d):(\d\d):(\d\d) (\.\d+)? (Z|[+-]\d\d:\d\d)? \s*\z/ix
Class Method Summary collapse
-
.time_from_iso8601(time_str) ⇒ Object
Parse an ISO8601 format time string.
-
.time_to_iso8601(time) ⇒ Object
Convert a Time object to an ISO8601 format time string.
Instance Method Summary collapse
Methods inherited from BaseSerializer
Constructor Details
This class inherits a constructor from MotionRecord::Serialization::BaseSerializer
Class Method Details
.time_from_iso8601(time_str) ⇒ Object
Parse an ISO8601 format time string
time_str - the String time representation in ISO8601 format
Returns a Time object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/motion_record/serialization/time_serializer.rb', line 62 def self.time_from_iso8601(time_str) # Logic stolen from Ruby Time's xmlschema method if (match = ISO8601_PATTERN.match(time_str)) year = match[1].to_i mon = match[2].to_i day = match[3].to_i hour = match[4].to_i min = match[5].to_i sec = match[6].to_i # usec = (match[7] || 0).to_i # microsecond values are discarded zone = match[8] if zone == "Z" Time.utc(year, mon, day, hour, min, sec) elsif zone Time.new(year, mon, day, hour, min, sec, zone) end else raise ArgumentError.new("invalid date: #{time_str.inspect}") end end |
.time_to_iso8601(time) ⇒ Object
Convert a Time object to an ISO8601 format time string
time - the Time to convert
Returns the String representation
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/motion_record/serialization/time_serializer.rb', line 41 def self.time_to_iso8601(time) if time.utc_offset == 0 zone = "Z" else offset_hours = time.utc_offset / 3600 offset_minutes = (time.utc_offset - (offset_hours * 3600)) / 60 zone = "%+03d:%02d" % [offset_hours, offset_minutes] end if time.usec != 0 "%04d-%02d-%02dT%02d:%02d:%02d.%03d%s" % [time.year, time.month, time.day, time.hour, time.min, time.sec, time.usec, zone] else "%04d-%02d-%02dT%02d:%02d:%02d:%s" % [time.year, time.month, time.day, time.hour, time.min, time.sec, zone] end end |
Instance Method Details
#deserialize(value) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/motion_record/serialization/time_serializer.rb', line 25 def deserialize(value) case @column.type when :integer, :float Time.at(value) when :text self.class.time_from_iso8601(value) else raise "Can't deserialize #{value.inspect} from #{@column.type.inspect}" end end |
#serialize(value) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/motion_record/serialization/time_serializer.rb', line 14 def serialize(value) case @column.type when :integer, :float value.to_i when :text self.class.time_to_iso8601(value) else raise "Can't serialize #{value.inspect} to #{@column.type.inspect}" end end |