@bevelo/date 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +35 -2
- package/dist/civil-GPupvlwZ.d.cts +116 -0
- package/dist/civil-GPupvlwZ.d.ts +116 -0
- package/dist/civil-QU5EukOm.cjs +461 -0
- package/dist/civil-SGLjw0gL.js +366 -0
- package/dist/index.cjs +506 -0
- package/dist/index.d.cts +302 -0
- package/dist/index.d.ts +302 -0
- package/dist/index.js +449 -0
- package/dist/pattern.cjs +328 -0
- package/dist/pattern.d.cts +75 -0
- package/dist/pattern.d.ts +75 -0
- package/dist/pattern.js +323 -0
- package/dist/timezone-BmJA-u4N.js +417 -0
- package/dist/timezone-DiMSaNmI.cjs +560 -0
- package/dist/timezone.cjs +10 -0
- package/dist/timezone.d.cts +108 -0
- package/dist/timezone.d.ts +108 -0
- package/dist/timezone.js +1 -0
- package/package.json +45 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bevelo contributors
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bevelo/date
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Calendar dates as plain `{ year, month, day }` values. Arithmetic, month grids, ISO weeks,
|
|
4
|
+
inclusive ranges, `Intl` formatting with an ISO fallback, locale field order for date
|
|
5
|
+
inputs, and IANA time-zone conversions read from the runtime's own `Intl` data. No
|
|
6
|
+
dependencies, no DOM, and no reads of the host clock: a calendar passes `now` in.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
pnpm add @bevelo/date
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { addDays, formatDate, generateMonthGrid, type CivilDate } from "@bevelo/date";
|
|
18
|
+
|
|
19
|
+
const today: CivilDate = { year: 2026, month: 3, day: 5 };
|
|
20
|
+
|
|
21
|
+
formatDate(addDays(today, 30), { locale: "en-GB", style: "long" }); // "4 April 2026"
|
|
22
|
+
|
|
23
|
+
const weeks = generateMonthGrid({ month: today, weekStartsOn: 1 });
|
|
24
|
+
for (const week of weeks) {
|
|
25
|
+
console.log(week.map((cell) => (cell.inMonth ? cell.date.day : "·")).join(" "));
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Stability
|
|
30
|
+
|
|
31
|
+
Every module is `@stable` and follows semver.
|
|
32
|
+
|
|
33
|
+
## Docs
|
|
34
|
+
|
|
35
|
+
See the [date page](https://github.com/bevelo/bevelo/blob/main/apps/docs/src/content/docs/core/date.mdx)
|
|
36
|
+
in the Bevelo docs.
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/** A day in the proleptic Gregorian calendar, with no timezone attached. */
|
|
2
|
+
type CivilDate = {
|
|
3
|
+
/** Astronomical year numbering: 0 is 1 BC, -1 is 2 BC. */
|
|
4
|
+
year: number;
|
|
5
|
+
/** Month, 1 through 12. */
|
|
6
|
+
month: number;
|
|
7
|
+
/** Day of month, 1 through the month's length. */
|
|
8
|
+
day: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A wall-clock time of day, with no zone attached.
|
|
12
|
+
*
|
|
13
|
+
* Seconds are 0..59. Leap seconds are not representable on purpose: a UI
|
|
14
|
+
* calendar never needs 23:59:60, and accepting it would make every operation
|
|
15
|
+
* on this type conditionally 60-safe.
|
|
16
|
+
*/
|
|
17
|
+
type CivilTime = {
|
|
18
|
+
hour: number;
|
|
19
|
+
minute: number;
|
|
20
|
+
second: number;
|
|
21
|
+
millisecond: number;
|
|
22
|
+
};
|
|
23
|
+
/** A civil date and a civil time; still no instant and no zone. */
|
|
24
|
+
type CivilDateTime = {
|
|
25
|
+
date: CivilDate;
|
|
26
|
+
time: CivilTime;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Sunday-first weekday index. Exported from ./civil rather than ./calendar so
|
|
30
|
+
* the arithmetic and calendar modules can share it without a runtime cycle;
|
|
31
|
+
* ./calendar re-exports it for discoverability.
|
|
32
|
+
*/
|
|
33
|
+
type Weekday = 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
34
|
+
/**
|
|
35
|
+
* The number of whole days from 1970-01-01 to `date`; negative before the
|
|
36
|
+
* epoch.
|
|
37
|
+
*
|
|
38
|
+
* Howard Hinnant's `days_from_civil`: shift January/February to the end of the
|
|
39
|
+
* previous year so the leap day is the last day of the shifted year, then count
|
|
40
|
+
* whole 400-year eras (146097 days, an exact cycle of the Gregorian calendar).
|
|
41
|
+
* Using Math.floor instead of C++ integer truncation is what makes it work for
|
|
42
|
+
* negative years; a truncating version drifts by one day before year 0.
|
|
43
|
+
*
|
|
44
|
+
* @throws RangeError when `date` is not a valid civil date.
|
|
45
|
+
*/
|
|
46
|
+
declare function toEpochDay(date: CivilDate): number;
|
|
47
|
+
/**
|
|
48
|
+
* The inverse of `toEpochDay`.
|
|
49
|
+
*
|
|
50
|
+
* Hinnant's `civil_from_days`. Correct for every day the civil calendar can
|
|
51
|
+
* name, including pre-1970 and far-future dates that a Date object cannot hold.
|
|
52
|
+
*
|
|
53
|
+
* @throws RangeError when `day` is not a safe integer.
|
|
54
|
+
*/
|
|
55
|
+
declare function fromEpochDay(day: number): CivilDate;
|
|
56
|
+
/**
|
|
57
|
+
* Narrows an unknown value to a CivilDate.
|
|
58
|
+
*
|
|
59
|
+
* The guard is the package boundary: a component reading `<input type="date">`
|
|
60
|
+
* or a JSON payload can validate before handing the value to arithmetic.
|
|
61
|
+
*/
|
|
62
|
+
declare function isValidDate(value: unknown): value is CivilDate;
|
|
63
|
+
/**
|
|
64
|
+
* Number of days in a month.
|
|
65
|
+
*
|
|
66
|
+
* @throws RangeError when `year` is not an integer or `month` is not 1..12.
|
|
67
|
+
*/
|
|
68
|
+
declare function daysInMonth(year: number, month: number): number;
|
|
69
|
+
/**
|
|
70
|
+
* Proleptic Gregorian leap year.
|
|
71
|
+
*
|
|
72
|
+
* @throws RangeError when `year` is not an integer.
|
|
73
|
+
*/
|
|
74
|
+
declare function isLeapYear(year: number): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Weekday of a date, 0 (Sunday) through 6 (Saturday).
|
|
77
|
+
*
|
|
78
|
+
* 1970-01-01 was a Thursday (4), which is the only anchor this needs; adding
|
|
79
|
+
* `% 7` twice keeps the result non-negative for pre-epoch dates.
|
|
80
|
+
*/
|
|
81
|
+
declare function dayOfWeek(date: CivilDate): Weekday;
|
|
82
|
+
/** One-based day of the year: January 1 is 1, December 31 is 365 or 366. */
|
|
83
|
+
declare function dayOfYear(date: CivilDate): number;
|
|
84
|
+
/**
|
|
85
|
+
* Reads the civil date and time of a JavaScript Date.
|
|
86
|
+
*
|
|
87
|
+
* With no `timeZone`, the host local zone is used (what a date picker in that
|
|
88
|
+
* zone means by "today"). With a zone, Intl is asked for the wall clock in that
|
|
89
|
+
* zone, which is DST-correct and needs no offset bookkeeping.
|
|
90
|
+
*
|
|
91
|
+
* @throws RangeError when the Date is invalid, when the timezone is unknown,
|
|
92
|
+
* or when a timezone is requested in a runtime without Intl.
|
|
93
|
+
*/
|
|
94
|
+
declare function fromDate(date: Date, timeZone?: string): CivilDateTime;
|
|
95
|
+
/**
|
|
96
|
+
* Creates the instant a civil date-time names.
|
|
97
|
+
*
|
|
98
|
+
* With no `timeZone`, this is the host local zone. The platform applies the
|
|
99
|
+
* ECMAScript "compatible" disambiguation: an ambiguous local time (the repeated
|
|
100
|
+
* hour when DST ends) resolves to the earlier instant, and a nonexistent one
|
|
101
|
+
* (the skipped hour when DST starts) shifts forward by the gap. That is exactly
|
|
102
|
+
* the policy this package documents, so the local path delegates instead of
|
|
103
|
+
* re-implementing it.
|
|
104
|
+
*
|
|
105
|
+
* With a `timeZone`, the offset is resolved from the zone database via Intl:
|
|
106
|
+
* candidate instants are derived from the offsets around the target, the ones
|
|
107
|
+
* whose wall clock matches are kept (ambiguous -> earliest), and if none match
|
|
108
|
+
* the time does not exist (nonexistent -> the forward-shifted candidate, per
|
|
109
|
+
* the same policy).
|
|
110
|
+
*
|
|
111
|
+
* @throws RangeError when the civil date-time is invalid or no valid Date can
|
|
112
|
+
* represent it.
|
|
113
|
+
*/
|
|
114
|
+
declare function toDate(civil: CivilDate, timeZone?: string): Date;
|
|
115
|
+
declare function toDate(civil: CivilDateTime, timeZone?: string): Date;
|
|
116
|
+
export { dayOfWeek as a, fromDate as c, isValidDate as d, toDate as f, Weekday as i, fromEpochDay as l, CivilDateTime as n, dayOfYear as o, toEpochDay as p, CivilTime as r, daysInMonth as s, CivilDate as t, isLeapYear as u };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/** A day in the proleptic Gregorian calendar, with no timezone attached. */
|
|
2
|
+
type CivilDate = {
|
|
3
|
+
/** Astronomical year numbering: 0 is 1 BC, -1 is 2 BC. */
|
|
4
|
+
year: number;
|
|
5
|
+
/** Month, 1 through 12. */
|
|
6
|
+
month: number;
|
|
7
|
+
/** Day of month, 1 through the month's length. */
|
|
8
|
+
day: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A wall-clock time of day, with no zone attached.
|
|
12
|
+
*
|
|
13
|
+
* Seconds are 0..59. Leap seconds are not representable on purpose: a UI
|
|
14
|
+
* calendar never needs 23:59:60, and accepting it would make every operation
|
|
15
|
+
* on this type conditionally 60-safe.
|
|
16
|
+
*/
|
|
17
|
+
type CivilTime = {
|
|
18
|
+
hour: number;
|
|
19
|
+
minute: number;
|
|
20
|
+
second: number;
|
|
21
|
+
millisecond: number;
|
|
22
|
+
};
|
|
23
|
+
/** A civil date and a civil time; still no instant and no zone. */
|
|
24
|
+
type CivilDateTime = {
|
|
25
|
+
date: CivilDate;
|
|
26
|
+
time: CivilTime;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Sunday-first weekday index. Exported from ./civil rather than ./calendar so
|
|
30
|
+
* the arithmetic and calendar modules can share it without a runtime cycle;
|
|
31
|
+
* ./calendar re-exports it for discoverability.
|
|
32
|
+
*/
|
|
33
|
+
type Weekday = 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
34
|
+
/**
|
|
35
|
+
* The number of whole days from 1970-01-01 to `date`; negative before the
|
|
36
|
+
* epoch.
|
|
37
|
+
*
|
|
38
|
+
* Howard Hinnant's `days_from_civil`: shift January/February to the end of the
|
|
39
|
+
* previous year so the leap day is the last day of the shifted year, then count
|
|
40
|
+
* whole 400-year eras (146097 days, an exact cycle of the Gregorian calendar).
|
|
41
|
+
* Using Math.floor instead of C++ integer truncation is what makes it work for
|
|
42
|
+
* negative years; a truncating version drifts by one day before year 0.
|
|
43
|
+
*
|
|
44
|
+
* @throws RangeError when `date` is not a valid civil date.
|
|
45
|
+
*/
|
|
46
|
+
declare function toEpochDay(date: CivilDate): number;
|
|
47
|
+
/**
|
|
48
|
+
* The inverse of `toEpochDay`.
|
|
49
|
+
*
|
|
50
|
+
* Hinnant's `civil_from_days`. Correct for every day the civil calendar can
|
|
51
|
+
* name, including pre-1970 and far-future dates that a Date object cannot hold.
|
|
52
|
+
*
|
|
53
|
+
* @throws RangeError when `day` is not a safe integer.
|
|
54
|
+
*/
|
|
55
|
+
declare function fromEpochDay(day: number): CivilDate;
|
|
56
|
+
/**
|
|
57
|
+
* Narrows an unknown value to a CivilDate.
|
|
58
|
+
*
|
|
59
|
+
* The guard is the package boundary: a component reading `<input type="date">`
|
|
60
|
+
* or a JSON payload can validate before handing the value to arithmetic.
|
|
61
|
+
*/
|
|
62
|
+
declare function isValidDate(value: unknown): value is CivilDate;
|
|
63
|
+
/**
|
|
64
|
+
* Number of days in a month.
|
|
65
|
+
*
|
|
66
|
+
* @throws RangeError when `year` is not an integer or `month` is not 1..12.
|
|
67
|
+
*/
|
|
68
|
+
declare function daysInMonth(year: number, month: number): number;
|
|
69
|
+
/**
|
|
70
|
+
* Proleptic Gregorian leap year.
|
|
71
|
+
*
|
|
72
|
+
* @throws RangeError when `year` is not an integer.
|
|
73
|
+
*/
|
|
74
|
+
declare function isLeapYear(year: number): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Weekday of a date, 0 (Sunday) through 6 (Saturday).
|
|
77
|
+
*
|
|
78
|
+
* 1970-01-01 was a Thursday (4), which is the only anchor this needs; adding
|
|
79
|
+
* `% 7` twice keeps the result non-negative for pre-epoch dates.
|
|
80
|
+
*/
|
|
81
|
+
declare function dayOfWeek(date: CivilDate): Weekday;
|
|
82
|
+
/** One-based day of the year: January 1 is 1, December 31 is 365 or 366. */
|
|
83
|
+
declare function dayOfYear(date: CivilDate): number;
|
|
84
|
+
/**
|
|
85
|
+
* Reads the civil date and time of a JavaScript Date.
|
|
86
|
+
*
|
|
87
|
+
* With no `timeZone`, the host local zone is used (what a date picker in that
|
|
88
|
+
* zone means by "today"). With a zone, Intl is asked for the wall clock in that
|
|
89
|
+
* zone, which is DST-correct and needs no offset bookkeeping.
|
|
90
|
+
*
|
|
91
|
+
* @throws RangeError when the Date is invalid, when the timezone is unknown,
|
|
92
|
+
* or when a timezone is requested in a runtime without Intl.
|
|
93
|
+
*/
|
|
94
|
+
declare function fromDate(date: Date, timeZone?: string): CivilDateTime;
|
|
95
|
+
/**
|
|
96
|
+
* Creates the instant a civil date-time names.
|
|
97
|
+
*
|
|
98
|
+
* With no `timeZone`, this is the host local zone. The platform applies the
|
|
99
|
+
* ECMAScript "compatible" disambiguation: an ambiguous local time (the repeated
|
|
100
|
+
* hour when DST ends) resolves to the earlier instant, and a nonexistent one
|
|
101
|
+
* (the skipped hour when DST starts) shifts forward by the gap. That is exactly
|
|
102
|
+
* the policy this package documents, so the local path delegates instead of
|
|
103
|
+
* re-implementing it.
|
|
104
|
+
*
|
|
105
|
+
* With a `timeZone`, the offset is resolved from the zone database via Intl:
|
|
106
|
+
* candidate instants are derived from the offsets around the target, the ones
|
|
107
|
+
* whose wall clock matches are kept (ambiguous -> earliest), and if none match
|
|
108
|
+
* the time does not exist (nonexistent -> the forward-shifted candidate, per
|
|
109
|
+
* the same policy).
|
|
110
|
+
*
|
|
111
|
+
* @throws RangeError when the civil date-time is invalid or no valid Date can
|
|
112
|
+
* represent it.
|
|
113
|
+
*/
|
|
114
|
+
declare function toDate(civil: CivilDate, timeZone?: string): Date;
|
|
115
|
+
declare function toDate(civil: CivilDateTime, timeZone?: string): Date;
|
|
116
|
+
export { dayOfWeek as a, fromDate as c, isValidDate as d, toDate as f, Weekday as i, fromEpochDay as l, CivilDateTime as n, dayOfYear as o, toEpochDay as p, CivilTime as r, daysInMonth as s, CivilDate as t, isLeapYear as u };
|