2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'chronic'
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
module Format
|
|
|
|
module Date
|
|
|
|
ISORegex = /\d\d\d\d-\d\d-\d\d/.freeze
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.parse(str)
|
|
|
|
Chronic.parse(str)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.from(str)
|
|
|
|
DateTime.strptime(str, '%m/%d/%Y')
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.to_readable(date)
|
|
|
|
date.strftime("%A, %B #{date.day.ordinalize}")
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.full(date, timezone = nil)
|
|
|
|
return '' if date.nil?
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
date = Chronic.parse(date) if date.is_a?(String)
|
|
|
|
date = date.in_time_zone(timezone) if timezone
|
|
|
|
date.strftime('%m/%-d/%Y %l:%M%P')
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.full_range(date1, date2, timezone = nil)
|
|
|
|
return full(date1, timezone) if date2.nil?
|
|
|
|
return full(date2, timezone) if date1.nil?
|
|
|
|
if simple(date1) == simple(date2)
|
|
|
|
return full(date1, timezone) + ' - ' + time(date2, timezone)
|
|
|
|
else
|
|
|
|
return full(date1, timezone) + ' - ' + full(date2, timezone)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.simple(date, timezone = nil)
|
|
|
|
return '' if date.nil?
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
date = Chronic.parse(date) if date.is_a?(String)
|
|
|
|
date = date.in_time_zone(timezone) if timezone
|
|
|
|
date.strftime('%m/%d/%Y')
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.time(datetime, timezone = nil)
|
|
|
|
return '' if datetime.nil?
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
datetime = Chronic.parse(datetime) if datetime.is_a?(String)
|
|
|
|
datetime = datetime.in_time_zone(timezone) if timezone
|
|
|
|
datetime.strftime('%l:%M%P')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.us_timezones
|
|
|
|
# zones=ActiveSupport::TimeZone.us_zones
|
|
|
|
zones = ActiveSupport::TimeZone.all
|
|
|
|
names = zones.map(&:name)
|
|
|
|
vals = zones.map { |t| t.tzinfo.name }
|
|
|
|
names.zip(vals).sort_by { |name, _val| name }
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.parse_partial_str(str)
|
|
|
|
return nil if str.nil?
|
|
|
|
|
|
|
|
Time.new(*str.match(/(\d\d\d\d)-?(\d\d)?-?(\d\d)?/).to_a[1..-1].compact.map(&:to_i))
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end; end
|