Module: StringToNewTime
- Included in:
- String
- Defined in:
- lib/ruby-rails-extensions/extensions/to_new_time.rb
Constant Summary collapse
- NEW_TIME_REGEXES =
{ # Supports: 9:00am, 12:15 pm, 10:12am hh_mm_p: /^(1[0-2]|0?[1-9]).([0-5]?[0-9])(.?[AaPp][Mm])$/i, # Supports: 16:0, 23:23, 01:00 hh_mm: /^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$/i, # Supports: 1p, 12a, 5a, 9p h_p: /^[0-1][0-9]?(.?[AaPp][Mm]?)?$/i }.freeze
Instance Method Summary collapse
-
#to_new_time(options = {}) ⇒ Date, ...
Converts a string to a date object when following one of the following patterns (no space delimiters): - HH:MM AM/PM - HH:MM (24 hour) - HH AM/PM.
Instance Method Details
#to_new_time(options = {}) ⇒ Date, ...
Converts a string to a date object when following one of the following patterns (no space delimiters):
- HH:MM AM/PM
- HH:MM (24 hour)
- HH AM/PM
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruby-rails-extensions/extensions/to_new_time.rb', line 32 def to_new_time( = {}) opts = || {} today = Date.today year = opts.fetch(:year, today.year) month = opts.fetch(:month, today.month) day = opts.fetch(:day, today.day) parts = extract_time Time.use_zone(RubyRailsExtensions.configuration.default_time_zone || 'Eastern Time (US & Canada)') do # Use {Time#parse} as there is no `Time.zone.new` method Time.zone.parse("#{year}-#{month}-#{day} #{parts[:hour]}:#{parts[:minute]} #{parts[:ampm]}") end end |