itsu 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/itsu.rb +105 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 18def3cf1e319629ae966bc3ae382e4a039b3d46
|
4
|
+
data.tar.gz: a9e5321576e0a64abfe35ddb7d130989ab9775d4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c48409817aa85e711f34021ac671ee44bd714f99223b5dc082f4b87aee14dd6eb8a2b8155f659cb6d8329deb061037d439d571679bc584827c0defdbe518fd0
|
7
|
+
data.tar.gz: 648312cfbca28e447df46ccc0b4c83f4653a0361d694259b30f1dfe17c7fc89bf7b444d61837935a82fc65f694d7fd5c48b34c161431bcd3e4635800dfb33691
|
data/lib/itsu.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Simple Time checking library.
|
4
|
+
# The goal is to check on regularly occurring events in a business cycle,
|
5
|
+
# such as something that needs to happen every week, month, or quarter.
|
6
|
+
#
|
7
|
+
# All time math is done relative to the time zone of the supplied argument.
|
8
|
+
#
|
9
|
+
module Itsu
|
10
|
+
module_function
|
11
|
+
|
12
|
+
# Supported period constants
|
13
|
+
#
|
14
|
+
module Period
|
15
|
+
WEEK = 'week'
|
16
|
+
MONTH = 'month'
|
17
|
+
QUARTER = 'quarter'
|
18
|
+
end
|
19
|
+
|
20
|
+
SECONDS_IN_HOUR = 3600
|
21
|
+
SECONDS_IN_DAY = 86400
|
22
|
+
|
23
|
+
# Roll back the time to the beginning of the day (midnight, 00:00:00)
|
24
|
+
#
|
25
|
+
# @param [Time] time Timestamp
|
26
|
+
# @return [Time] Timestamp of the start of the day.
|
27
|
+
#
|
28
|
+
def start_of_day(time)
|
29
|
+
day = time - time.hour * SECONDS_IN_HOUR
|
30
|
+
day -= time.min * 60
|
31
|
+
day -= time.sec
|
32
|
+
_adjust_dst(day)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Get the starting time for the first day of the week (Monday) relative to
|
36
|
+
# the provided timestamp.
|
37
|
+
# If the timestamp is on Monday, will return the start of the same day.
|
38
|
+
#
|
39
|
+
# @param [Time] time Timestamp
|
40
|
+
# @return [Time] Timestamp of the previous Monday.
|
41
|
+
#
|
42
|
+
def start_of_week(time)
|
43
|
+
mon = time - ((time.wday + 6) % 7 * SECONDS_IN_DAY)
|
44
|
+
start_of_day(mon)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Roll back the time to the beginning of the month.
|
48
|
+
#
|
49
|
+
# @param [Time] time Timestamp
|
50
|
+
# @return [Time] Timestamp of the start of the month.
|
51
|
+
#
|
52
|
+
def start_of_month(time)
|
53
|
+
first = time - (time.mday - 1) * SECONDS_IN_DAY
|
54
|
+
start_of_day(first)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Get the starting time for the first day of the quarter relative to the
|
58
|
+
# provided timestamp.
|
59
|
+
#
|
60
|
+
# @param [Time] time Timestamp
|
61
|
+
# @return [Time] Timestamp of the start of the quarter.
|
62
|
+
#
|
63
|
+
def start_of_quarter(time)
|
64
|
+
back = (time.mon - 1) % 3
|
65
|
+
qtr = time
|
66
|
+
(1..back).each { qtr -= qtr.mday * SECONDS_IN_DAY }
|
67
|
+
start_of_month(qtr)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Determine if a time falls within the current period.
|
71
|
+
# For example, if the period is 'week', then check if the date falls within
|
72
|
+
# the current week.
|
73
|
+
# This can be used to test if a occurrence has already happened this period.
|
74
|
+
# For weeks, consider the week to start on Monday.
|
75
|
+
# This is a history check. All future times will return true.
|
76
|
+
#
|
77
|
+
# @param [Time] time Timestamp to check.
|
78
|
+
# @param [Itsu::Period] period Period to check against.
|
79
|
+
# @return [Boolean] Whether the timestamp is within the most recent period.
|
80
|
+
#
|
81
|
+
def in_period?(time, period)
|
82
|
+
case period
|
83
|
+
when Period::WEEK
|
84
|
+
time > start_of_week(Time.now)
|
85
|
+
when Period::MONTH
|
86
|
+
time > start_of_month(Time.now)
|
87
|
+
when Period::QUARTER
|
88
|
+
time > start_of_quarter(Time.now)
|
89
|
+
else
|
90
|
+
raise ArgumentError, "Unrecognized period, #{period}."
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def _adjust_dst(time)
|
95
|
+
# Internal method. Assumes midnight adjustments have already occurred.
|
96
|
+
case time.hour
|
97
|
+
when 0
|
98
|
+
time
|
99
|
+
when 1
|
100
|
+
time - SECONDS_IN_HOUR
|
101
|
+
when 23
|
102
|
+
time + SECONDS_IN_HOUR
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: itsu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael S. Daines
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Simple Time library. The goal is to check on regularly occurring events
|
14
|
+
in a business cycle, such as something that needs to happen every week, month, or
|
15
|
+
quarter.
|
16
|
+
email: defndaines@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/itsu.rb
|
22
|
+
homepage: http://rubygems.org/gems/itsu
|
23
|
+
licenses:
|
24
|
+
- EPL
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.0.7
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: itsu (いつ), a simple time library.
|
46
|
+
test_files: []
|