tempora 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/LICENSE.txt +21 -0
- data/README.md +176 -0
- data/Rakefile +8 -0
- data/lib/initialization.rb +19 -0
- data/lib/month.rb +54 -0
- data/lib/quarter.rb +52 -0
- data/lib/tempora/version.rb +5 -0
- data/lib/tempora.rb +15 -0
- data/lib/timeperiod.rb +49 -0
- data/lib/week.rb +40 -0
- data/lib/year.rb +43 -0
- data/sig/tempus.rbs +4 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0a945f2d08e09bed694a289ec99717a591e348d780329fc3d3b9e6d1c57354a8
|
4
|
+
data.tar.gz: a37eed5acf4ff7a0d4c423733b069e7afa195bf859742253c8474c47c3446d6f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5e9b967909906cd6f2511c19e455904d53d4797692f635c04a14a4b1a5fe20cd42badafe74e9fb5f0ed1f8722f85836b5424b0b71c7acf67614209fdc6c8e902
|
7
|
+
data.tar.gz: 35ef820b1e2133b6cb2af1b8d5ed80b86fd66f2fb6ef6112fd9fc44239e40c9a948bcd00208fd09776bec0b3884e8c9b98f76fea717bf0fe5a7a0a27555f8ab1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Max Power
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
# Tempora
|
2
|
+
|
3
|
+
Tempora is a lightweight Ruby library for handling months, weeks, quarters, and years with intuitive date-based calculations and range support.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
```bash
|
10
|
+
bundle add tempora
|
11
|
+
```
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
gem install tempora
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Initialization
|
22
|
+
|
23
|
+
```
|
24
|
+
year = Year.new(2025)
|
25
|
+
month = Month.new(2025, 12)
|
26
|
+
week = Week.new(2025, 52)
|
27
|
+
quarter = Quarter.new(2025, 4)
|
28
|
+
```
|
29
|
+
|
30
|
+
Initialization with a Date or Time
|
31
|
+
```
|
32
|
+
year = Year.from Time.now
|
33
|
+
month = Month.from Date.today
|
34
|
+
week = Week.from Date.new(2025,1,1)
|
35
|
+
quarter = Quarter.from Date.today
|
36
|
+
```
|
37
|
+
|
38
|
+
Convenient `.now` method (aliased as `.current`)
|
39
|
+
```
|
40
|
+
year = Year.now
|
41
|
+
month = Month.now
|
42
|
+
week = Week.now
|
43
|
+
quarter = Quarter.now
|
44
|
+
```
|
45
|
+
|
46
|
+
### Navigation
|
47
|
+
|
48
|
+
Previous/Next
|
49
|
+
```
|
50
|
+
month = Month.now
|
51
|
+
next = month.next # alias: month.succ
|
52
|
+
prev = month.prev # alias: month.pred
|
53
|
+
```
|
54
|
+
|
55
|
+
Use it in ranges
|
56
|
+
```
|
57
|
+
range = Month.new(2025, 1)..Month.new(2025, 6)
|
58
|
+
range.count # => 6
|
59
|
+
range.to_a.map(&:to_s) # => ["January 2025", "February 2025", "March 2025", "April 2025", "May 2025", "June 2025"]
|
60
|
+
```
|
61
|
+
|
62
|
+
Start date / End date
|
63
|
+
```
|
64
|
+
month = Month.now
|
65
|
+
month.start_date # alias: month.begin
|
66
|
+
month.end_date # alias: month.end
|
67
|
+
|
68
|
+
Month.now.weeks.map(&:start_date)
|
69
|
+
```
|
70
|
+
|
71
|
+
###
|
72
|
+
|
73
|
+
|
74
|
+
## 1. Generating Monthly Reports
|
75
|
+
|
76
|
+
Useful for finance, sales, employee attendance, or analytics dashboards
|
77
|
+
Example: Fetch revenue per day for a given month
|
78
|
+
|
79
|
+
```
|
80
|
+
month = Month.new(2025, 2)
|
81
|
+
sales = { "2025-02-01" => 1000, "2025-02-02" => 2000, "2025-02-10" => 500 } # Sample sales data
|
82
|
+
|
83
|
+
month.days.each do |day|
|
84
|
+
puts "#{day}: $#{sales[day.to_s] || 0}"
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
📌 Output:
|
89
|
+
```
|
90
|
+
2025-02-01: $1000
|
91
|
+
2025-02-02: $2000
|
92
|
+
2025-02-03: $0
|
93
|
+
...
|
94
|
+
2025-02-10: $500
|
95
|
+
```
|
96
|
+
|
97
|
+
## 2. Calendar & Scheduling Systems
|
98
|
+
|
99
|
+
Generate a calendar for scheduling shifts, meetings, or events
|
100
|
+
|
101
|
+
```
|
102
|
+
month = Month.from(Date.today)
|
103
|
+
puts "📅 Calendar for #{month}"
|
104
|
+
month.weeks.each { |week| puts week.to_s }
|
105
|
+
```
|
106
|
+
|
107
|
+
📌 Output:
|
108
|
+
```
|
109
|
+
📅 Calendar for February 2025
|
110
|
+
Week 5, 2025
|
111
|
+
Week 6, 2025
|
112
|
+
Week 7, 2025
|
113
|
+
Week 8, 2025
|
114
|
+
Week 9, 2025
|
115
|
+
...
|
116
|
+
```
|
117
|
+
|
118
|
+
➡️ Could be used in a Rails view to generate a full calendar!
|
119
|
+
|
120
|
+
## 3. Recurring Events & Subscriptions
|
121
|
+
|
122
|
+
Automate billing cycles, gym memberships, or SaaS renewals
|
123
|
+
|
124
|
+
```
|
125
|
+
def next_billing_cycle(start_date)
|
126
|
+
Month.from(start_date).next.start_date
|
127
|
+
end
|
128
|
+
|
129
|
+
puts next_billing_cycle(Date.today) # => 2025-03-01
|
130
|
+
```
|
131
|
+
|
132
|
+
➡️ Use this for subscription renewals, invoice due dates, etc.
|
133
|
+
|
134
|
+
## 4. Employee Shift Management
|
135
|
+
|
136
|
+
Organize employees' shifts week-by-week in a month
|
137
|
+
|
138
|
+
```
|
139
|
+
month = Month.new(2025, 2)
|
140
|
+
shifts = {
|
141
|
+
"2025-02-05" => "John",
|
142
|
+
"2025-02-10" => "Anna"
|
143
|
+
}
|
144
|
+
|
145
|
+
month.each_day do |day|
|
146
|
+
puts "#{day}: #{shifts[day.to_s] || 'No Shift'}"
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
➡️ Helps in HR systems for workforce planning.
|
151
|
+
|
152
|
+
|
153
|
+
## 5. Generating Reports for a Quarter
|
154
|
+
|
155
|
+
Use the Quarter class to analyze quarterly data
|
156
|
+
|
157
|
+
```
|
158
|
+
q = Quarter.new(2025, 1)
|
159
|
+
q.months.each { |m| puts m.to_s }
|
160
|
+
q.weeks.each { |w| puts w.to_s }
|
161
|
+
```
|
162
|
+
|
163
|
+
➡️ Use this for quarterly earnings, tax calculations, and budget planning.
|
164
|
+
|
165
|
+
|
166
|
+
## Contributing
|
167
|
+
|
168
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/max-power/tempora.
|
169
|
+
|
170
|
+
## License
|
171
|
+
|
172
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module Initialization
|
4
|
+
def now
|
5
|
+
from Time.now
|
6
|
+
end
|
7
|
+
|
8
|
+
alias_method :current, :now
|
9
|
+
|
10
|
+
def from(date)
|
11
|
+
new *initialization_parameter(date.to_date)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def initialization_parameter(date)
|
17
|
+
[date.year]
|
18
|
+
end
|
19
|
+
end
|
data/lib/month.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative 'timeperiod'
|
2
|
+
require_relative 'initialization'
|
3
|
+
require_relative 'week'
|
4
|
+
|
5
|
+
class Month
|
6
|
+
include TimePeriod
|
7
|
+
extend Initialization
|
8
|
+
|
9
|
+
def initialize(year, month)
|
10
|
+
@year = Integer(year)
|
11
|
+
@number = Integer(month).clamp(1, 12)
|
12
|
+
|
13
|
+
@start_date = Date.new(@year, @number, 1)
|
14
|
+
@end_date = Date.new(@year, @number, -1)
|
15
|
+
end
|
16
|
+
|
17
|
+
def id(seperator="-")
|
18
|
+
"#{@year}#{seperator}#{@number.to_s.rjust(2, '0')}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
"#{month_name} #{@year}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def month_name
|
26
|
+
Date::MONTHNAMES[@number]
|
27
|
+
end
|
28
|
+
|
29
|
+
def month_abbr
|
30
|
+
Date::ABBR_MONTHNAMES[@number]
|
31
|
+
end
|
32
|
+
|
33
|
+
def next
|
34
|
+
self.class.from(@start_date.next_month) # @start_date >> 1
|
35
|
+
end
|
36
|
+
|
37
|
+
def prev
|
38
|
+
self.class.from(@start_date.prev_month) # @start_date << 1
|
39
|
+
end
|
40
|
+
|
41
|
+
def weeks
|
42
|
+
Week.from(start_date)..Week.from(end_date)
|
43
|
+
end
|
44
|
+
|
45
|
+
alias_method :month, :number
|
46
|
+
alias_method :succ, :next
|
47
|
+
alias_method :pred, :prev
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def self.initialization_parameter(date)
|
52
|
+
[date.year, date.month]
|
53
|
+
end
|
54
|
+
end
|
data/lib/quarter.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative 'timeperiod'
|
2
|
+
require_relative 'initialization'
|
3
|
+
require_relative 'month'
|
4
|
+
require_relative 'week'
|
5
|
+
|
6
|
+
class Quarter
|
7
|
+
include TimePeriod
|
8
|
+
extend Initialization
|
9
|
+
|
10
|
+
def initialize(year, quarter)
|
11
|
+
@year = Integer(year)
|
12
|
+
@number = Integer(quarter).clamp(1,4)
|
13
|
+
|
14
|
+
start_month = (quarter - 1) * 3 + 1
|
15
|
+
@start_date = Date.new(year, start_month, 1)
|
16
|
+
@end_date = @start_date.next_month(3).prev_day
|
17
|
+
end
|
18
|
+
|
19
|
+
def id(seperator="-")
|
20
|
+
"#{@year}#{seperator}Q#{@number}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
"Q#{@number} #{@year}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def next
|
28
|
+
self.class.from(@start_date >> 3)
|
29
|
+
end
|
30
|
+
|
31
|
+
def prev
|
32
|
+
self.class.from(@start_date << 3)
|
33
|
+
end
|
34
|
+
|
35
|
+
alias_method :succ, :next
|
36
|
+
alias_method :pred, :prev
|
37
|
+
|
38
|
+
def months
|
39
|
+
Month.from(@start_date)..Month.from(@start_date.next_month(2))
|
40
|
+
end
|
41
|
+
|
42
|
+
def weeks
|
43
|
+
Week.from(@start_date)..Week.from(@end_date)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def self.initialization_parameter(date)
|
49
|
+
quarter = ((date.month - 1) / 3) + 1
|
50
|
+
[date.year, quarter]
|
51
|
+
end
|
52
|
+
end
|
data/lib/tempora.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "tempora/version"
|
4
|
+
require_relative "timeperiod"
|
5
|
+
require_relative "initialization"
|
6
|
+
require_relative "year"
|
7
|
+
require_relative "month"
|
8
|
+
require_relative "week"
|
9
|
+
require_relative "quarter"
|
10
|
+
|
11
|
+
|
12
|
+
module Tempora
|
13
|
+
class Error < StandardError; end
|
14
|
+
# Your code goes here...
|
15
|
+
end
|
data/lib/timeperiod.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module TimePeriod
|
4
|
+
include Comparable
|
5
|
+
|
6
|
+
attr_reader :start_date, :end_date, :year, :number
|
7
|
+
|
8
|
+
def range
|
9
|
+
@start_date..@end_date
|
10
|
+
end
|
11
|
+
|
12
|
+
def duration
|
13
|
+
(@end_date - @start_date).to_i + 1
|
14
|
+
end
|
15
|
+
|
16
|
+
def days
|
17
|
+
range.to_a
|
18
|
+
end
|
19
|
+
|
20
|
+
def each_day(&block)
|
21
|
+
range.each(&block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def contains?(date)
|
25
|
+
range.cover?(date)
|
26
|
+
end
|
27
|
+
|
28
|
+
def overlaps?(other)
|
29
|
+
range.overlaps?(other.range)
|
30
|
+
end
|
31
|
+
|
32
|
+
def intersection(other)
|
33
|
+
return nil unless overlaps?(other)
|
34
|
+
|
35
|
+
new_start = [@start_date, other.start_date].max
|
36
|
+
new_end = [@end_date, other.end_date].min
|
37
|
+
|
38
|
+
self.class.new(new_start, new_end)
|
39
|
+
end
|
40
|
+
|
41
|
+
def <=>(other)
|
42
|
+
@start_date <=> other.start_date
|
43
|
+
end
|
44
|
+
|
45
|
+
alias_method :begin, :start_date
|
46
|
+
alias_method :end, :end_date
|
47
|
+
alias_method :length, :duration
|
48
|
+
end
|
49
|
+
|
data/lib/week.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative 'timeperiod'
|
2
|
+
require_relative 'initialization'
|
3
|
+
|
4
|
+
class Week
|
5
|
+
include TimePeriod
|
6
|
+
extend Initialization
|
7
|
+
|
8
|
+
def initialize(year, week)
|
9
|
+
@year = Integer(year)
|
10
|
+
@number = Integer(week).clamp(1,52)
|
11
|
+
|
12
|
+
@start_date = Date.commercial(@year, @number, 1)
|
13
|
+
@end_date = start_date + 6
|
14
|
+
end
|
15
|
+
|
16
|
+
def id(seperator="-")
|
17
|
+
"#{@year}#{seperator}W#{format('%02d', @number)}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
"Week #{@number}, #{@year}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def next
|
25
|
+
self.class.from(@start_date + 7)
|
26
|
+
end
|
27
|
+
|
28
|
+
def prev
|
29
|
+
self.class.from(@start_date - 7)
|
30
|
+
end
|
31
|
+
|
32
|
+
alias_method :succ, :next
|
33
|
+
alias_method :pred, :prev
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def self.initialization_parameter(date)
|
38
|
+
[date.cwyear, date.cweek]
|
39
|
+
end
|
40
|
+
end
|
data/lib/year.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative 'timeperiod'
|
2
|
+
require_relative 'initialization'
|
3
|
+
require_relative 'month'
|
4
|
+
require_relative 'week'
|
5
|
+
|
6
|
+
class Year
|
7
|
+
include TimePeriod
|
8
|
+
extend Initialization
|
9
|
+
|
10
|
+
def initialize(year)
|
11
|
+
@year = Integer(year)
|
12
|
+
|
13
|
+
@start_date = Date.new(@year, 1, 1)
|
14
|
+
@end_date = Date.new(@year, 12, 31)
|
15
|
+
end
|
16
|
+
|
17
|
+
def id
|
18
|
+
@year
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
@year.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
def next
|
26
|
+
self.class.new(@year + 1)
|
27
|
+
end
|
28
|
+
|
29
|
+
def prev
|
30
|
+
self.class.new(@year - 1)
|
31
|
+
end
|
32
|
+
|
33
|
+
def months
|
34
|
+
Month.new(@year, 1)..Month.new(@year, 12)
|
35
|
+
end
|
36
|
+
|
37
|
+
def weeks
|
38
|
+
Week.new(@year, 1)..Week.new(@year, 52)
|
39
|
+
end
|
40
|
+
|
41
|
+
alias_method :succ, :next
|
42
|
+
alias_method :pred, :prev
|
43
|
+
end
|
data/sig/tempus.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tempora
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Max Power
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-02-15 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: Tempora is a lightweight Ruby library for handling months, weeks, quarters,
|
13
|
+
and years with intuitive date-based calculations and range support.
|
14
|
+
email:
|
15
|
+
- kevin.melchert@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE.txt
|
21
|
+
- README.md
|
22
|
+
- Rakefile
|
23
|
+
- lib/initialization.rb
|
24
|
+
- lib/month.rb
|
25
|
+
- lib/quarter.rb
|
26
|
+
- lib/tempora.rb
|
27
|
+
- lib/tempora/version.rb
|
28
|
+
- lib/timeperiod.rb
|
29
|
+
- lib/week.rb
|
30
|
+
- lib/year.rb
|
31
|
+
- sig/tempus.rbs
|
32
|
+
homepage: https://github.com/max-power/tempora
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata:
|
36
|
+
allowed_push_host: https://rubygems.org
|
37
|
+
homepage_uri: https://github.com/max-power/tempora
|
38
|
+
source_code_uri: https://github.com/max-power/tempora
|
39
|
+
changelog_uri: https://github.com/max-power/tempora/README.md
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.1.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.6.2
|
55
|
+
specification_version: 4
|
56
|
+
summary: Tempora is a lightweight Ruby library for handling months, weeks, quarters,
|
57
|
+
and years.
|
58
|
+
test_files: []
|