Class: Square::DateTimeHelper
- Inherits:
-
Object
- Object
- Square::DateTimeHelper
- Defined in:
- lib/square/utilities/date_time_helper.rb
Overview
A utility that supports dateTime conversion to different formats
Class Method Summary collapse
-
.from_rfc1123(date_time) ⇒ DateTime
Safely converts a rfc1123 format string into a DateTime object.
-
.from_rfc3339(date_time) ⇒ DateTime
Safely converts a rfc3339 format string into a DateTime object.
-
.from_unix(date_time) ⇒ DateTime
Safely converts a unix format string into a DateTime object.
-
.to_rfc1123(date_time) ⇒ String
Safely converts a DateTime object into a rfc1123 format string.
-
.to_rfc1123_array(date_time, hash, key) ⇒ Array
Safely converts an array of DateTime objects into an array of rfc1123 format string.
-
.to_rfc1123_map(date_time, hash, key) ⇒ hash
Safely converts a map of DateTime objects into a map of rfc1123 format string.
-
.to_rfc3339(date_time) ⇒ String
Safely converts a DateTime object into a rfc3339 format string.
-
.to_rfc3339_array(date_time, hash, key) ⇒ Array
Safely converts an array of DateTime objects into an array of rfc1123 format string.
-
.to_rfc3339_map(date_time, hash, key) ⇒ hash
Safely converts a map of DateTime objects into a map of rfc1123 format string.
-
.to_unix(date_time) ⇒ String
Safely converts a DateTime object into a unix format string.
-
.to_unix_array(date_time, hash, key) ⇒ hash
Safely converts an array of DateTime objects into a map of unix format string.
-
.to_unix_map(date_time, hash, key) ⇒ hash
Safely converts a map of DateTime objects into a map of unix format string.
Class Method Details
.from_rfc1123(date_time) ⇒ DateTime
Safely converts a rfc1123 format string into a DateTime object
128 129 130 |
# File 'lib/square/utilities/date_time_helper.rb', line 128 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
142 143 144 145 146 147 148 149 |
# File 'lib/square/utilities/date_time_helper.rb', line 142 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
135 136 137 |
# File 'lib/square/utilities/date_time_helper.rb', line 135 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
8 9 10 |
# File 'lib/square/utilities/date_time_helper.rb', line 8 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
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/square/utilities/date_time_helper.rb', line 33 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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/square/utilities/date_time_helper.rb', line 15 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
88 89 90 |
# File 'lib/square/utilities/date_time_helper.rb', line 88 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
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/square/utilities/date_time_helper.rb', line 113 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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/square/utilities/date_time_helper.rb', line 95 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
48 49 50 |
# File 'lib/square/utilities/date_time_helper.rb', line 48 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
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/square/utilities/date_time_helper.rb', line 73 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/square/utilities/date_time_helper.rb', line 55 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 |