Class: Metar::Data::Time

Inherits:
Base
  • Object
show all
Defined in:
lib/metar/data/time.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#raw

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, strict:, year:, month:, day:, hour:, minute:) ⇒ Time

Returns a new instance of Time.



48
49
50
51
52
53
54
55
56
# File 'lib/metar/data/time.rb', line 48

def initialize(raw, strict:, year:, month:, day:, hour:, minute:)
  @raw = raw
  @strict = strict
  @year = year
  @month = month
  @day = day
  @hour = hour
  @minute = minute
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



44
45
46
# File 'lib/metar/data/time.rb', line 44

def day
  @day
end

#hourObject (readonly)

Returns the value of attribute hour.



45
46
47
# File 'lib/metar/data/time.rb', line 45

def hour
  @hour
end

#minuteObject (readonly)

Returns the value of attribute minute.



46
47
48
# File 'lib/metar/data/time.rb', line 46

def minute
  @minute
end

#monthObject (readonly)

Returns the value of attribute month.



43
44
45
# File 'lib/metar/data/time.rb', line 43

def month
  @month
end

#strictObject (readonly)

Returns the value of attribute strict.



41
42
43
# File 'lib/metar/data/time.rb', line 41

def strict
  @strict
end

#yearObject (readonly)

Returns the value of attribute year.



42
43
44
# File 'lib/metar/data/time.rb', line 42

def year
  @year
end

Class Method Details

.parse(raw, year: nil, month: nil, strict: true) ⇒ Object



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
# File 'lib/metar/data/time.rb', line 6

def self.parse(raw, year: nil, month: nil, strict: true)
  year ||= DateTime.now.year
  month ||= DateTime.now.month

  date_matcher =
    if strict
      /^(\d{2})(\d{2})(\d{2})Z$/
    else
      /^(\d{1,2})(\d{2})(\d{2})Z$/
    end

  m1 = raw.match(date_matcher)
  if m1
    day = m1[1].to_i
    hour = m1[2].to_i
    minute = m1[3].to_i
  else
    return nil if strict

    m2 = raw.match(/^(\d{1,2})(\d{2})Z$/)
    return nil if !m2

    # The day is missing, use today's date
    day = ::Time.now.day
    hour = m2[1].to_i
    minute = m2[2].to_i
  end

  new(
    raw,
    strict: strict,
    year: year, month: month, day: day, hour: hour, minute: minute
  )
end

Instance Method Details

#valueObject



58
59
60
# File 'lib/metar/data/time.rb', line 58

def value
  ::Time.gm(year, month, day, hour, minute)
end