class TheFox::Timr::Helper::DateTimeHelper
This class should NOT replace [Time](ruby-doc.org/core-2.4.1/Time.html), [Date](ruby-doc.org/stdlib-2.4.1/libdoc/date/rdoc/Date.html) or [DateTime](ruby-doc.org/stdlib-2.4.1/libdoc/date/rdoc/DateTime.html).
Public Class Methods
convert_date(date)
click to toggle source
Convert String to Date.
# File lib/timr/helper/datetime_helper.rb, line 18 def convert_date(date) case date when String Time.parse(date).utc.to_date when nil Time.now.utc.to_date end end
convert_time(time)
click to toggle source
Convert String to Time
.
# File lib/timr/helper/datetime_helper.rb, line 28 def convert_time(time) case time when String Time.parse(time).utc when nil Time.now.utc end end
get_datetime_from_options(options = Hash.new)
click to toggle source
Create a Time
instance from a Hash.
Options:
-
`:date` String
-
`:time` String
# File lib/timr/helper/datetime_helper.rb, line 98 def get_datetime_from_options(options = Hash.new) date_opt = options.fetch(:date, nil) time_opt = options.fetch(:time, nil) if date_opt && !time_opt raise DateTimeHelperError, 'You also need to set a time when giving a date.' end datetime_a = [] if date_opt datetime_a << date_opt end if time_opt datetime_a << time_opt end if datetime_a.count > 0 datetime_s = datetime_a.join(' ') Time.parse(datetime_s).utc else Time.now.utc end end
parse_day_argv(argv)
click to toggle source
Parse an argv Array.
Create a `from` and `to` for a day.
# File lib/timr/helper/datetime_helper.rb, line 40 def parse_day_argv(argv) day = argv.shift from = Time.parse("#{day} 00:00:00") to = Time.parse("#{day} 23:59:59") [from, to] end
parse_month_argv(argv)
click to toggle source
Parse an argv Array.
Create a `from` and `to` for a month.
# File lib/timr/helper/datetime_helper.rb, line 50 def parse_month_argv(argv) parts = argv.shift.split('-').map{ |s| s.to_i } if parts.count == 1 y = Date.today.year m = parts.first else y, m = parts end if y < 2000 # shit y += 2000 end start_date = Date.new(y, m, 1) end_date = Date.new(y, m, -1) from = Time.parse("#{start_date.strftime('%F')} 00:00:00") to = Time.parse("#{end_date.strftime('%F')} 23:59:59") [from, to] end
parse_year_argv(argv)
click to toggle source
Parse an argv Array.
Create a `from` and `to` for a year.
# File lib/timr/helper/datetime_helper.rb, line 73 def parse_year_argv(argv) y = argv.shift if y y = y.to_i else y = Date.today.year end if y < 2000 # shit y += 2000 end start_date = Date.new(y, 1, 1) end_date = Date.new(y, 12, -1) from = Time.parse("#{start_date.strftime('%F')} 00:00:00") to = Time.parse("#{end_date.strftime('%F')} 23:59:59") [from, to] end