Module: SimpleMapper::Attributes::Types::TimestampHighRes
- Defined in:
- lib/simple_mapper/attributes/types.rb
Constant Summary collapse
- SECOND_FRACTION =
Rational(1, 24 * 60 * 60)
- PATTERN =
/^([^.]+)\.(\d+)([-+]\d{4})$/
- OUT_FORMAT =
'%Y-%m-%d %H:%M:%S.%N%z'
- IN_FORMAT =
'%Y-%m-%d %H:%M:%S%z'
Class Method Summary collapse
Class Method Details
.decode(value) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/simple_mapper/attributes/types.rb', line 207 def self.decode(value) if value.kind_of?(DateTime) value elsif value.nil? nil else if match = PATTERN.match(value.to_s) stamp, second_fraction, zone = match.captures subseconds = BigDecimal('0.' + second_fraction).to_r DateTime.strptime(stamp + zone, IN_FORMAT) + (subseconds * SECOND_FRACTION) else raise SimpleMapper::TypeConversionException, "cannot transform '#{value}' into hi-res DateTime" end end end |
.encode(value) ⇒ Object
202 203 204 205 |
# File 'lib/simple_mapper/attributes/types.rb', line 202 def self.encode(value) return nil if value.nil? value.strftime(OUT_FORMAT) end |