Class: Jamm::TimeUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/jamm/time_util.rb

Class Method Summary collapse

Class Method Details

.iso_format?(string) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/jamm/time_util.rb', line 33

def self.iso_format?(string)
  # Check if the string matches the ISO 8601 datetime format

  !!Time.iso8601(string)
rescue StandardError
  false
end

.relative_time_to_iso(relative_time) ⇒ Object



4
5
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
# File 'lib/jamm/time_util.rb', line 4

def self.relative_time_to_iso(relative_time)
  # Check if the input string is in ISO format
  return relative_time if iso_format?(relative_time)

  # Parse the relative time string
  quantity, unit = relative_time.split
  quantity = quantity.to_i

  # Calculate the time delta
  case unit.downcase
  when 'second', 'seconds'
    time_delta = quantity
  when 'minute', 'minutes'
    time_delta = quantity * 60
  when 'hour', 'hours'
    time_delta = quantity * 60 * 60
  when 'day', 'days'
    time_delta = quantity * 60 * 60 * 24
  when 'week', 'weeks'
    time_delta = quantity * 60 * 60 * 24 * 7
  else
    raise ArgumentError, "Unknown time unit: #{unit}"
  end

  # Calculate the ISO datetime string
  iso_time = Time.now + time_delta
  iso_time.strftime('%Y-%m-%dT%H:%M:%SZ')
end