Module: Itsu

Defined in:
lib/itsu.rb

Overview

Simple Time checking library. The goal is to check on regularly occurring events in a business cycle, such as something that needs to happen every week, month, or quarter.

All time math is done relative to the time zone of the supplied argument.

Defined Under Namespace

Modules: Period

Constant Summary collapse

SECONDS_IN_HOUR =
3600
SECONDS_IN_DAY =
86400

Class Method Summary collapse

Class Method Details

._adjust_dst(time) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/itsu.rb', line 94

def _adjust_dst(time)
  # Internal method. Assumes midnight adjustments have already occurred.
  case time.hour
  when 0
    time
  when 1
    time - SECONDS_IN_HOUR
  when 23
    time + SECONDS_IN_HOUR
  end
end

.in_period?(time, period) ⇒ Boolean

Determine if a time falls within the current period. For example, if the period is ‘week’, then check if the date falls within the current week. This can be used to test if a occurrence has already happened this period. For weeks, consider the week to start on Monday. This is a history check. All future times will return true.

Parameters:

  • time (Time)

    Timestamp to check.

  • period (Itsu::Period)

    Period to check against.

Returns:

  • (Boolean)

    Whether the timestamp is within the most recent period.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/itsu.rb', line 81

def in_period?(time, period)
  case period
  when Period::WEEK
    time > start_of_week(Time.now)
  when Period::MONTH
    time > start_of_month(Time.now)
  when Period::QUARTER
    time > start_of_quarter(Time.now)
  else
    raise ArgumentError, "Unrecognized period, #{period}."
  end
end

.start_of_day(time) ⇒ Time

Roll back the time to the beginning of the day (midnight, 00:00:00)

Parameters:

  • time (Time)

    Timestamp

Returns:

  • (Time)

    Timestamp of the start of the day.



28
29
30
31
32
33
# File 'lib/itsu.rb', line 28

def start_of_day(time)
  day = time - time.hour * SECONDS_IN_HOUR
  day -= time.min * 60
  day -= time.sec
  _adjust_dst(day)
end

.start_of_month(time) ⇒ Time

Roll back the time to the beginning of the month.

Parameters:

  • time (Time)

    Timestamp

Returns:

  • (Time)

    Timestamp of the start of the month.



52
53
54
55
# File 'lib/itsu.rb', line 52

def start_of_month(time)
  first = time - (time.mday - 1) * SECONDS_IN_DAY
  start_of_day(first)
end

.start_of_quarter(time) ⇒ Time

Get the starting time for the first day of the quarter relative to the provided timestamp.

Parameters:

  • time (Time)

    Timestamp

Returns:

  • (Time)

    Timestamp of the start of the quarter.



63
64
65
66
67
68
# File 'lib/itsu.rb', line 63

def start_of_quarter(time)
  back = (time.mon - 1) % 3
  qtr = time
  (1..back).each { qtr -= qtr.mday * SECONDS_IN_DAY }
  start_of_month(qtr)
end

.start_of_week(time) ⇒ Time

Get the starting time for the first day of the week (Monday) relative to the provided timestamp. If the timestamp is on Monday, will return the start of the same day.

Parameters:

  • time (Time)

    Timestamp

Returns:

  • (Time)

    Timestamp of the previous Monday.



42
43
44
45
# File 'lib/itsu.rb', line 42

def start_of_week(time)
  mon = time - ((time.wday + 6) % 7 * SECONDS_IN_DAY)
  start_of_day(mon)
end