Class: TheFox::Timr::Helper::DateTimeHelper

Inherits:
Object
  • Object
show all
Extended by:
Error
Defined in:
lib/timr/helper/datetime_helper.rb

Overview

Class Method Summary collapse

Class Method Details

.convert_date(date) ⇒ Object

Convert String to Date.



18
19
20
21
22
23
24
25
# 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) ⇒ Object

Convert String to Time.



28
29
30
31
32
33
34
35
# 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) ⇒ Object

Create a Time instance from a Hash.

Options:

  • ‘:date` String

  • ‘:time` String



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object

Parse an argv Array.

Create a ‘from` and `to` for a day.



40
41
42
43
44
45
# 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) ⇒ Object

Parse an argv Array.

Create a ‘from` and `to` for a month.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object

Parse an argv Array.

Create a ‘from` and `to` for a year.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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