Class: Longleaf::ServiceDateHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/helpers/service_date_helper.rb

Overview

Helper methods for interacting with dates/timestamps on services

Class Method Summary collapse

Class Method Details

.add_to_timestamp(timestamp, modifier) ⇒ String

Adds the amount of time from modifier to the provided timestamp “<quantity> <time unit>”, where quantity must be a positive whole number and time unit must be second, minute, hour, day, week, month or year (unit may be plural). Any info after a comma will be ignored.



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
# File 'lib/longleaf/helpers/service_date_helper.rb', line 13

def self.add_to_timestamp(timestamp, modifier)
  if modifier =~ /^(\d+) *(second|minute|hour|day|week|month|year)s?(,.*)?/
    value = $1.to_i
    unit = $2
  else
    raise ArgumentError.new("Cannot parse time modifier #{modifier}")
  end

  datetime = Time.iso8601(timestamp)
  case unit
  when 'second'
    unit_modifier = 1
  when 'minute'
    unit_modifier = 60
  when 'hour'
    unit_modifier = 3600
  when 'day'
    unit_modifier = 24 * 3600
  when 'week'
    unit_modifier = 7 * 24 * 3600
  when 'month'
    unit_modifier = 30 * 24 * 3600
  when 'year'
    unit_modifier = 365 * 24 * 3600
  end

  modified_time = datetime + (value * unit_modifier)
  modified_time.iso8601
end

.formatted_timestamp(timestamp = Time.now) ⇒ String

Get a timestamp in the format expected for service timestamps.



46
47
48
# File 'lib/longleaf/helpers/service_date_helper.rb', line 46

def self.formatted_timestamp(timestamp = Time.now)
  timestamp.utc.iso8601(3).to_s
end