tempora 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8c7a7d910796a3c678069364a7b4ecd5ec076047510fb0ae12214a360849bcb
4
- data.tar.gz: a1c2639026325e11d15985a329169ad1d09d80e4eff423f45c14a64c5b5d3e6a
3
+ metadata.gz: df646739fb8d7d27a0bb3eed5a3981c668b300e7f105245cb340a233bf447a54
4
+ data.tar.gz: 45821e6f2200f5dc69838086ab76bcedfee33773bf7883dfd5fa82fb9a736195
5
5
  SHA512:
6
- metadata.gz: c71c88d86a13485cdbf93c793052ad8a04b9a4e76383d31f8e187f09a8e0a76b9f1e69b1cb70db6c58f8a40270586eccb20a2e6ed3bf77705e4c106e385bb4e6
7
- data.tar.gz: 3e8e137aca28e67bf704a99c42205a63052bdeec22c9ddb2d17a0dd29ca061db4809a7667cb8be12f7be474745e37c735e209d5a1fe48aa72aa394bb52dc9b97
6
+ metadata.gz: 3bce9a11d0bc1fedc9f975392c629ebe449a3ddae1ed50584941d0f3256d4b09729a69da168f7fed2cd04cd34d33457788a3cb00a511e9256687d25a51a76fc1
7
+ data.tar.gz: d44541c8c7894193804b6d1fce6adf731c8acfa14ffe2d528487f4c839d66e85d2a01992f7209184d1d9323f4fa32ebbcb79be05364b424f03999d4d0ec0eb4e
data/lib/_timespan.rb ADDED
@@ -0,0 +1,106 @@
1
+ require 'date'
2
+
3
+ class TimeSpan
4
+ include Comparable
5
+
6
+ attr_reader :start_date, :end_date
7
+
8
+ def initialize(start_date, end_date)
9
+ @start_date = TimeSpan.from_date_like(start_date)
10
+ @end_date = TimeSpan.from_date_like(end_date)
11
+
12
+ raise ArgumentError, "start_date must be before end_date" if @start_date > @end_date
13
+ end
14
+
15
+ def self.from_date_like(date_like)
16
+ if date_like.respond_to?(:year) && date_like.respond_to?(:month) && date_like.respond_to?(:day)
17
+ Date.new(date_like.year, date_like.month, date_like.day)
18
+ else
19
+ raise ArgumentError, "Invalid date-like object: #{date_like.inspect}"
20
+ end
21
+ end
22
+
23
+ def range
24
+ @start_date..@end_date
25
+ end
26
+
27
+ def duration
28
+ (@end_date - @start_date).to_i + 1
29
+ end
30
+
31
+ def overlaps?(other)
32
+ @start_date <= other.end_date && @end_date >= other.start_date
33
+ end
34
+
35
+ def contains?(date)
36
+ range.cover?(TimeSpan.from_date_like(date))
37
+ end
38
+
39
+ def shift(days)
40
+ TimeSpan.new(@start_date + days, @end_date + days)
41
+ end
42
+
43
+ def intersection(other)
44
+ return nil unless overlaps?(other)
45
+
46
+ new_start = [@start_date, other.start_date].max
47
+ new_end = [@end_date, other.end_date].min
48
+
49
+ TimeSpan.new(new_start, new_end)
50
+ end
51
+
52
+ def <=>(other)
53
+ start_date <=> other.start_date
54
+ end
55
+
56
+ def to_s
57
+ "#{@start_date} to #{@end_date} (#{duration} days)"
58
+ end
59
+ end
60
+ # 🔹 TimeSpan Class (For Any Date Range)
61
+ #
62
+ # This will handle custom periods, like fiscal years, school semesters, or project timelines.
63
+ #
64
+ # 🔹 How TimeSpan Works
65
+ #
66
+ # ✅ Define Any Custom Date Range
67
+ # fiscal_year = TimeSpan.new(Date.new(2024, 4, 1), Date.new(2025, 3, 31))
68
+ # puts fiscal_year.to_s
69
+ # # => "2024-04-01 to 2025-03-31 (365 days)"
70
+ # ✅ Check Overlaps
71
+ # q2 = TimeSpan.new(Date.new(2024, 4, 1), Date.new(2024, 6, 30))
72
+ # q3 = TimeSpan.new(Date.new(2024, 7, 1), Date.new(2024, 9, 30))
73
+ # puts q2.overlaps?(q3) # => false
74
+ # ✅ Check If a Date Falls Within the Range
75
+ # semester = TimeSpan.new(Date.new(2024, 8, 1), Date.new(2024, 12, 15))
76
+ # puts semester.contains?(Date.new(2024, 9, 10)) # => true
77
+ # puts semester.contains?(Date.new(2025, 1, 1)) # => false
78
+ # ✅ Shift Date Ranges
79
+ # event = TimeSpan.new(Date.new(2024, 6, 10), Date.new(2024, 6, 15))
80
+ # puts event.shift(7).to_s
81
+ # # => "2024-06-17 to 2024-06-22 (6 days)"
82
+ #
83
+ # 🔹 Why TimeSpan is Useful
84
+ #
85
+ # ✅ Handles custom time ranges (fiscal years, semesters, event durations)
86
+ # ✅ Works alongside Week, Month, Quarter, and Year
87
+ # ✅ Supports overlapping checks and shifts
88
+ #
89
+ # 🔹 How intersection Works
90
+ #
91
+ # ✅ Two Overlapping Periods
92
+ # t1 = TimeSpan.new(Date.new(2024, 3, 1), Date.new(2024, 3, 20))
93
+ # t2 = TimeSpan.new(Date.new(2024, 3, 10), Date.new(2024, 3, 25))
94
+ #
95
+ # overlap = t1.intersection(t2)
96
+ # puts overlap.to_s
97
+ # # => "2024-03-10 to 2024-03-20 (11 days)"
98
+ # ✅ No Overlap Returns nil
99
+ # t3 = TimeSpan.new(Date.new(2024, 4, 1), Date.new(2024, 4, 10))
100
+ # puts t1.intersection(t3)
101
+ # # => nil
102
+ # 🔹 Why intersection is Useful
103
+ #
104
+ # ✅ Find common time slots (e.g., two employees' available time)
105
+ # ✅ Determine overlapping project phases
106
+ # ✅ Works with any time period comparisons
data/lib/month.rb CHANGED
@@ -42,6 +42,10 @@ class Month
42
42
  Week.from(start_date)..Week.from(end_date)
