Class: XapianDb::TypeCodec::DateTimeCodec
- Inherits:
-
Object
- Object
- XapianDb::TypeCodec::DateTimeCodec
- Defined in:
- lib/type_codec.rb
Class Method Summary collapse
-
.decode(datetime_as_string) ⇒ DateTime
Decode a string to a datetime.
-
.encode(datetime) ⇒ String
Encode a datetime to a string in the format ‘yyyymmdd h:m:s+l’.
Class Method Details
.decode(datetime_as_string) ⇒ DateTime
Decode a string to a datetime
132 133 134 135 136 137 138 139 |
# File 'lib/type_codec.rb', line 132 def self.decode(datetime_as_string) return nil if datetime_as_string.nil? || datetime_as_string.strip == "" begin DateTime.parse datetime_as_string rescue ArgumentError raise ArgumentError.new "'#{datetime_as_string}' cannot be converted to a datetime" end end |
.encode(datetime) ⇒ String
Encode a datetime to a string in the format ‘yyyymmdd h:m:s+l’
120 121 122 123 124 125 126 127 |
# File 'lib/type_codec.rb', line 120 def self.encode(datetime) return nil unless datetime begin datetime.strftime "%Y%m%d %H:%M:%S+%L" rescue NoMethodError raise ArgumentError.new "#{datetime} was expected to be a datetime" end end |