Module: Barometer::Utils::Time

Defined in:
lib/barometer/utils/time.rb

Class Method Summary collapse

Class Method Details

.add_one_day(time) ⇒ Object



51
52
53
54
55
# File 'lib/barometer/utils/time.rb', line 51

def self.add_one_day(time)
  return unless time
  one_day_minus_one_second = (60 * 60 * 24 - 1)
  time + one_day_minus_one_second
end

.add_one_hour(time) ⇒ Object



57
58
59
60
61
# File 'lib/barometer/utils/time.rb', line 57

def self.add_one_hour(time)
  return unless time
  one_hour = (60 * 60 * 1)
  time + one_hour
end

.end_of_day(time) ⇒ Object



70
71
72
73
74
75
# File 'lib/barometer/utils/time.rb', line 70

def self.end_of_day(time)
  ::Time.utc(
    time.year, time.month, time.day,
    23, 59, 59
  )
end

.parse(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/barometer/utils/time.rb', line 4

def self.parse(*args)
  return unless args.compact.size > 0
  first_arg = args.first

  if first_arg.is_a? ::Time
    first_arg
  elsif first_arg.is_a?(::DateTime) || first_arg.is_a?(::Date)
    ::Time.parse(first_arg.to_s)
  elsif args.size == 1 || args.size == 2
    strptime(*args)
  else
    ::Time.utc(*args)
  end
end

.start_of_day(time) ⇒ Object



63
64
65
66
67
68
# File 'lib/barometer/utils/time.rb', line 63

def self.start_of_day(time)
  ::Time.utc(
    time.year, time.month, time.day,
    0, 0, 0
  )
end

.strftime(time) ⇒ Object



19
20
21
# File 'lib/barometer/utils/time.rb', line 19

def self.strftime(time)
  time.strftime("%Y-%m-%d %H:%M:%S %z")
end

.strptime(str, format = nil) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/barometer/utils/time.rb', line 23

def self.strptime(str, format=nil)
  dt = if format
    ::DateTime.strptime(str, format)
  else
    ::DateTime.parse(str)
  end
  ::Time.utc(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec) - (dt.zone.to_f * 60 * 60)
end

.utc_from_base_plus_local_time(tz, base, hour, min) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/barometer/utils/time.rb', line 32

def self.utc_from_base_plus_local_time(tz, base, hour, min)
  return unless tz && base
  local_base = tz.utc_to_local(base.utc)

  local_time = ::Time.utc(local_base.year, local_base.month, local_base.day, hour, min, 0)
  tz.local_to_utc(local_time)
end

.utc_merge_base_plus_time(base_time = nil, time = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/barometer/utils/time.rb', line 40

def self.utc_merge_base_plus_time(base_time=nil, time=nil)
  return unless base_time && time
  base_time_utc = base_time.utc
  time_utc = time.utc

  ::Time.utc(
    base_time_utc.year, base_time_utc.month, base_time_utc.day,
    time_utc.hour, time_utc.min, 0
  )
end