Class: ReeDatetime::GeneralDiff

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/general_diff.rb

Constant Summary collapse

INTERVALS =
[:years, :months, :weeks, :days, :hours, :minutes, :seconds]
SECONDS_IN_INTERVAL =
INTERVALS.zip([365 * 24 * 60 * 60, 30 * 24 * 60 * 60, 7 * 24 * 60 * 60, 24 * 60 * 60, 60 * 60, 60, 1]).to_h

Instance Method Summary collapse

Instance Method Details

#call(start_time, end_time, **opts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/general_diff.rb', line 27

def call(start_time, end_time, **opts)
  results = []
  end_time = end_time.to_time

  opts = slice(opts, [:round_mode])

  y_delta = years_diff(start_time, end_time, **opts)
  results << y_delta

  past_time = start_time.to_time + y_delta * SECONDS_IN_INTERVAL[:years]

  m_delta = end_time > past_time ? months_diff(past_time, end_time, **opts) : 0
  results << m_delta
  past_time += m_delta * SECONDS_IN_INTERVAL[:months]

  w_delta = end_time > past_time ? weeks_diff(past_time, end_time, **opts) : 0
  results << w_delta
  past_time += w_delta * SECONDS_IN_INTERVAL[:weeks]

  d_delta = end_time > past_time ? days_diff(past_time, end_time, **opts) : 0
  results << d_delta
  past_time += d_delta * SECONDS_IN_INTERVAL[:days]

  h_delta = end_time > past_time ? hours_diff(past_time, end_time, **opts) : 0
  results << h_delta
  past_time += h_delta * SECONDS_IN_INTERVAL[:hours]

  m_delta = end_time > past_time ? minutes_diff(past_time, end_time, **opts) : 0
  results << m_delta
  past_time += m_delta * SECONDS_IN_INTERVAL[:minutes]

  s_delta = end_time > past_time ? seconds_diff(past_time, end_time, **opts) : 0
  results << s_delta * SECONDS_IN_INTERVAL[:seconds]

  Hash[INTERVALS.zip(results)]
end