Module: Timeliness::Parser

Defined in:
lib/timeliness/parser.rb

Defined Under Namespace

Classes: MissingTimezoneSupport

Class Method Summary collapse

Class Method Details

._parse(string, type = nil, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/timeliness/parser.rb', line 41

def _parse(string, type=nil, options={})
  if options[:strict] && type
    Definitions.send("#{type}_format_set").match(string, options[:format])
  else
    values = nil
    Definitions.format_sets(type, string).any? { |set| values = set.match(string, options[:format]) }
    values
  end
rescue
  nil
end

.make_time(time_array, zone_option = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/timeliness/parser.rb', line 27

def make_time(time_array, zone_option=nil)
  return nil unless fast_date_valid_with_fallback(*time_array[0..2])

  zone, offset = zone_and_offset(time_array[7]) if time_array[7]

  value = create_time_in_zone(time_array[0..6].compact, zone || zone_option)
  value = shift_time_to_zone(value, zone_option) if zone
  return nil unless value

  offset ? value + (value.utc_offset - offset) : value
rescue ArgumentError, TypeError
  nil
end

.parse(value, type = nil, **options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/timeliness/parser.rb', line 7

def parse(value, type=nil, **options)
  return value if acts_like_temporal?(value)
  return nil unless parseable?(value)

  if type && !type.is_a?(Symbol)
    options[:now] = type if type
    type = nil
  end

  time_array = _parse(value, type, options)
  return nil if time_array.nil?

  default_values_by_type(time_array, type, options) unless type == :datetime

  make_time(time_array[0..7], options[:zone])
rescue NoMethodError => ex
  raise ex unless ex.message =~ /undefined method `(zone|use_zone|current)' for Time:Class/
  raise MissingTimezoneSupport, "ActiveSupport timezone support must be loaded to use timezones other than :utc and :local."
end