lib/timeliness/parser.rb in timeliness-0.3.7 vs lib/timeliness/parser.rb in timeliness-0.3.8

- old
+ new

@@ -3,11 +3,12 @@ class MissingTimezoneSupport < StandardError; end class << self def parse(value, *args) - return value unless value.is_a?(String) + return value if acts_like_temporal?(value) + return nil unless parseable?(value) type, options = type_and_options_from_args(args) time_array = _parse(value, type, options) return nil if time_array.nil? @@ -45,10 +46,18 @@ nil end private + def parseable?(value) + value.is_a?(String) + end + + def acts_like_temporal?(value) + value.is_a?(Time) || value.is_a?(Date) || value.respond_to?(:acts_like_date?) || value.respond_to?(:acts_like_time?) + end + def type_and_options_from_args(args) options = args.last.is_a?(Hash) ? args.pop : {} type_or_now = args.first if type_or_now.is_a?(Symbol) type = type_or_now @@ -127,10 +136,10 @@ return zone, offset end # Taken from ActiveSupport and simplified def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0) - return nil if hour > 23 || min > 59 || sec > 59 + return nil if hour > 23 || min > 59 || sec > 59 ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec) rescue offset = utc_or_local == :local ? (::Time.local(2007).utc_offset.to_r/86400) : 0 ::DateTime.civil(year, month, day, hour, min, sec, offset) end