nth_weekday 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 +21 -0
- data/README.md +85 -0
- data/Rakefile +6 -0
- data/VERSION +1 -0
- data/examples/basic_usage.rb +21 -0
- data/examples/simple_demo.rb +21 -0
- data/lib/nth_weekday.rb +50 -0
- metadata +98 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6fe9148a95e6fccceea5df2e566aacef828d5ba8f45c1a3c1f5a617cb885fd83
|
|
4
|
+
data.tar.gz: d1fd53f25234b13c9a83e6b23ec1e38ae7391cc57d25a3173f60e51bdef5cc14
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8fa5d703efbdb0227a22669f21f709bde1441f46d112a6aca5f5409f1fdf337850a7fdbdc984f90d856395c230fdded36c38cbd7f7805eb717bed5c77d42a87f
|
|
7
|
+
data.tar.gz: 17049383e92ed5e877ba249bf4900484bd583b547d149264eb5fb8251ec14219dd45cffad33e2733f4ae91402fa16d92a942627c202bb3f1fb5528aef0dd0706
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yusuke Higasa
|
|
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,85 @@
|
|
|
1
|
+
# nth_weekday
|
|
2
|
+
|
|
3
|
+
[](https://rubygems.org/gems/nth_weekday)
|
|
4
|
+
[](https://github.com/yusukehigasa/nth_weekday/actions)
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
**🇯🇵 任意の年月から「第3水曜日」「最終金曜日」などを簡単に取得できる軽量Rubyライブラリです。Rails対応・外部依存ゼロ。**
|
|
9
|
+
|
|
10
|
+
**🇺🇸 A lightweight Ruby library to get dates like "3rd Wednesday" or "last Friday" from any given year and month. Rails-compatible and dependency-free.**
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## ✨ Features / 特長
|
|
15
|
+
|
|
16
|
+
- 🎯 指定月の「第◯◯曜日」を簡単に取得
|
|
17
|
+
- 🔧 `:we`, `:fr` のような直感的な曜日指定
|
|
18
|
+
- ⚙️ Rubyのみで動作、ActiveSupport不要
|
|
19
|
+
- ✅ Rails / Sinatra / CLI / バッチ対応
|
|
20
|
+
- 🧪 テスト済み、CI/CD導入しやすい構成
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## 💎 Installation / インストール
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
# Gemfileに追加
|
|
27
|
+
gem 'nth_weekday'
|
|
28
|
+
|
|
29
|
+
# または直接インストール
|
|
30
|
+
gem install nth_weekday
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## 🚀 Usage / 使い方
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
require 'nth_weekday'
|
|
37
|
+
|
|
38
|
+
# 第3水曜日(2025年4月)
|
|
39
|
+
NthWeekday.get(year: 2025, month: 4, weekday: :we, nth: 3)
|
|
40
|
+
# => #<Date: 2025-04-16>
|
|
41
|
+
|
|
42
|
+
# 最後の金曜日(2025年12月)
|
|
43
|
+
NthWeekday.get(year: 2025, month: 12, weekday: :fr, nth: -1)
|
|
44
|
+
# => #<Date: 2025-12-26>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 📘 Parameters / パラメータ
|
|
48
|
+
|
|
49
|
+
| パラメータ名 | 型 | 説明 |
|
|
50
|
+
|--------------|--------|-------------------------------------------------------|
|
|
51
|
+
| `year` | Integer| 対象年(例: 2025) |
|
|
52
|
+
| `month` | Integer| 対象月(1〜12) |
|
|
53
|
+
| `weekday` | Symbol | 対象曜日(例: `:mo`, `:tu`, `:we`, `:fr` など) |
|
|
54
|
+
| `nth` | Integer| 第n◯曜日。1〜5、または `-1` で「最後の◯曜日」を指定 |
|
|
55
|
+
|
|
56
|
+
## 🧪 Example: 2025年の各月の第3水曜日を取得
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
(1..12).each do |month|
|
|
60
|
+
date = NthWeekday.get(year: 2025, month: month, weekday: :we, nth: 3)
|
|
61
|
+
puts "2025年#{month}月 第3水曜日: #{date}"
|
|
62
|
+
end
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## 📦 Development / 開発者向け
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
git clone https://github.com/yusukehigasa/nth_weekday.git
|
|
69
|
+
cd nth_weekday
|
|
70
|
+
bundle install
|
|
71
|
+
rspec
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## 📄 License
|
|
75
|
+
|
|
76
|
+
- MIT License
|
|
77
|
+
|
|
78
|
+
## 🙌 Author
|
|
79
|
+
|
|
80
|
+
- [Yusuke Higasa](https://github.com/yusukehigasa)
|
|
81
|
+
|
|
82
|
+
## 🔗 Links
|
|
83
|
+
|
|
84
|
+
- [RubyGems - nth_weekday](https://rubygems.org/gems/nth_weekday)
|
|
85
|
+
- [GitHub Repository](https://github.com/yusukehigasa/nth_weekday)
|
data/Rakefile
ADDED
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.0
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# nth_weekday の使用例
|
|
2
|
+
|
|
3
|
+
require 'nth_weekday'
|
|
4
|
+
|
|
5
|
+
## 基本的な使用方法
|
|
6
|
+
|
|
7
|
+
# 2025年5月の第2月曜日を取得
|
|
8
|
+
monday = NthWeekday.get(year: 2025, month: 5, weekday: :mo, nth: 2)
|
|
9
|
+
puts "2025年5月の第2月曜日: #{monday.strftime('%Y-%m-%d')}"
|
|
10
|
+
|
|
11
|
+
# 2025年12月の最終金曜日を取得
|
|
12
|
+
last_friday = NthWeekday.get(year: 2025, month: 12, weekday: :fr, nth: -1)
|
|
13
|
+
puts "2025年12月の最終金曜日: #{last_friday.strftime('%Y-%m-%d')}"
|
|
14
|
+
|
|
15
|
+
## 応用例: 年間の特定曜日を一覧表示
|
|
16
|
+
|
|
17
|
+
puts "\n2025年の全ての第3水曜日:"
|
|
18
|
+
(1..12).each do |month|
|
|
19
|
+
date = NthWeekday.get(year: 2025, month: month, weekday: :we, nth: 3)
|
|
20
|
+
puts "2025年#{month}月の第3水曜日: #{date.strftime('%Y-%m-%d')}"
|
|
21
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'nth_weekday'
|
|
4
|
+
|
|
5
|
+
puts "NthWeekday gem example"
|
|
6
|
+
puts "======================="
|
|
7
|
+
|
|
8
|
+
# 2025年5月の第2月曜日を取得
|
|
9
|
+
monday = NthWeekday.get(year: 2025, month: 5, weekday: :mo, nth: 2)
|
|
10
|
+
puts "2025年5月の第2月曜日: #{monday.strftime('%Y-%m-%d')}"
|
|
11
|
+
|
|
12
|
+
# 2025年12月の最終金曜日を取得
|
|
13
|
+
last_friday = NthWeekday.get(year: 2025, month: 12, weekday: :fr, nth: -1)
|
|
14
|
+
puts "2025年12月の最終金曜日: #{last_friday.strftime('%Y-%m-%d')}"
|
|
15
|
+
|
|
16
|
+
# 2025年の全ての第3水曜日を表示
|
|
17
|
+
puts "\n2025年の全ての第3水曜日:"
|
|
18
|
+
(1..12).each do |month|
|
|
19
|
+
date = NthWeekday.get(year: 2025, month: month, weekday: :we, nth: 3)
|
|
20
|
+
puts "2025年#{month}月の第3水曜日: #{date.strftime('%Y-%m-%d')}"
|
|
21
|
+
end
|
data/lib/nth_weekday.rb
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
|
|
3
|
+
# NthWeekday module - Get specific weekdays (like 3rd Wednesday) from any year/month
|
|
4
|
+
module NthWeekday
|
|
5
|
+
# Mapping of weekday symbols to Date's wday values (0-6, Sunday is 0)
|
|
6
|
+
WEEKDAY_MAP = {
|
|
7
|
+
su: 0, # Sunday
|
|
8
|
+
mo: 1, # Monday
|
|
9
|
+
tu: 2, # Tuesday
|
|
10
|
+
we: 3, # Wednesday
|
|
11
|
+
th: 4, # Thursday
|
|
12
|
+
fr: 5, # Friday
|
|
13
|
+
sa: 6 # Saturday
|
|
14
|
+
}.freeze
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
# Get the nth weekday of a specific month and year
|
|
18
|
+
# @param year [Integer] Year (e.g., 2025)
|
|
19
|
+
# @param month [Integer] Month (1-12)
|
|
20
|
+
# @param weekday [Symbol] Weekday symbol (:mo, :tu, :we, :th, :fr, :sa, :su)
|
|
21
|
+
# @param nth [Integer] The occurrence of the weekday (1-5, or -1 for last occurrence)
|
|
22
|
+
# @return [Date] Date object representing the requested day
|
|
23
|
+
# @raise [ArgumentError] If parameters are invalid
|
|
24
|
+
def get(year:, month:, weekday:, nth:)
|
|
25
|
+
validate_params(year, month, weekday, nth)
|
|
26
|
+
|
|
27
|
+
wday = WEEKDAY_MAP[weekday.to_sym]
|
|
28
|
+
raise ArgumentError, "Invalid weekday symbol" unless wday
|
|
29
|
+
|
|
30
|
+
first_day = Date.new(year, month, 1)
|
|
31
|
+
last_day = first_day.next_month.prev_day
|
|
32
|
+
|
|
33
|
+
days = (first_day..last_day).select { |date| date.wday == wday }
|
|
34
|
+
|
|
35
|
+
if nth == -1
|
|
36
|
+
days.last
|
|
37
|
+
else
|
|
38
|
+
days[nth - 1]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def validate_params(year, month, weekday, nth)
|
|
45
|
+
raise ArgumentError, "Invalid year: #{year}" unless year.is_a?(Integer) && year > 0
|
|
46
|
+
raise ArgumentError, "Invalid month: #{month}" unless month.is_a?(Integer) && month.between?(1, 12)
|
|
47
|
+
raise ArgumentError, "Invalid nth: #{nth}" unless nth.is_a?(Integer) && (nth.between?(1, 5) || nth == -1)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: nth_weekday
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Yusuke Higasa
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-05-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '13.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '13.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
description: A lightweight Ruby library to get dates like "3rd Wednesday" or "last
|
|
56
|
+
Friday" from any given year and month. Rails-compatible and dependency-free.
|
|
57
|
+
email:
|
|
58
|
+
- higasa@studio-higasa.com
|
|
59
|
+
executables: []
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- LICENSE
|
|
64
|
+
- README.md
|
|
65
|
+
- Rakefile
|
|
66
|
+
- VERSION
|
|
67
|
+
- examples/basic_usage.rb
|
|
68
|
+
- examples/simple_demo.rb
|
|
69
|
+
- lib/nth_weekday.rb
|
|
70
|
+
homepage: https://github.com/yusukehigasa/nth_weekday
|
|
71
|
+
licenses:
|
|
72
|
+
- MIT
|
|
73
|
+
metadata:
|
|
74
|
+
homepage_uri: https://github.com/yusukehigasa/nth_weekday
|
|
75
|
+
source_code_uri: https://github.com/yusukehigasa/nth_weekday
|
|
76
|
+
changelog_uri: https://github.com/yusukehigasa/nth_weekday/blob/main/CHANGELOG.md
|
|
77
|
+
bug_tracker_uri: https://github.com/yusukehigasa/nth_weekday/issues
|
|
78
|
+
rubygems_mfa_required: 'true'
|
|
79
|
+
post_install_message:
|
|
80
|
+
rdoc_options: []
|
|
81
|
+
require_paths:
|
|
82
|
+
- lib
|
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
|
+
requirements:
|
|
85
|
+
- - ">="
|
|
86
|
+
- !ruby/object:Gem::Version
|
|
87
|
+
version: 3.3.0
|
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
|
+
requirements:
|
|
90
|
+
- - ">="
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: '0'
|
|
93
|
+
requirements: []
|
|
94
|
+
rubygems_version: 3.5.16
|
|
95
|
+
signing_key:
|
|
96
|
+
specification_version: 4
|
|
97
|
+
summary: Get dates like "3rd Wednesday" or "last Friday" from any given year and month
|
|
98
|
+
test_files: []
|