43
43
  end
44
44
 
45
+ def each_week(&block)
46
+ weeks.each(&block)
47
+ end
48
+
45
49
  alias_method :month, :number
46
50
  alias_method :succ, :next
47
51
  alias_method :pred, :prev
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tempora
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/week.rb CHANGED
@@ -7,12 +7,12 @@ class Week
7
7
 
8
8
  def initialize(year, week)
9
9
  @year = Integer(year)
10
- @number = Integer(week).clamp(1,52)
10
+ @number = Integer(week)
11
11
 
12
12
  @start_date = Date.commercial(@year, @number, 1)
13
13
  @end_date = start_date + 6
14
14
  end
15
-
15
+
16
16
  def id(seperator="-")
17
17
  "#{year}#{seperator}W#{format('%02d', number)}"
18
18
  end
@@ -31,10 +31,10 @@ class Week
31
31
 
32
32
  alias_method :succ, :next
33
33
  alias_method :pred, :prev
34
-
34
+
35
35
  private
36
36
 
37
37
  def self.initialization_parameter(date)
38
- [date.year, date.cweek]
38
+ [date.cwyear, date.cweek]
39
39
  end
40
40
  end
data/lib/year.rb CHANGED
@@ -37,6 +37,14 @@ class Year
37
37
  def weeks
38
38
  Week.from(start_date)..Week.from(end_date)
39
39
  end
40
+
41
+ def each_month(&block)
42
+ months.each(&block)
43
+ end
44
+
45
+ def each_week(&block)
46
+ weeks.each(&block)
47
+ end
40
48
 
41
49
  alias_method :succ, :next
42
50
  alias_method :pred, :prev
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tempora
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Power
@@ -20,6 +20,7 @@ files:
20
20
  - LICENSE.txt
21
21
  - README.md
22
22
  - Rakefile
23
+ - lib/_timespan.rb
23
24
  - lib/initialization.rb
24
25
  - lib/month.rb
25
26
  - lib/quarter.rb