Module: HMachine::Pattern::DateTime

Extended by:
HMachine
Defined in:
lib/hmachine/pattern/datetime.rb

Constant Summary

Constants included from HMachine

HMachine::PRODID, VERSION

Class Method Summary collapse

Methods included from HMachine

extract, extract_from, find, find_in, found_in?, get, get_document, get_url, map, normalize, parse, parse_first, search, valid?, validate

Class Method Details

.date(datestring) ⇒ Object

Normalize ISO8601 Dates



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hmachine/pattern/datetime.rb', line 19

def self.date(datestring)
  datetime = Date._parse(datestring)
  if !datetime.empty? && datetime[:year] && (datetime[:mon] || datetime[:yday])
    local = Time.now
    year = datetime[:year] || local.year
    if datetime[:yday]
      ordinal = Date.ordinal(year, datetime[:yday]) rescue nil
      if ordinal
        month = ordinal.month
        day = ordinal.day
      end
    else
      month = datetime[:mon] || local.month
      day = datetime[:mday] || 1
    end
    "#{year}-#{month}-#{day}" if (month && day)
  end
end

.date?(string) ⇒ Boolean

Is this string a simple date?

Returns:

  • (Boolean)


9
10
11
# File 'lib/hmachine/pattern/datetime.rb', line 9

def self.date?(string)
  !date(string).nil?
end

.iso8601(datetime) ⇒ Object

Build a normalized iso8601 datetime string



52
53
54
55
56
# File 'lib/hmachine/pattern/datetime.rb', line 52

def self.iso8601(datetime)
  datestamp = date(datetime) || ''
  timestamp = time(datetime) || ''
  datestamp + timestamp
end

.time(timestring) ⇒ Object

Normalize ISO8601 Times



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hmachine/pattern/datetime.rb', line 39

def self.time(timestring)
  datetime = Date._parse(timestring)
  if !datetime.empty? && datetime[:hour]
    local = Time.now
    hour = datetime[:hour]
    min = datetime[:min] || 0
    sec = datetime[:sec] || 0
    zone = datetime[:zone] || local.utc_offset
    "T#{hour}:#{min}:#{sec}#{zone}"
  end
end

.time?(string) ⇒ Boolean

Is this string a simple time?

Returns:

  • (Boolean)


14
15
16
# File 'lib/hmachine/pattern/datetime.rb', line 14

def self.time?(string)
  !time(string).nil?
end