Module: Datemaster
- Defined in:
- lib/datemaster.rb,
lib/datemaster/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
- .calculate_format(input) ⇒ Object
- .calculate_format_european(input) ⇒ Object
- .date_to_iso_string(input) ⇒ Object (also: datetime_to_iso_string)
- .date_to_string(input, format = "/") ⇒ Object
- .date_to_string_european_format(input, format = "/") ⇒ Object
- .datetime_to_string(input, format = "/", full = true) ⇒ Object
- .datetime_to_string_european_format(input, format = "/", full = true) ⇒ Object
- .iso_string_to_date(iso_string) ⇒ Object
- .iso_string_to_datetime(iso_string) ⇒ Object
- .string_to_date(input) ⇒ Object
- .string_to_date_european(input) ⇒ Object
- .string_to_datetime(input) ⇒ Object
- .string_to_datetime_european(input) ⇒ Object
Class Method Details
.calculate_format(input) ⇒ Object
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 32 |
# File 'lib/datemaster.rb', line 7 def self.calculate_format(input) case input.length when 8 "%m%d%Y" when 10 if input.include?('/') "%m/%d/%Y" elsif input.include?('-') "%m-%d-%Y" else raise ArgumentError, "Invalid date format" end when 6 "%m%d%y" when 8 if input.include?('/') "%m/%d/%y" elsif input.include?('-') "%m-%d-%y" else raise ArgumentError, "Invalid date format" end else raise ArgumentError, "Invalid date format" end end |
.calculate_format_european(input) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/datemaster.rb', line 34 def self.calculate_format_european(input) case input.length when 8 "%d%m%Y" when 10 if input.include?('/') "%d/%m/%Y" elsif input.include?('-') "%d-%m-%Y" else raise ArgumentError, "Invalid date format" end when 6 "%d%m%y" when 8 if input.include?('/') "%d/%m/%y" elsif input.include?('-') "%d-%m-%y" else raise ArgumentError, "Invalid date format" end else raise ArgumentError, "Invalid date format" end end |
.date_to_iso_string(input) ⇒ Object Also known as: datetime_to_iso_string
120 121 122 123 124 125 126 127 128 |
# File 'lib/datemaster.rb', line 120 def self.date_to_iso_string(input) if input.is_a?(Date) || input.is_a?(DateTime) iso_string = input.iso8601 puts iso_string return iso_string else raise ArgumentError, "Input must be a Date or DateTime object" end end |
.date_to_string(input, format = "/") ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/datemaster.rb', line 101 def self.date_to_string(input, format="/") begin case format when "/" formatted_date = "%02d/%02d/%04d" % [input.month, input.day, input.year] when "-" formatted_date = "%02d-%02d-%04d" % [input.month, input.day, input.year] when "" formatted_date = "%02d%02d%04d" % [input.month, input.day, input.year] else raise ArgumentError, "Invalid format: #{format}" end puts formatted_date return formatted_date rescue puts "Invalid date format: #{input}" end end |
.date_to_string_european_format(input, format = "/") ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/datemaster.rb', line 134 def self.date_to_string_european_format(input, format="/") begin case format when "/" formatted_date = "%02d/%02d/%04d" % [input.day, input.month, input.year] when "-" formatted_date = "%02d-%02d-%04d" % [input.day, input.month, input.year] when "" formatted_date = "%02d%02d%04d" % [input.day, input.month, input.year] else raise ArgumentError, "Invalid format: #{format}" end puts formatted_date return formatted_date rescue puts "Invalid date format: #{input}" end end |
.datetime_to_string(input, format = "/", full = true) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/datemaster.rb', line 153 def self.datetime_to_string(input, format="/", full=true) begin if full case format when "/" formatted_datetime = "%02d/%02d/%04d %02d:%02d:%02d" % [input.month, input.day, input.year, input.hour, input.min, input.sec] when "-" formatted_datetime = "%02d-%02d-%04d %02d:%02d:%02d" % [input.month, input.day, input.year, input.hour, input.min, input.sec] when "" formatted_datetime = "%02d%02d%04d%02d%02d%02d" % [input.month, input.day, input.year, input.hour, input.min, input.sec] else raise ArgumentError, "Invalid format: #{format}" end else case format when "/" formatted_datetime = "%02d/%02d/%04d" % [input.month, input.day, input.year] when "-" formatted_datetime = "%02d-%02d-%04d" % [input.month, input.day, input.year] when "" formatted_datetime = "%02d%02d%04d" % [input.month, input.day, input.year] else raise ArgumentError, "Invalid format: #{format}" end end puts formatted_datetime return formatted_datetime rescue puts "Invalid datetime format: #{input}" end end |
.datetime_to_string_european_format(input, format = "/", full = true) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/datemaster.rb', line 186 def self.datetime_to_string_european_format(input, format="/", full=true) begin if full case format when "/" formatted_datetime = "%02d/%02d/%04d %02d:%02d:%02d" % [input.day, input.month, input.year, input.hour, input.min, input.sec] when "-" formatted_datetime = "%02d-%02d-%04d %02d:%02d:%02d" % [input.day, input.month, input.year, input.hour, input.min, input.sec] when "" formatted_datetime = "%02d%02d%04d%02d%02d%02d" % [input.day, input.month, input.year, input.hour, input.min, input.sec] else raise ArgumentError, "Invalid format: #{format}" end else case format when "/" formatted_datetime = "%02d/%02d/%04d" % [input.day, input.month, input.year] when "-" formatted_datetime = "%02d-%02d-%04d" % [input.day, input.month, input.year] when "" formatted_datetime = "%02d%02d%04d" % [input.day, input.month, input.year] else raise ArgumentError, "Invalid format: #{format}" end end puts formatted_datetime return formatted_datetime rescue puts "Invalid datetime format: #{input}" end end |
.iso_string_to_date(iso_string) ⇒ Object
68 69 70 71 72 |
# File 'lib/datemaster.rb', line 68 def self.iso_string_to_date(iso_string) converted = DateTime.iso8601(iso_string).to_date puts converted return converted end |
.iso_string_to_datetime(iso_string) ⇒ Object
81 82 83 84 85 |
# File 'lib/datemaster.rb', line 81 def self.iso_string_to_datetime(iso_string) converted = DateTime.iso8601(iso_string) puts converted return converted end |
.string_to_date(input) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/datemaster.rb', line 61 def self.string_to_date(input) format = calculate_format(input) converted = Date.strptime(input, format) puts converted return converted end |
.string_to_date_european(input) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/datemaster.rb', line 87 def self.string_to_date_european(input) format = calculate_format_european(input) converted = Date.strptime(input, format) puts converted return converted end |
.string_to_datetime(input) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/datemaster.rb', line 74 def self.string_to_datetime(input) format = calculate_format(input) converted = DateTime.strptime(input, format) puts converted return converted end |
.string_to_datetime_european(input) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/datemaster.rb', line 94 def self.string_to_datetime_european(input) format = calculate_format_european(input) converted = DateTime.strptime(input, format) puts converted return converted end |