1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
require 'mail/fields/named_structured_field'
require 'mail/elements/date_time_element'
require 'mail/utilities'
module Mail
class CommonDateField < NamedStructuredField #:nodoc:
def self.singular?
true
end
def self.normalize_datetime(string)
if Utilities.blank?(string)
datetime = ::DateTime.now
else
stripped = string.to_s.gsub(/\(.*?\)/, '').squeeze(' ')
begin
datetime = ::DateTime.parse(stripped)
rescue ArgumentError => e
raise unless 'invalid date' == e.message
end
end
if datetime
datetime.strftime('%a, %d %b %Y %H:%M:%S %z')
else
string
end
end
def initialize(value = nil, charset = nil)
super self.class.normalize_datetime(value), charset
end
# Returns a date time object of the parsed date
def date_time
::DateTime.parse("#{element.date_string} #{element.time_string}")
rescue ArgumentError => e
raise e unless e.message == 'invalid date'
end
def default
date_time
end
def element
@element ||= Mail::DateTimeElement.new(value)
end
private
def do_encode
"#{name}: #{value}\r\n"
end
def do_decode
value.to_s
end
end
end
|