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.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:

  • 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

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

.to_unix(date_time) ⇒ String

Safely converts a DateTime object into a unix format string.

Parameters:

  • date_time (DateTime)

    The DateTime object.

Returns:

  • (String)

    The unix formatted datetime string.



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