Class: CoreLibrary::DateTimeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core/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:

  • date_time (String)

    The rfc1123 formatted datetime string.

Returns:

  • (DateTime)

    A DateTime object.



103
104
105
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 103

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:

  • date_time (String)

    The rfc3339 formatted datetime string.

Returns:

  • (DateTime)

    A DateTime object.



117
118
119
120
121
122
123
124
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 117

def self.from_rfc3339(date_time)
  # missing timezone information
  if date_time.match?(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[-+]\d{2}:\d{2})\z$/)
    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:

  • date_time (String)

    The unix formatted datetime string.

Returns:

  • (DateTime)

    A DateTime object.



110
111
112
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 110

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

.rfc_1123?(datetime_value) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 152

def self.rfc_1123?(datetime_value)
  DateTime.strptime(datetime_value, '%a, %d %b %Y %H:%M:%S %Z')
  true
rescue ArgumentError, TypeError
  false
end

.rfc_3339?(datetime_value) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 159

def self.rfc_3339?(datetime_value)
  DateTime.strptime(datetime_value, '%Y-%m-%dT%H:%M:%S')
  true
rescue ArgumentError, TypeError
  false
end

.to_rfc1123(date_time) ⇒ String

Safely converts a DateTime object into a rfc1123 format string.

Parameters:

  • date_time (DateTime)

    The DateTime object.

Returns:

  • (String)

    The rfc1123 formatted datetime string.



7
8
9
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 7

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:

  • date_time (Array)

    An array of DateTime objects.

Returns:

  • (Array)

    An array of rfc1123 formatted datetime string.



28
29
30
31
32
33
34
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 28

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

  hash[key] = date_time.map do |v|
    v.instance_of?(DateTime) ? DateTimeHelper.to_rfc1123(v) : v
  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:

  • date_time (hash)

    A map of DateTime objects.

Returns:

  • (hash)

    A map of rfc1123 formatted datetime string.



14
15
16
17
18
19
20
21
22
23
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 14

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] =
      v.instance_of?(DateTime) ? DateTimeHelper.to_rfc1123(v) : v
  end
  hash[key]
end

.to_rfc3339(date_time) ⇒ String

Safely converts a DateTime object into a rfc3339 format string.

Parameters:

  • date_time (DateTime)

    The DateTime object.

Returns:

  • (String)

    The rfc3339 formatted datetime string.



71
72
73
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 71

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:

  • date_time (Array)

    An array of DateTime objects.

Returns:

  • (Array)

    An array of rfc1123 formatted datetime string.



92
93
94
95
96
97
98
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 92

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

  hash[key] = date_time.map do |v|
    v.instance_of?(DateTime) ? DateTimeHelper.to_rfc3339(v) : v
  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:

  • date_time (hash)

    A map of DateTime objects.

Returns:

  • (hash)

    A map of rfc1123 formatted datetime string.



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

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] =
      v.instance_of?(DateTime) ? DateTimeHelper.to_rfc3339(v) : v
  end
  hash[key]
end

.to_unix(date_time) ⇒ Integer

Safely converts a DateTime object into a unix format string.

Parameters:

  • date_time (DateTime)

    The DateTime object.

Returns:

  • (Integer)

    The unix formatted datetime integer.



39
40
41
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 39

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:

  • date_time (hash)

    An array of DateTime objects.

Returns:

  • (hash)

    An array of unix formatted datetime string.



60
61
62
63
64
65
66
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 60

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

  hash[key] = date_time.map do |v|
    v.instance_of?(DateTime) ? DateTimeHelper.to_unix(v) : v
  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:

  • date_time (hash)

    A map of DateTime objects.

Returns:

  • (hash)

    A map of unix formatted datetime string.



46
47
48
49
50
51
52
53
54
55
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 46

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] =
      v.instance_of?(DateTime) ? DateTimeHelper.to_unix(v) : v
  end
  hash[key]
end

.unix_timestamp?(timestamp) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 166

def self.unix_timestamp?(timestamp)
  Time.at(Float(timestamp))
  true
rescue ArgumentError, TypeError
  false
end

.valid_date?(date_value) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 139

def self.valid_date?(date_value)
  if date_value.instance_of?(Date)
    true
  elsif date_value.instance_of?(String) && date_value.match?(/^\d{4}-\d{2}-\d{2}$/)
    DateTime.strptime(date_value, '%Y-%m-%d')
    true
  else
    false
  end
rescue ArgumentError
  false
end

.valid_datetime?(dt_format, dt) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/apimatic-core/utilities/date_time_helper.rb', line 126

def self.valid_datetime?(dt_format, dt)
  case dt_format
  when DateTimeFormat::HTTP_DATE_TIME
    return DateTimeHelper.rfc_1123?(dt)
  when DateTimeFormat::RFC3339_DATE_TIME
    return DateTimeHelper.rfc_3339?(dt)
  when DateTimeFormat::UNIX_DATE_TIME
    return DateTimeHelper.unix_timestamp?(dt)
  end

  false
end