Class: ApimaticCalculator::DateTimeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic_calculator/utilities/date_time_helper.rb

Overview

A utility that supports dateTime conversion to different formats

Class Method Summary collapse

Class Method Details

.from_rfc1123(date_time) ⇒ DateTime

Safely converts a rfc1123 format string into a DateTime object

Parameters:

  • The (String)

    rfc1123 formatted datetime string

Returns:

  • (DateTime)

    A DateTime object



133
134
135
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 133

def self.from_rfc1123(date_time)
  DateTime.httpdate(date_time)
end

.from_rfc3339(date_time) ⇒ DateTime

Safely converts a rfc3339 format string into a DateTime object

Parameters:

  • The (String)

    rfc3339 formatted datetime string

Returns:

  • (DateTime)

    A DateTime object



147
148
149
150
151
152
153
154
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 147

def self.from_rfc3339(date_time)
  # missing timezone information
  if date_time.end_with?('Z') || date_time.index('+')
    DateTime.rfc3339(date_time)
  else
    DateTime.rfc3339("#{date_time}Z")
  end
end

.from_unix(date_time) ⇒ DateTime

Safely converts a unix format string into a DateTime object

Parameters:

  • The (String)

    unix formatted datetime string

Returns:

  • (DateTime)

    A DateTime object



140
141
142
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 140

def self.from_unix(date_time)
  Time.at(date_time.to_i).utc.to_datetime
end

.to_rfc1123(date_time) ⇒ String

Safely converts a DateTime object into a rfc1123 format string

Parameters:

  • The (DateTime)

    DateTime object

Returns:

  • (String)

    The rfc1123 formatted datetime string



13
14
15
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 13

def self.to_rfc1123(date_time)
  date_time&.httpdate
end

.to_rfc1123_array(date_time, hash, key) ⇒ Array

Safely converts an array of DateTime objects into an array of rfc1123 format string

Parameters:

  • an (Array)

    array of DateTime objects

Returns:

  • (Array)

    an array of rfc1123 formatted datetime string



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 38

def self.to_rfc1123_array(date_time, hash, key)
  return if date_time.nil?

  hash[key] = date_time.map do |v|
    if v.is_a?(BaseModel)
      v.to_hash
    else
      v.is_a?(DateTime) ? DateTimeHelper.to_rfc1123(v) : v
    end
  end
end

.to_rfc1123_map(date_time, hash, key) ⇒ hash

Safely converts a map of DateTime objects into a map of rfc1123 format string

Parameters:

  • a (hash)

    map of DateTime objects

Returns:

  • (hash)

    a map of rfc1123 formatted datetime string



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 20

def self.to_rfc1123_map(date_time, hash, key)
  return if date_time.nil?

  hash[key] = {}
  date_time.each do |k, v|
    hash[key][k] =
      if v.is_a?(BaseModel)
        v.to_hash
      else
        v.is_a?(DateTime) ? DateTimeHelper.to_rfc1123(v) : v
      end
  end
  hash[key]
end

.to_rfc3339(date_time) ⇒ String

Safely converts a DateTime object into a rfc3339 format string

Parameters:

  • The (DateTime)

    DateTime object

Returns:

  • (String)

    The rfc3339 formatted datetime string



93
94
95
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 93

def self.to_rfc3339(date_time)
  date_time&.rfc3339
end

.to_rfc3339_array(date_time, hash, key) ⇒ Array

Safely converts an array of DateTime objects into an array of rfc1123 format string

Parameters:

  • an (Array)

    array of DateTime objects

Returns:

  • (Array)

    an array of rfc1123 formatted datetime string



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 118

def self.to_rfc3339_array(date_time, hash, key)
  return if date_time.nil?

  hash[key] = date_time.map do |v|
    if v.is_a?(BaseModel)
      v.to_hash
    else
      v.is_a?(DateTime) ? DateTimeHelper.to_rfc3339(v) : v
    end
  end
end

.to_rfc3339_map(date_time, hash, key) ⇒ hash

Safely converts a map of DateTime objects into a map of rfc1123 format string

Parameters:

  • a (hash)

    map of DateTime objects

Returns:

  • (hash)

    a map of rfc1123 formatted datetime string



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 100

def self.to_rfc3339_map(date_time, hash, key)
  return if date_time.nil?

  hash[key] = {}
  date_time.each do |k, v|
    hash[key][k] =
      if v.is_a?(BaseModel)
        v.to_hash
      else
        v.is_a?(DateTime) ? DateTimeHelper.to_rfc3339(v) : v
      end
  end
  hash[key]
end

.to_unix(date_time) ⇒ String

Safely converts a DateTime object into a unix format string

Parameters:

  • The (DateTime)

    DateTime object

Returns:

  • (String)

    The unix formatted datetime string



53
54
55
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 53

def self.to_unix(date_time)
  date_time.to_time.utc.to_i unless date_time.nil?
end

.to_unix_array(date_time, hash, key) ⇒ hash

Safely converts an array of DateTime objects into a map of unix format string

Parameters:

  • an (hash)

    array of DateTime objects

Returns:

  • (hash)

    an array of unix formatted datetime string



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 78

def self.to_unix_array(date_time, hash, key)
  return if date_time.nil?

  hash[key] = date_time.map do |v|
    if v.is_a?(BaseModel)
      v.to_hash
    else
      v.is_a?(DateTime) ? DateTimeHelper.to_unix(v) : v
    end
  end
end

.to_unix_map(date_time, hash, key) ⇒ hash

Safely converts a map of DateTime objects into a map of unix format string

Parameters:

  • a (hash)

    map of DateTime objects

Returns:

  • (hash)

    a map of unix formatted datetime string



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/apimatic_calculator/utilities/date_time_helper.rb', line 60

def self.to_unix_map(date_time, hash, key)
  return if date_time.nil?

  hash[key] = {}
  date_time.each do |k, v|
    hash[key][k] =
      if v.is_a?(BaseModel)
        v.to_hash
      else
        v.is_a?(DateTime) ? DateTimeHelper.to_unix(v) : v
      end
  end
  hash[key]
end