philiprehberger-date_kit 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6d98337f647fbd67ead4931f4f95e0553a15076c01fc4a58f0ec5139e1b0bc4a
4
+ data.tar.gz: a27295be6217b5cfafa6e8b9a9041590324c047fad03e491596598a1e0d30cfa
5
+ SHA512:
6
+ metadata.gz: 291136dcc83f3445d53d59a91a86c18e75434eca88713be6dce3d0ae23b0990da9dc0993821c5153c2d111333763d2a0aee5f894c908da63344c6fa859e5b9b7
7
+ data.tar.gz: a6897bcffebfd3dfaf94ff825aabbbe79332bdda5c56333bbedcced1c6f86746651012a549509d7d1016861acb6c3e6d5b2c4796676c3381ef8d035233b207b3
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+
3
+ All notable changes to this gem will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-03-22
11
+
12
+ ### Added
13
+
14
+ - Initial release
15
+ - Business day counting between two dates
16
+ - Business day arithmetic with holiday support
17
+ - Quarter boundary calculation (beginning and end)
18
+ - Weekend detection
19
+ - Natural language relative date parsing ("2 weeks ago", "next month", "yesterday")
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 philiprehberger
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # philiprehberger-date_kit
2
+
3
+ [![Tests](https://github.com/philiprehberger/rb-date-kit/actions/workflows/ci.yml/badge.svg)](https://github.com/philiprehberger/rb-date-kit/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/philiprehberger-date_kit.svg)](https://rubygems.org/gems/philiprehberger-date_kit)
5
+ [![License](https://img.shields.io/github/license/philiprehberger/rb-date-kit)](LICENSE)
6
+
7
+ Date utilities for business days, relative expressions, and period calculations
8
+
9
+ ## Requirements
10
+
11
+ - Ruby >= 3.1
12
+
13
+ ## Installation
14
+
15
+ Add to your Gemfile:
16
+
17
+ ```ruby
18
+ gem "philiprehberger-date_kit"
19
+ ```
20
+
21
+ Or install directly:
22
+
23
+ ```bash
24
+ gem install philiprehberger-date_kit
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require "philiprehberger/date_kit"
31
+
32
+ Philiprehberger::DateKit.business_days_between(Date.new(2026, 3, 16), Date.new(2026, 3, 23))
33
+ # => 4
34
+ ```
35
+
36
+ ### Adding Business Days
37
+
38
+ ```ruby
39
+ Philiprehberger::DateKit.add_business_days(Date.new(2026, 3, 20), 3)
40
+ # => 2026-03-25 (skips weekend)
41
+
42
+ holidays = [Date.new(2026, 3, 23)]
43
+ Philiprehberger::DateKit.add_business_days(Date.new(2026, 3, 20), 1, holidays: holidays)
44
+ # => 2026-03-24 (skips weekend and holiday)
45
+ ```
46
+
47
+ ### Quarter Boundaries
48
+
49
+ ```ruby
50
+ Philiprehberger::DateKit.beginning_of_quarter(Date.new(2026, 5, 15))
51
+ # => 2026-04-01
52
+
53
+ Philiprehberger::DateKit.end_of_quarter(Date.new(2026, 5, 15))
54
+ # => 2026-06-30
55
+ ```
56
+
57
+ ### Relative Date Parsing
58
+
59
+ ```ruby
60
+ Philiprehberger::DateKit.parse_relative('2 weeks ago')
61
+ Philiprehberger::DateKit.parse_relative('next month')
62
+ Philiprehberger::DateKit.parse_relative('yesterday')
63
+ Philiprehberger::DateKit.parse_relative('in 3 days')
64
+ ```
65
+
66
+ ### Weekend Detection
67
+
68
+ ```ruby
69
+ Philiprehberger::DateKit.weekend?(Date.new(2026, 3, 21)) # => true (Saturday)
70
+ Philiprehberger::DateKit.weekend?(Date.new(2026, 3, 20)) # => false (Friday)
71
+ ```
72
+
73
+ ## API
74
+
75
+ | Method | Description |
76
+ |--------|-------------|
77
+ | `.business_days_between(start, finish)` | Count business days between two dates |
78
+ | `.add_business_days(date, n, holidays:)` | Add business days, skipping weekends and holidays |
79
+ | `.beginning_of_quarter(date)` | Return the first day of the quarter |
80
+ | `.end_of_quarter(date)` | Return the last day of the quarter |
81
+ | `.weekend?(date)` | Check if a date falls on a weekend |
82
+ | `.parse_relative(str, relative_to:)` | Parse a relative date expression |
83
+
84
+ ## Development
85
+
86
+ ```bash
87
+ bundle install
88
+ bundle exec rspec
89
+ bundle exec rubocop
90
+ ```
91
+
92
+ ## License
93
+
94
+ MIT
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module DateKit
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,192 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require_relative 'date_kit/version'
5
+
6
+ module Philiprehberger
7
+ module DateKit
8
+ class Error < StandardError; end
9
+
10
+ # Count business days (Mon-Fri) between two dates, exclusive of both endpoints
11
+ #
12
+ # @param start_date [Date] the start date
13
+ # @param finish_date [Date] the end date
14
+ # @return [Integer] number of business days between the dates
15
+ def self.business_days_between(start_date, finish_date)
16
+ start_date = coerce_date(start_date)
17
+ finish_date = coerce_date(finish_date)
18
+
19
+ return 0 if start_date >= finish_date
20
+
21
+ count = 0
22
+ current = start_date + 1
23
+ while current < finish_date
24
+ count += 1 unless weekend?(current)
25
+ current += 1
26
+ end
27
+ count
28
+ end
29
+
30
+ # Add business days to a date, skipping weekends and holidays
31
+ #
32
+ # @param date [Date] the starting date
33
+ # @param days [Integer] number of business days to add
34
+ # @param holidays [Array<Date>] optional list of holiday dates to skip
35
+ # @return [Date] the resulting date
36
+ def self.add_business_days(date, days, holidays: [])
37
+ date = coerce_date(date)
38
+ holidays = holidays.map { |h| coerce_date(h) }
39
+
40
+ raise Error, 'days must be an integer' unless days.is_a?(Integer)
41
+
42
+ direction = days.positive? ? 1 : -1
43
+ remaining = days.abs
44
+ current = date
45
+
46
+ while remaining.positive?
47
+ current += direction
48
+ next if weekend?(current) || holidays.include?(current)
49
+
50
+ remaining -= 1
51
+ end
52
+
53
+ current
54
+ end
55
+
56
+ # Return the first day of the quarter containing the given date
57
+ #
58
+ # @param date [Date] the input date
59
+ # @return [Date] the first day of the quarter
60
+ def self.beginning_of_quarter(date)
61
+ date = coerce_date(date)
62
+ quarter_month = ((date.month - 1) / 3) * 3 + 1
63
+ Date.new(date.year, quarter_month, 1)
64
+ end
65
+
66
+ # Return the last day of the quarter containing the given date
67
+ #
68
+ # @param date [Date] the input date
69
+ # @return [Date] the last day of the quarter
70
+ def self.end_of_quarter(date)
71
+ date = coerce_date(date)
72
+ quarter_month = ((date.month - 1) / 3) * 3 + 3
73
+ end_month = Date.new(date.year, quarter_month, -1)
74
+ end_month
75
+ end
76
+
77
+ # Check if a date falls on a weekend (Saturday or Sunday)
78
+ #
79
+ # @param date [Date] the date to check
80
+ # @return [Boolean]
81
+ def self.weekend?(date)
82
+ date = coerce_date(date)
83
+ date.saturday? || date.sunday?
84
+ end
85
+
86
+ # Parse a relative date expression into a Date
87
+ #
88
+ # @param str [String] the expression (e.g., "2 weeks ago", "next month", "yesterday")
89
+ # @param relative_to [Date] the reference date (defaults to today)
90
+ # @return [Date] the parsed date
91
+ # @raise [Error] if the expression cannot be parsed
92
+ def self.parse_relative(str, relative_to: Date.today)
93
+ relative_to = coerce_date(relative_to)
94
+ normalized = str.to_s.strip.downcase
95
+
96
+ case normalized
97
+ when 'today'
98
+ relative_to
99
+ when 'yesterday'
100
+ relative_to - 1
101
+ when 'tomorrow'
102
+ relative_to + 1
103
+ when /\A(\d+)\s+days?\s+ago\z/
104
+ relative_to - ::Regexp.last_match(1).to_i
105
+ when /\Ain\s+(\d+)\s+days?\z/
106
+ relative_to + ::Regexp.last_match(1).to_i
107
+ when /\A(\d+)\s+weeks?\s+ago\z/
108
+ relative_to - (::Regexp.last_match(1).to_i * 7)
109
+ when /\Ain\s+(\d+)\s+weeks?\z/
110
+ relative_to + (::Regexp.last_match(1).to_i * 7)
111
+ when /\A(\d+)\s+months?\s+ago\z/
112
+ months_ago(relative_to, ::Regexp.last_match(1).to_i)
113
+ when /\Ain\s+(\d+)\s+months?\z/
114
+ months_ahead(relative_to, ::Regexp.last_match(1).to_i)
115
+ when /\A(\d+)\s+years?\s+ago\z/
116
+ years_ago(relative_to, ::Regexp.last_match(1).to_i)
117
+ when /\Ain\s+(\d+)\s+years?\z/
118
+ years_ahead(relative_to, ::Regexp.last_match(1).to_i)
119
+ when 'last week'
120
+ relative_to - 7
121
+ when 'next week'
122
+ relative_to + 7
123
+ when 'last month'
124
+ months_ago(relative_to, 1)
125
+ when 'next month'
126
+ months_ahead(relative_to, 1)
127
+ when 'last year'
128
+ years_ago(relative_to, 1)
129
+ when 'next year'
130
+ years_ahead(relative_to, 1)
131
+ else
132
+ raise Error, "cannot parse relative date: #{str}"
133
+ end
134
+ end
135
+
136
+ # @api private
137
+ def self.months_ago(date, n)
138
+ target_month = date.month - n
139
+ target_year = date.year
140
+ while target_month < 1
141
+ target_month += 12
142
+ target_year -= 1
143
+ end
144
+ day = [date.day, days_in_month(target_year, target_month)].min
145
+ Date.new(target_year, target_month, day)
146
+ end
147
+
148
+ # @api private
149
+ def self.months_ahead(date, n)
150
+ target_month = date.month + n
151
+ target_year = date.year
152
+ while target_month > 12
153
+ target_month -= 12
154
+ target_year += 1
155
+ end
156
+ day = [date.day, days_in_month(target_year, target_month)].min
157
+ Date.new(target_year, target_month, day)
158
+ end
159
+
160
+ # @api private
161
+ def self.years_ago(date, n)
162
+ target_year = date.year - n
163
+ day = [date.day, days_in_month(target_year, date.month)].min
164
+ Date.new(target_year, date.month, day)
165
+ end
166
+
167
+ # @api private
168
+ def self.years_ahead(date, n)
169
+ target_year = date.year + n
170
+ day = [date.day, days_in_month(target_year, date.month)].min
171
+ Date.new(target_year, date.month, day)
172
+ end
173
+
174
+ # @api private
175
+ def self.days_in_month(year, month)
176
+ Date.new(year, month, -1).day
177
+ end
178
+
179
+ # @api private
180
+ def self.coerce_date(value)
181
+ case value
182
+ when Date then value
183
+ when Time then value.to_date
184
+ when String then Date.parse(value)
185
+ else
186
+ raise Error, "cannot coerce #{value.class} to Date"
187
+ end
188
+ end
189
+
190
+ private_class_method :months_ago, :months_ahead, :years_ago, :years_ahead, :days_in_month, :coerce_date
191
+ end
192
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: philiprehberger-date_kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Philip Rehberger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-03-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Date utilities including business day counting and arithmetic, quarter
14
+ boundaries, weekend detection, and natural language relative date parsing.
15
+ email:
16
+ - me@philiprehberger.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CHANGELOG.md
22
+ - LICENSE
23
+ - README.md
24
+ - lib/philiprehberger/date_kit.rb
25
+ - lib/philiprehberger/date_kit/version.rb
26
+ homepage: https://github.com/philiprehberger/rb-date-kit
27
+ licenses:
28
+ - MIT
29
+ metadata:
30
+ homepage_uri: https://github.com/philiprehberger/rb-date-kit
31
+ source_code_uri: https://github.com/philiprehberger/rb-date-kit
32
+ changelog_uri: https://github.com/philiprehberger/rb-date-kit/blob/main/CHANGELOG.md
33
+ bug_tracker_uri: https://github.com/philiprehberger/rb-date-kit/issues
34
+ rubygems_mfa_required: 'true'
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.1.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.5.22
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Date utilities for business days, relative expressions, and period calculations
54
+ test_files: []