nth_weekday 0.1.2 → 0.2.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 +4 -4
- data/README.md +17 -6
- data/VERSION +1 -1
- data/lib/nth_weekday.rb +25 -9
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e28ea963f943f3e81ebc4875aae5e1fd589e59ccb0805255e32b9e56aa33548b
|
|
4
|
+
data.tar.gz: adb591ba5a6e1438f514631081414236e94eb10ef86cc1b76fb87a12b92abed8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9ced69b3dc9394c73dae6ce531cc2946e0cec722d6f049abf5f33cd36c12e11dce1da87475243fa38e77e7d3bf08f509c66162e85e0e247f100ec340c6076d58
|
|
7
|
+
data.tar.gz: 9c5ecc75f439b0c174d87afd85defa3f8b70d35ef999c950830ab76c47a83439d19c4f103b897242767c9911fde974c52387849805c61f862645f510f729d266
|
data/README.md
CHANGED
|
@@ -46,16 +46,27 @@ NthWeekday.get(year: 2025, month: 4, weekday: :we, nth: 3)
|
|
|
46
46
|
# 最後の金曜日(2025年12月)
|
|
47
47
|
NthWeekday.get(year: 2025, month: 12, weekday: :fr, nth: -1)
|
|
48
48
|
# => #<Date: 2025-12-26>
|
|
49
|
+
|
|
50
|
+
# 日付文字列として取得
|
|
51
|
+
NthWeekday.get(year: 2025, month: 4, weekday: :we, nth: 3, format: '%Y-%m-%d')
|
|
52
|
+
# => "2025-04-16"
|
|
53
|
+
|
|
54
|
+
# UNIXタイムスタンプとして取得(UTC 00:00:00基準)
|
|
55
|
+
NthWeekday.get(year: 2025, month: 4, weekday: :we, nth: 3, format: :unix)
|
|
56
|
+
# => 1744761600
|
|
49
57
|
```
|
|
50
58
|
|
|
51
59
|
## 📘 Parameters / パラメータ
|
|
52
60
|
|
|
53
|
-
| パラメータ名 | 型
|
|
54
|
-
|
|
55
|
-
| `year` | Integer| 対象年(例: 2025) |
|
|
56
|
-
| `month` | Integer| 対象月(1〜12) |
|
|
57
|
-
| `weekday` | Symbol
|
|
58
|
-
| `nth` | Integer| 第n◯曜日。1〜5、または `-1` で「最後の◯曜日」を指定 |
|
|
61
|
+
| パラメータ名 | 型 | 説明 |
|
|
62
|
+
|--------------|-----------------------|-------------------------------------------------------|
|
|
63
|
+
| `year` | Integer | 対象年(例: 2025) |
|
|
64
|
+
| `month` | Integer | 対象月(1〜12) |
|
|
65
|
+
| `weekday` | Symbol | 対象曜日(`:su`, `:mo`, `:tu`, `:we`, `:th`, `:fr`, `:sa`) |
|
|
66
|
+
| `nth` | Integer | 第n◯曜日。1〜5、または `-1` で「最後の◯曜日」を指定 |
|
|
67
|
+
| `format` | String, Symbol, nil | 省略可。`String` は `Date#strftime` 形式、`:unix` は UTC 00:00:00 基準の UNIX タイムスタンプ |
|
|
68
|
+
|
|
69
|
+
`format` を省略した場合は、従来通り `Date` オブジェクトを返します。
|
|
59
70
|
|
|
60
71
|
### 曜日シンボル一覧
|
|
61
72
|
- `:su` - 日曜日 (Sunday)
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.2.0
|
data/lib/nth_weekday.rb
CHANGED
|
@@ -21,10 +21,11 @@ module NthWeekday
|
|
|
21
21
|
# @param month [Integer] Month (1-12)
|
|
22
22
|
# @param weekday [Symbol] Weekday symbol (:mo, :tu, :we, :th, :fr, :sa, :su)
|
|
23
23
|
# @param nth [Integer] The occurrence of the weekday (1-5, or -1 for last occurrence)
|
|
24
|
-
# @
|
|
24
|
+
# @param format [String, Symbol, nil] Optional output format. String uses Date#strftime, :unix returns UTC midnight timestamp.
|
|
25
|
+
# @return [Date, String, Integer, nil] Date object, formatted string, UNIX timestamp, or nil when no matching date exists
|
|
25
26
|
# @raise [ArgumentError] If parameters are invalid
|
|
26
|
-
def get(year:, month:, weekday:, nth:)
|
|
27
|
-
validate_params(year, month, weekday, nth)
|
|
27
|
+
def get(year:, month:, weekday:, nth:, format: nil)
|
|
28
|
+
validate_params(year, month, weekday, nth, format)
|
|
28
29
|
|
|
29
30
|
wday = WEEKDAY_MAP[weekday.to_sym]
|
|
30
31
|
raise ArgumentError, 'Invalid weekday symbol' unless wday
|
|
@@ -34,19 +35,34 @@ module NthWeekday
|
|
|
34
35
|
|
|
35
36
|
days = (first_day..last_day).select { |date| date.wday == wday }
|
|
36
37
|
|
|
37
|
-
if nth == -1
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
date = if nth == -1
|
|
39
|
+
days.last
|
|
40
|
+
else
|
|
41
|
+
days[nth - 1]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
format_date(date, format)
|
|
42
45
|
end
|
|
43
46
|
|
|
44
47
|
private
|
|
45
48
|
|
|
46
|
-
def validate_params(year, month, _weekday, nth)
|
|
49
|
+
def validate_params(year, month, _weekday, nth, format)
|
|
47
50
|
raise ArgumentError, "Invalid year: #{year}" unless year.is_a?(Integer) && year.positive?
|
|
48
51
|
raise ArgumentError, "Invalid month: #{month}" unless month.is_a?(Integer) && month.between?(1, 12)
|
|
49
52
|
raise ArgumentError, "Invalid nth: #{nth}" unless nth.is_a?(Integer) && (nth.between?(1, 5) || nth == -1)
|
|
53
|
+
raise ArgumentError, "Invalid format: #{format}" unless valid_format?(format)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def valid_format?(format)
|
|
57
|
+
format.nil? || format.is_a?(String) || format == :unix
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def format_date(date, format)
|
|
61
|
+
return nil if date.nil?
|
|
62
|
+
return date if format.nil?
|
|
63
|
+
return Time.utc(date.year, date.month, date.day).to_i if format == :unix
|
|
64
|
+
|
|
65
|
+
date.strftime(format)
|
|
50
66
|
end
|
|
51
67
|
end
|
|
52
68
|
end
|