jalalix 0.1.0 → 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/CHANGELOG.md +5 -0
- data/README.md +213 -10
- data/lib/jalalix/jalalix.rb +110 -4
- data/lib/jalalix/version.rb +1 -1
- metadata +8 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 73ce462481c155589f78c0c816212773457b19b54970de398f4b676541bac4e0
|
|
4
|
+
data.tar.gz: febf42466c83cb12e54cba0144081cbc9fd9e89a343b22a5f55ac1b99dcf1ba7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9f450137368eb31ad929e3666e8e250a5e29a220aaba1990b499fedd3e0d0497ef85abf10f59c0df70fe2f3fe5450b02e3e8215918a6cad4ab72f47204ecc5e0
|
|
7
|
+
data.tar.gz: e715bf5387b7ff66ea1499b5bde9180a7b7f43d5804a952c8fd8d3d181c9a3167db19049035659f535287fb73380645cd9e81d3e53f3013411a28ab83148cdbf
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Release Notes
|
|
2
2
|
All notable changes to this package will be documented in this file.
|
|
3
3
|
|
|
4
|
+
## 0.2.0
|
|
5
|
+
- Added `normalizeQueryDate` for loose query/form inputs (sentinels, unix s/ms, Jalali vs Gregorian auto-detect by year band; default `1…1899`, overridable via `jalaliYearMin` / `jalaliYearMax`)
|
|
6
|
+
- `fromPersian` also converts Arabic-Indic digits (`٠-٩`)
|
|
7
|
+
- `parseJalali` accepts `.` as a date separator
|
|
8
|
+
|
|
4
9
|
## 0.1.0
|
|
5
10
|
- Initial commit
|
data/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
# JalaliX
|
|
1
|
+
# JalaliX - Ruby
|
|
2
2
|
|
|
3
|
-
Jalali (Persian / Shamsi) calendar for Ruby. Pure Ruby 3.0+, no runtime gems (timezone via stdlib `ENV["TZ"]` / zoneinfo, with embedded offsets for
|
|
3
|
+
Jalali (Persian / Shamsi) calendar for Ruby. Pure Ruby 3.0+, no runtime gems (timezone via stdlib `ENV["TZ"]` / zoneinfo, with embedded offsets for golden parity).
|
|
4
|
+
|
|
5
|
+
[](./LICENSE)
|
|
4
6
|
|
|
5
7
|
```bash
|
|
6
8
|
gem install jalalix
|
|
@@ -18,28 +20,223 @@ date.format("DD MMM YYYY", persianDigits: true) # ۰۷ اسفند ۱۴۰۳
|
|
|
18
20
|
JalaliX.parseJalali("1403/12/07").toISODate # 2025-02-25
|
|
19
21
|
```
|
|
20
22
|
|
|
21
|
-
`toISODate` uses wall-
|
|
23
|
+
`toISODate` uses wall-clock getters in the active timezone.
|
|
22
24
|
|
|
23
|
-
##
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
gem install jalalix
|
|
29
|
+
# or: bundle add jalalix
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
require "jalalix"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
### create / format
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
now = JalaliX.create(Time.now.to_f * 1000, timezone: "Asia/Tehran")
|
|
42
|
+
|
|
43
|
+
now.format("YYYY/MM/DD")
|
|
44
|
+
now.format("WN، DD MMM YYYY")
|
|
45
|
+
now.format("YYYY/MM/DD", persianDigits: true)
|
|
46
|
+
now.format("DD MMM YYYY", calendar: "gregorian")
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`format` accepts a hash with `:persianDigits` / `:calendar`, or positional `persian_or_options` and `gregorian` flags.
|
|
50
|
+
|
|
51
|
+
### parseJalali
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
JalaliX.parseJalali("1403/12/07")
|
|
55
|
+
JalaliX.parseJalali("۱۴۰۳-۱۲-۰۷ 14:30")
|
|
56
|
+
JalaliX.parseJalali("1403.12.07")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Named `parseJalali` because Ruby's `Date` / `Time` already have parsers. Accepts `/`, `-`, or `.` as the date separator.
|
|
60
|
+
|
|
61
|
+
### normalizeQueryDate
|
|
62
|
+
|
|
63
|
+
Soft-normalize loose API/query date values to a Gregorian `YYYY-MM-DD HH:mm:ss` string (or `nil`). Does not raise on junk input.
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
JalaliX.normalizeQueryDate("1403/12/07", timezone: "Asia/Tehran")
|
|
67
|
+
# => "2025-02-25 00:00:00"
|
|
68
|
+
|
|
69
|
+
JalaliX.normalizeQueryDate("undefined") # nil
|
|
70
|
+
JalaliX.normalizeQueryDate(1_740_430_800_000, timezone: "Asia/Tehran")
|
|
71
|
+
# => "2025-02-25 00:30:00"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Handles sentinels, unix timestamps (seconds or milliseconds; non-positive → `nil`), Persian/Arabic-Indic digits, `/` `-` `.` separators, fractional seconds, optional `Z` / offset suffixes.
|
|
24
75
|
|
|
25
|
-
|
|
26
|
-
|
|
76
|
+
Detects Jalali when the year is in `jalaliYearMin`…`jalaliYearMax` (default `1`…`1899`) **and** the civil date is valid; otherwise Gregorian when valid, or returns the digit-cleaned string. Non-string/non-number values pass through unchanged.
|
|
77
|
+
|
|
78
|
+
### toISODate
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
d = JalaliX.create("2025-02-25T00:30:00+03:30")
|
|
82
|
+
|
|
83
|
+
d.toISODate # "2025-02-25"
|
|
84
|
+
d.toISODate("jalali") # "1403-12-07"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### add / start / end
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
JalaliX.create("2025-02-25").addMonths(1).getStartOfMonth.format("YYYY/MM/DD")
|
|
91
|
+
# e.g. 1404/01/01
|
|
92
|
+
|
|
93
|
+
d = JalaliX.create("2025-02-25", timezone: "Asia/Tehran")
|
|
94
|
+
d.addDays(7).format("YYYY/MM/DD")
|
|
95
|
+
d.getEndOfWeek.format("YYYY/MM/DD")
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Persian / Arabic digits
|
|
99
|
+
|
|
100
|
+
```ruby
|
|
101
|
+
JalaliX.toPersian("1403/12/07") # ۱۴۰۳/۱۲/۰۷
|
|
102
|
+
JalaliX.fromPersian("۱۴۰۳/۱۲/۰۷") # 1403/12/07
|
|
103
|
+
JalaliX.fromPersian("١٤٠٣/١٢/٠٧") # 1403/12/07 (Arabic-Indic)
|
|
104
|
+
```
|
|
27
105
|
|
|
28
106
|
## Timezone
|
|
29
107
|
|
|
30
|
-
Internal storage is a **UTC Unix millisecond instant** plus an IANA timezone id. `setTimezone` is **instant-preserving
|
|
108
|
+
Internal storage is always a **UTC Unix millisecond instant** plus an IANA timezone id. `setTimezone` is **instant-preserving**: the timestamp stays the same; only the zone (and therefore wall clock / Jalali day) changes.
|
|
31
109
|
|
|
32
110
|
```ruby
|
|
33
111
|
d = JalaliX.create(1_740_430_800_000)
|
|
112
|
+
|
|
34
113
|
tehran = d.setTimezone("Asia/Tehran")
|
|
35
114
|
tehran.format("YYYY/MM/DD") # 1403/12/07
|
|
36
|
-
tehran.format("HH:mm
|
|
115
|
+
tehran.format("HH:mm") # 00:30
|
|
116
|
+
|
|
117
|
+
utc = d.setTimezone("UTC")
|
|
118
|
+
utc.format("YYYY/MM/DD") # 1403/12/06
|
|
119
|
+
utc.format("HH:mm") # 21:00
|
|
120
|
+
|
|
121
|
+
tehran.getTime == utc.getTime # true
|
|
37
122
|
```
|
|
38
123
|
|
|
39
|
-
- **Aliases:** `utc` / `gmt`
|
|
40
|
-
- **Invalid zone**
|
|
124
|
+
- **Aliases:** `utc` / `gmt` → `UTC`; `local` / `default` / `system` → host zone when detectable.
|
|
125
|
+
- **Invalid zone** → `ArgumentError`.
|
|
41
126
|
- Offsets use system zoneinfo through `ENV["TZ"]` when available; `Asia/Tehran` (+03:30 since 2022) and `UTC` are also embedded so golden parity works without TZInfo.
|
|
42
127
|
|
|
128
|
+
```ruby
|
|
129
|
+
JalaliX.create(Time.now.to_f * 1000, timezone: "Asia/Tehran")
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Months
|
|
133
|
+
|
|
134
|
+
Easy to get wrong:
|
|
135
|
+
|
|
136
|
+
- `getMonth()` → **1…12** (Farvardin = 1)
|
|
137
|
+
- `setMonth(m)` / `setFullYear(y, m)` → **0…11**, same as JavaScript `Date` (Farvardin = 0)
|
|
138
|
+
|
|
139
|
+
```ruby
|
|
140
|
+
d = JalaliX.create
|
|
141
|
+
d.setFullYear(1404, 0, 1) # 1 Farvardin 1404
|
|
142
|
+
d.getMonth # 1
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Format tokens
|
|
146
|
+
|
|
147
|
+
| Token | Meaning |
|
|
148
|
+
| ------------------- | ------------------------------- |
|
|
149
|
+
| `YYYY` `YY` | year |
|
|
150
|
+
| `MMM` `MM` `M` | month name / number |
|
|
151
|
+
| `DD` `D` | day |
|
|
152
|
+
| `HH` `hh` `mm` `ss` | time |
|
|
153
|
+
| `A` `a` | meridiem (ق.ظ. / ب.ظ. or AM/PM) |
|
|
154
|
+
| `WN` `WL` | weekday name / letter |
|
|
155
|
+
| `w` `DDD` | week of year / day of year |
|
|
156
|
+
| `X` `x` | unix seconds / ms |
|
|
157
|
+
|
|
158
|
+
## API
|
|
159
|
+
|
|
160
|
+
Options hashes use `:timezone`, `:jalaliYearMin`, `:jalaliYearMax` (for `normalizeQueryDate`), `:persianDigits`, and `:calendar` (for `format`).
|
|
161
|
+
|
|
162
|
+
### Constants
|
|
163
|
+
|
|
164
|
+
| API | Description |
|
|
165
|
+
| ----------------- | ----------------------------------------------- |
|
|
166
|
+
| `PERSIAN_NUMBERS` | Array of Persian digit characters `۰`…`۹`. |
|
|
167
|
+
| `ARABIC_NUMBERS` | Array of Arabic-Indic digit characters `٠`…`٩`. |
|
|
168
|
+
|
|
169
|
+
### Class methods
|
|
170
|
+
|
|
171
|
+
| API | Description |
|
|
172
|
+
| -------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
|
|
173
|
+
| `JalaliX.create(date, options)` | Build from unix ms, ISO string, `Time`, `Date`, or `JalaliX`; optional `timezone` in options hash. |
|
|
174
|
+
| `JalaliX.parseJalali(value, options)` | Parse a Jalali date/time string (`/` `-` `.`, Persian/Arabic digits, optional time). |
|
|
175
|
+
| `JalaliX.normalizeQueryDate(value, options)` | Soft-normalize loose query values → Gregorian `YYYY-MM-DD HH:mm:ss`, cleaned string, passthrough, or `nil`. |
|
|
176
|
+
| `JalaliX.isValidDate(date)` | `true` if the value is a supported date type. |
|
|
177
|
+
| `JalaliX.toPersian(str)` | Replace Western digits `0-9` with Persian `۰-۹`. |
|
|
178
|
+
| `JalaliX.fromPersian(str)` | Replace Persian `۰-۹` and Arabic-Indic `٠-٩` with Western digits. |
|
|
179
|
+
| `JalaliX.compare(date, comparing)` | `true` if the first date is strictly before the second (by unix ms). |
|
|
180
|
+
| `JalaliX.host_timezone` | Resolve host timezone name for `local` / `default` / `system` aliases. |
|
|
181
|
+
|
|
182
|
+
### Instance - getters
|
|
183
|
+
|
|
184
|
+
| API | Description |
|
|
185
|
+
| ----------------- | --------------------------------------------------------------------- |
|
|
186
|
+
| `getFullYear` | Jalali year in the active timezone. |
|
|
187
|
+
| `getMonth` | Jalali month **1…12** (Farvardin = 1). |
|
|
188
|
+
| `getDate` | Jalali day of month **1…31**. |
|
|
189
|
+
| `getDay` | Weekday **1…7** (Saturday = 1 … Friday = 7). |
|
|
190
|
+
| `getHours` | Wall-clock hours in the active timezone. |
|
|
191
|
+
| `getMinutes` | Wall-clock minutes. |
|
|
192
|
+
| `getSeconds` | Wall-clock seconds. |
|
|
193
|
+
| `getMilliseconds` | Wall-clock milliseconds. |
|
|
194
|
+
| `getTime` | Unix timestamp in milliseconds (UTC instant). |
|
|
195
|
+
| `getTimezone` | Active IANA timezone id. |
|
|
196
|
+
| `getWeekOfYear` | Jalali week-of-year (1-based). |
|
|
197
|
+
| `getDayOfYear` | Day of Jalali year (1…365/366). |
|
|
198
|
+
| `getDayISO` | ISO-style weekday index used internally for week math. |
|
|
199
|
+
| `getHoursISO` | Hours in 0…23 for formatting helpers. |
|
|
200
|
+
| `getMeridiem` | Localized meridiem string (ق.ظ./ب.ظ. or AM/PM depending on calendar). |
|
|
201
|
+
|
|
202
|
+
### Instance - setters
|
|
203
|
+
|
|
204
|
+
| API | Description |
|
|
205
|
+
| -------------------------------- | -------------------------------------------------------- |
|
|
206
|
+
| `setTime(time)` | Set the UTC instant from unix milliseconds. |
|
|
207
|
+
| `setMilliseconds(ms)` | Set milliseconds (wall clock). |
|
|
208
|
+
| `setSeconds(sec, ms)` | Set seconds and optional milliseconds. |
|
|
209
|
+
| `setMinutes(min, sec, ms)` | Set minutes and optional lower units. |
|
|
210
|
+
| `setHours(hours, min, sec, ms)` | Set hours and optional lower units. |
|
|
211
|
+
| `setDate(date)` | Set Jalali day of month. |
|
|
212
|
+
| `setMonth(month, date)` | Set Jalali month **0…11** (Farvardin = 0); optional day. |
|
|
213
|
+
| `setFullYear(year, month, date)` | Set Jalali year; optional month **0…11** and day. |
|
|
214
|
+
| `setTimezone(timezone)` | Instant-preserving zone change; returns a new instance. |
|
|
215
|
+
|
|
216
|
+
### Instance - math, range, format
|
|
217
|
+
|
|
218
|
+
| API | Description |
|
|
219
|
+
| -------------------------------------------- | ------------------------------------------------------ |
|
|
220
|
+
| `addYears(year)` | Add calendar years (mutates / returns `self`). |
|
|
221
|
+
| `addMonths(month)` | Add calendar months. |
|
|
222
|
+
| `addWeeks(weeks)` | Add weeks. |
|
|
223
|
+
| `addDays(days)` | Add days. |
|
|
224
|
+
| `getStartOfYear` | Start of Jalali year (mutates / returns `self`). |
|
|
225
|
+
| `getStartOfMonth` | Start of Jalali month. |
|
|
226
|
+
| `getStartOfWeek` | Start of week. |
|
|
227
|
+
| `getStartOfDay` | Start of day (`00:00:00.000`). |
|
|
228
|
+
| `getEndOfYear` | End of Jalali year. |
|
|
229
|
+
| `getEndOfMonth` | End of Jalali month. |
|
|
230
|
+
| `getEndOfWeek` | End of week. |
|
|
231
|
+
| `getEndOfDay` | End of day (`23:59:59.999`). |
|
|
232
|
+
| `format(str, persian_or_options, gregorian)` | Format with tokens; hash options or positional flags. |
|
|
233
|
+
| `toISODate(calendar)` | `YYYY-MM-DD` in `"gregorian"` (default) or `"jalali"`. |
|
|
234
|
+
| `clone` | Copy with the same instant and timezone. |
|
|
235
|
+
| `toString` | Human-readable Jalali date/time string. |
|
|
236
|
+
| `toDateString` | Jalali weekday + date string. |
|
|
237
|
+
| `toLocaleDateString` | `YYYY/MM/DD` in Jalali. |
|
|
238
|
+
| `toLocaleTimeString` | 12-hour time with meridiem. |
|
|
239
|
+
|
|
43
240
|
## Test
|
|
44
241
|
|
|
45
242
|
```bash
|
|
@@ -47,6 +244,12 @@ rake test
|
|
|
47
244
|
# or: ruby -Ilib:test test/test_golden.rb
|
|
48
245
|
```
|
|
49
246
|
|
|
247
|
+
## Links
|
|
248
|
+
|
|
249
|
+
- https://jalalix.com
|
|
250
|
+
- https://github.com/jalalix/ruby
|
|
251
|
+
- [CHANGELOG.md](./CHANGELOG.md)
|
|
252
|
+
|
|
50
253
|
## License
|
|
51
254
|
|
|
52
255
|
MIT
|
data/lib/jalalix/jalalix.rb
CHANGED
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
require "time"
|
|
4
4
|
require "date"
|
|
5
5
|
|
|
6
|
-
# JalaliX
|
|
7
|
-
# Pure Ruby port of @jalalix/typescript
|
|
6
|
+
# JalaliX - Jalali (Persian / Shamsi) calendar for Ruby.
|
|
7
|
+
# Pure Ruby port of @jalalix/typescript - same algorithm and camelCase API.
|
|
8
8
|
# Timezone: system zoneinfo via ENV["TZ"] when available; embedded fixed
|
|
9
9
|
# offsets for golden zones (UTC, Asia/Tehran +03:30 since 2022) as fallback.
|
|
10
10
|
class JalaliX
|
|
11
11
|
PERSIAN_NUMBERS = %w[۰ ۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹].freeze
|
|
12
|
+
ARABIC_NUMBERS = %w[٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩].freeze
|
|
12
13
|
|
|
13
14
|
# Fixed offsets (seconds east of UTC) for zones needed without zoneinfo.
|
|
14
15
|
# Asia/Tehran abolished DST in 2022 -> permanent +03:30.
|
|
@@ -530,7 +531,7 @@ class JalaliX
|
|
|
530
531
|
|
|
531
532
|
def self.parseJalali(value, options = nil)
|
|
532
533
|
normalized = fromPersian(value.to_s).strip
|
|
533
|
-
match = normalized.match(/\A(\d{4})[
|
|
534
|
+
match = normalized.match(/\A(\d{4})[\/\-.](\d{1,2})[\/\-.](\d{1,2})(?:[ T](\d{1,2}):(\d{1,2})(?::(\d{1,2}))?)?\z/)
|
|
534
535
|
raise ArgumentError, "JalaliX.parseJalali: invalid Jalali date \"#{value}\"" unless match
|
|
535
536
|
|
|
536
537
|
year = match[1].to_i
|
|
@@ -557,12 +558,117 @@ class JalaliX
|
|
|
557
558
|
out
|
|
558
559
|
end
|
|
559
560
|
|
|
561
|
+
# Converts Persian (۰-۹) and Arabic-Indic (٠-٩) numerals to Western numerals (0-9).
|
|
560
562
|
def self.fromPersian(str)
|
|
561
563
|
out = str.dup
|
|
562
|
-
10.times
|
|
564
|
+
10.times do |i|
|
|
565
|
+
out = out.gsub(PERSIAN_NUMBERS[i], i.to_s)
|
|
566
|
+
out = out.gsub(ARABIC_NUMBERS[i], i.to_s)
|
|
567
|
+
end
|
|
563
568
|
out
|
|
564
569
|
end
|
|
565
570
|
|
|
571
|
+
# Soft-normalizes loose query / form date inputs into a Gregorian "Y-m-d H:i:s" string.
|
|
572
|
+
#
|
|
573
|
+
# Handles empty/sentinel values (nil, "undefined", "Invalid Date", "NaN"), unix timestamps
|
|
574
|
+
# (seconds or milliseconds), Persian/Arabic digits, "/" "-" "." separators, fractional seconds,
|
|
575
|
+
# optional "Z" / offset suffixes, and auto-detects Jalali vs Gregorian by year band: years in
|
|
576
|
+
# +jalaliYearMin..jalaliYearMax+ (default 1..1899) are treated as Jalali when the civil date is
|
|
577
|
+
# valid; otherwise Gregorian.
|
|
578
|
+
#
|
|
579
|
+
# Invalid or unrecognized values are returned cleaned (digits normalized) rather than raising;
|
|
580
|
+
# sentinels and non-positive timestamps return nil. Non-string / non-numeric values pass through.
|
|
581
|
+
def self.normalizeQueryDate(value, options = nil)
|
|
582
|
+
return nil if value.nil?
|
|
583
|
+
|
|
584
|
+
if value.is_a?(String)
|
|
585
|
+
is_invalid_date = value.casecmp("Invalid Date").zero? || value.match?(/NaN/i)
|
|
586
|
+
return nil if value == "null" || value == "" || value == "undefined" || is_invalid_date
|
|
587
|
+
end
|
|
588
|
+
|
|
589
|
+
if value.is_a?(Integer) || value.is_a?(Float)
|
|
590
|
+
return normalize_query_timestamp(value.to_i, options)
|
|
591
|
+
end
|
|
592
|
+
|
|
593
|
+
if value.is_a?(String)
|
|
594
|
+
trimmed = value.strip
|
|
595
|
+
numeric = parse_numeric(trimmed)
|
|
596
|
+
return normalize_query_timestamp(numeric, options) unless numeric.nil?
|
|
597
|
+
end
|
|
598
|
+
|
|
599
|
+
return value unless value.is_a?(String)
|
|
600
|
+
|
|
601
|
+
digits = fromPersian(value)
|
|
602
|
+
digits = digits.gsub(/(\d{2}:\d{2}:\d{2})\.\d+/, '\1')
|
|
603
|
+
|
|
604
|
+
match = digits.match(
|
|
605
|
+
/\A(\d{4})[\/\-.](\d{1,2})[\/\-.](\d{1,2})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2}))?(?:Z|[+-]\d{2}:?\d{2})?)?/
|
|
606
|
+
)
|
|
607
|
+
return digits unless match
|
|
608
|
+
|
|
609
|
+
year = match[1].to_i
|
|
610
|
+
month = match[2].to_i
|
|
611
|
+
day = match[3].to_i
|
|
612
|
+
hour = (match[4] || 0).to_i
|
|
613
|
+
minute = (match[5] || 0).to_i
|
|
614
|
+
second = (match[6] || 0).to_i
|
|
615
|
+
|
|
616
|
+
jalali_year_min = option_value(options, :jalaliYearMin) || 1
|
|
617
|
+
jalali_year_max = option_value(options, :jalaliYearMax) || 1899
|
|
618
|
+
|
|
619
|
+
if year >= jalali_year_min && year <= jalali_year_max && valid_jalali_civil_date?(year, month, day)
|
|
620
|
+
begin
|
|
621
|
+
return parseJalali(format("%04d/%02d/%02d %02d:%02d:%02d", year, month, day, hour, minute, second), options)
|
|
622
|
+
.format("YYYY-MM-DD HH:mm:ss", calendar: "gregorian")
|
|
623
|
+
rescue ArgumentError
|
|
624
|
+
# fall through to Gregorian / cleaned
|
|
625
|
+
end
|
|
626
|
+
end
|
|
627
|
+
|
|
628
|
+
return format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second) if valid_gregorian_date?(year, month, day)
|
|
629
|
+
|
|
630
|
+
digits
|
|
631
|
+
end
|
|
632
|
+
|
|
633
|
+
def self.normalize_query_timestamp(timestamp, options)
|
|
634
|
+
timestamp = timestamp / 1000 if timestamp > 9_999_999_999
|
|
635
|
+
return nil if timestamp <= 0
|
|
636
|
+
|
|
637
|
+
create(timestamp * 1000, options).format("YYYY-MM-DD HH:mm:ss", calendar: "gregorian")
|
|
638
|
+
end
|
|
639
|
+
private_class_method :normalize_query_timestamp
|
|
640
|
+
|
|
641
|
+
def self.valid_jalali_civil_date?(year, month, day)
|
|
642
|
+
return false if year < 1 || month < 1 || month > 12 || day < 1
|
|
643
|
+
return day <= 31 if month <= 6
|
|
644
|
+
return day <= 30 if month <= 11
|
|
645
|
+
|
|
646
|
+
leap = new(0).check_leap_year(year)
|
|
647
|
+
day <= (leap ? 30 : 29)
|
|
648
|
+
end
|
|
649
|
+
private_class_method :valid_jalali_civil_date?
|
|
650
|
+
|
|
651
|
+
def self.valid_gregorian_date?(year, month, day)
|
|
652
|
+
return false if year < 1 || month < 1 || month > 12 || day < 1
|
|
653
|
+
|
|
654
|
+
Date.valid_date?(year, month, day)
|
|
655
|
+
end
|
|
656
|
+
private_class_method :valid_gregorian_date?
|
|
657
|
+
|
|
658
|
+
def self.parse_numeric(str)
|
|
659
|
+
return nil unless str.match?(/\A[+-]?\d+(\.\d+)?\z/)
|
|
660
|
+
|
|
661
|
+
str.to_f.to_i
|
|
662
|
+
end
|
|
663
|
+
private_class_method :parse_numeric
|
|
664
|
+
|
|
665
|
+
def self.option_value(options, key)
|
|
666
|
+
return nil unless options.is_a?(Hash)
|
|
667
|
+
|
|
668
|
+
options[key] || options[key.to_s]
|
|
669
|
+
end
|
|
670
|
+
private_class_method :option_value
|
|
671
|
+
|
|
566
672
|
def self.compare(date, comparing)
|
|
567
673
|
date.getTime < comparing.getTime
|
|
568
674
|
end
|
data/lib/jalalix/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jalalix
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Hossein Maklavani
|
|
@@ -9,7 +9,7 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
|
-
description: Pure Ruby port of JalaliX
|
|
12
|
+
description: Pure Ruby port of JalaliX - same algorithm and camelCase API as the TypeScript
|
|
13
13
|
package.
|
|
14
14
|
email:
|
|
15
15
|
- hossein@maklavani.dev
|
|
@@ -26,7 +26,12 @@ files:
|
|
|
26
26
|
homepage: https://jalalix.com
|
|
27
27
|
licenses:
|
|
28
28
|
- MIT
|
|
29
|
-
metadata:
|
|
29
|
+
metadata:
|
|
30
|
+
homepage_uri: https://jalalix.com
|
|
31
|
+
source_code_uri: https://github.com/jalalix/ruby
|
|
32
|
+
changelog_uri: https://github.com/jalalix/ruby/blob/main/CHANGELOG.md
|
|
33
|
+
bug_tracker_uri: https://github.com/jalalix/ruby/issues
|
|
34
|
+
funding_uri: https://opencollective.com/jalalix
|
|
30
35
|
rdoc_options: []
|
|
31
36
|
require_paths:
|
|
32
37
|
- lib
|