date-vir 0.0.0 → 0.0.1
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-CC0 +121 -0
- package/LICENSE-MIT +21 -0
- package/README.md +148 -0
- package/dist/cjs/full-date/create-full-date.js +67 -0
- package/dist/cjs/full-date/date-like.js +2 -0
- package/dist/cjs/full-date/format-presets.js +32 -0
- package/dist/cjs/full-date/full-date-shape.js +33 -0
- package/dist/cjs/full-date/is-valid-full-date.js +24 -0
- package/dist/cjs/full-date/luxon-date-time-conversion.js +43 -0
- package/dist/cjs/full-date/parsing.js +43 -0
- package/dist/cjs/full-date/primitive-conversions.js +74 -0
- package/dist/cjs/full-date/string-format-types.js +2 -0
- package/dist/cjs/index.js +29 -0
- package/dist/cjs/locales.js +5 -0
- package/dist/cjs/timezone/timezone-checks.js +34 -0
- package/dist/cjs/timezone/timezone-names.js +479 -0
- package/dist/cjs/timezone/timezones.js +14 -0
- package/dist/full-date/create-full-date.d.ts +20 -0
- package/dist/full-date/create-full-date.js +67 -0
- package/dist/full-date/date-like.d.ts +4 -0
- package/dist/full-date/date-like.js +2 -0
- package/dist/full-date/format-presets.d.ts +28 -0
- package/dist/full-date/format-presets.js +32 -0
- package/dist/full-date/full-date-shape.d.ts +65 -0
- package/dist/full-date/full-date-shape.js +33 -0
- package/dist/full-date/is-valid-full-date.d.ts +3 -0
- package/dist/full-date/is-valid-full-date.js +24 -0
- package/dist/full-date/luxon-date-time-conversion.d.ts +14 -0
- package/dist/full-date/luxon-date-time-conversion.js +43 -0
- package/dist/full-date/parsing.d.ts +27 -0
- package/dist/full-date/parsing.js +43 -0
- package/dist/full-date/primitive-conversions.d.ts +42 -0
- package/dist/full-date/primitive-conversions.js +74 -0
- package/dist/full-date/string-format-types.d.ts +4 -0
- package/dist/full-date/string-format-types.js +2 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +29 -0
- package/dist/locales.d.ts +1 -0
- package/dist/locales.js +5 -0
- package/dist/timezone/timezone-checks.d.ts +17 -0
- package/dist/timezone/timezone-checks.js +34 -0
- package/dist/timezone/timezone-names.d.ts +9 -0
- package/dist/timezone/timezone-names.js +479 -0
- package/dist/timezone/timezones.d.ts +479 -0
- package/dist/timezone/timezones.js +14 -0
- package/package.json +72 -10
package/LICENSE-CC0
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
CC0 1.0 Universal
|
|
2
|
+
|
|
3
|
+
Creative Commons Legal Code
|
|
4
|
+
|
|
5
|
+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
6
|
+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
7
|
+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
8
|
+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
9
|
+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
10
|
+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
11
|
+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
12
|
+
HEREUNDER.
|
|
13
|
+
|
|
14
|
+
Statement of Purpose
|
|
15
|
+
|
|
16
|
+
The laws of most jurisdictions throughout the world automatically confer
|
|
17
|
+
exclusive Copyright and Related Rights (defined below) upon the creator
|
|
18
|
+
and subsequent owner(s) (each and all, an "owner") of an original work of
|
|
19
|
+
authorship and/or a database (each, a "Work").
|
|
20
|
+
|
|
21
|
+
Certain owners wish to permanently relinquish those rights to a Work for
|
|
22
|
+
the purpose of contributing to a commons of creative, cultural and
|
|
23
|
+
scientific works ("Commons") that the public can reliably and without fear
|
|
24
|
+
of later claims of infringement build upon, modify, incorporate in other
|
|
25
|
+
works, reuse and redistribute as freely as possible in any form whatsoever
|
|
26
|
+
and for any purposes, including without limitation commercial purposes.
|
|
27
|
+
These owners may contribute to the Commons to promote the ideal of a free
|
|
28
|
+
culture and the further production of creative, cultural and scientific
|
|
29
|
+
works, or to gain reputation or greater distribution for their Work in
|
|
30
|
+
part through the use and efforts of others.
|
|
31
|
+
|
|
32
|
+
For these and/or other purposes and motivations, and without any
|
|
33
|
+
expectation of additional consideration or compensation, the person
|
|
34
|
+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
|
35
|
+
is an owner of Copyright and Related Rights in the Work, voluntarily
|
|
36
|
+
elects to apply CC0 to the Work and publicly distribute the Work under its
|
|
37
|
+
terms, with knowledge of his or her Copyright and Related Rights in the
|
|
38
|
+
Work and the meaning and intended legal effect of CC0 on those rights.
|
|
39
|
+
|
|
40
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
41
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
|
42
|
+
Related Rights"). Copyright and Related Rights include, but are not
|
|
43
|
+
limited to, the following:
|
|
44
|
+
|
|
45
|
+
i. the right to reproduce, adapt, distribute, perform, display,
|
|
46
|
+
communicate, and translate a Work;
|
|
47
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
48
|
+
iii. publicity and privacy rights pertaining to a person's image or
|
|
49
|
+
likeness depicted in a Work;
|
|
50
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
|
51
|
+
subject to the limitations in paragraph 4(a), below;
|
|
52
|
+
v. rights protecting the extraction, dissemination, use and reuse of data
|
|
53
|
+
in a Work;
|
|
54
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
55
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
|
56
|
+
protection of databases, and under any national implementation
|
|
57
|
+
thereof, including any amended or successor version of such
|
|
58
|
+
directive); and
|
|
59
|
+
vii. other similar, equivalent or corresponding rights throughout the
|
|
60
|
+
world based on applicable law or treaty, and any national
|
|
61
|
+
implementations thereof.
|
|
62
|
+
|
|
63
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention
|
|
64
|
+
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
|
65
|
+
irrevocably and unconditionally waives, abandons, and surrenders all of
|
|
66
|
+
Affirmer's Copyright and Related Rights and associated claims and causes
|
|
67
|
+
of action, whether now known or unknown (including existing as well as
|
|
68
|
+
future claims and causes of action), in the Work (i) in all territories
|
|
69
|
+
worldwide, (ii) for the maximum duration provided by applicable law or
|
|
70
|
+
treaty (including future time extensions), (iii) in any current or future
|
|
71
|
+
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
|
72
|
+
including without limitation commercial, advertising or promotional
|
|
73
|
+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
|
74
|
+
member of the public at large and to the detriment of Affirmer's heirs and
|
|
75
|
+
successors, fully intending that such Waiver shall not be subject to
|
|
76
|
+
revocation, rescission, cancellation, termination, or any other legal or
|
|
77
|
+
equitable action to disrupt the quiet enjoyment of the Work by the public
|
|
78
|
+
as contemplated by Affirmer's express Statement of Purpose.
|
|
79
|
+
|
|
80
|
+
3. Public License Fallback. Should any part of the Waiver for any reason
|
|
81
|
+
be judged legally invalid or ineffective under applicable law, then the
|
|
82
|
+
Waiver shall be preserved to the maximum extent permitted taking into
|
|
83
|
+
account Affirmer's express Statement of Purpose. In addition, to the
|
|
84
|
+
extent the Waiver is so judged Affirmer hereby grants to each affected
|
|
85
|
+
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
|
86
|
+
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
|
87
|
+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
|
88
|
+
maximum duration provided by applicable law or treaty (including future
|
|
89
|
+
time extensions), (iii) in any current or future medium and for any number
|
|
90
|
+
of copies, and (iv) for any purpose whatsoever, including without
|
|
91
|
+
limitation commercial, advertising or promotional purposes (the
|
|
92
|
+
"License"). The License shall be deemed effective as of the date CC0 was
|
|
93
|
+
applied by Affirmer to the Work. Should any part of the License for any
|
|
94
|
+
reason be judged legally invalid or ineffective under applicable law, such
|
|
95
|
+
partial invalidity or ineffectiveness shall not invalidate the remainder
|
|
96
|
+
of the License, and in such case Affirmer hereby affirms that he or she
|
|
97
|
+
will not (i) exercise any of his or her remaining Copyright and Related
|
|
98
|
+
Rights in the Work or (ii) assert any associated claims and causes of
|
|
99
|
+
action with respect to the Work, in either case contrary to Affirmer's
|
|
100
|
+
express Statement of Purpose.
|
|
101
|
+
|
|
102
|
+
4. Limitations and Disclaimers.
|
|
103
|
+
|
|
104
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
105
|
+
surrendered, licensed or otherwise affected by this document.
|
|
106
|
+
b. Affirmer offers the Work as-is and makes no representations or
|
|
107
|
+
warranties of any kind concerning the Work, express, implied,
|
|
108
|
+
statutory or otherwise, including without limitation warranties of
|
|
109
|
+
title, merchantability, fitness for a particular purpose, non
|
|
110
|
+
infringement, or the absence of latent or other defects, accuracy, or
|
|
111
|
+
the present or absence of errors, whether or not discoverable, all to
|
|
112
|
+
the greatest extent permissible under applicable law.
|
|
113
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
114
|
+
that may apply to the Work or any use thereof, including without
|
|
115
|
+
limitation any person's Copyright and Related Rights in the Work.
|
|
116
|
+
Further, Affirmer disclaims responsibility for obtaining any necessary
|
|
117
|
+
consents, permissions or other rights required for any use of the
|
|
118
|
+
Work.
|
|
119
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
120
|
+
party to this document and has no duty or obligation with respect to
|
|
121
|
+
this CC0 or use of the Work.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 electrovir
|
|
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
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# date-vir
|
|
2
|
+
|
|
3
|
+
Easy dates and times with explicit timezones (and typed timezones).
|
|
4
|
+
|
|
5
|
+
`date-vir` revolves around the new `FullDate` type, which includes all the information you need to correctly pinpoint _and_ format a given point in time. Most notably, `FullDate` always includes a `timezone` property, so you _always_ know what timezone the date was originally defined in, or what timezone the date is intended to be used in:
|
|
6
|
+
|
|
7
|
+
<!-- example-link: src/readme-examples/full-date.example.ts -->
|
|
8
|
+
|
|
9
|
+
```TypeScript
|
|
10
|
+
import {FullDate, timezones} from 'date-vir';
|
|
11
|
+
|
|
12
|
+
const myDate: FullDate = {
|
|
13
|
+
year: 2023,
|
|
14
|
+
month: 6,
|
|
15
|
+
day: 5,
|
|
16
|
+
|
|
17
|
+
hour: 5,
|
|
18
|
+
minute: 23,
|
|
19
|
+
second: 27,
|
|
20
|
+
millisecond: 652,
|
|
21
|
+
|
|
22
|
+
timezone: timezones.UTC,
|
|
23
|
+
};
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
# Creating a FullDate
|
|
27
|
+
|
|
28
|
+
Several functions are available for creating `FullDate` objects, with `createFullDate` being the most commonly used:
|
|
29
|
+
|
|
30
|
+
<!-- example-link: src/readme-examples/full-date-creation.example.ts -->
|
|
31
|
+
|
|
32
|
+
```TypeScript
|
|
33
|
+
import {
|
|
34
|
+
createFullDate,
|
|
35
|
+
getNowFullDate,
|
|
36
|
+
parseInputElementValue,
|
|
37
|
+
parseStrangeString,
|
|
38
|
+
timezones,
|
|
39
|
+
} from 'date-vir';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates a FullDate from a wide range of possible inputs. See TypeScript types for full details on
|
|
43
|
+
* available inputs.
|
|
44
|
+
*/
|
|
45
|
+
createFullDate('2023-06-05', timezones['Europe/Rome']);
|
|
46
|
+
|
|
47
|
+
/** Get the current date and time right now in the given timezone. */
|
|
48
|
+
getNowFullDate(timezones['America/Argentina/Buenos_Aires']);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Parsed the value of an <input> element directly from the element itself. This is intended to be
|
|
52
|
+
* used for type="date" or type="time" <input> elements, but any input element with a valid date or
|
|
53
|
+
* time string will work.
|
|
54
|
+
*/
|
|
55
|
+
parseInputElementValue(document.querySelector('input'), timezones['Asia/Tokyo']);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* If you have a really oddly formatted date/time string and createFullDate does not suffice, you
|
|
59
|
+
* can have full parsing control using this function. Luxon is used under the hood for this parsing,
|
|
60
|
+
* so you can see all the formatString options in its docs:
|
|
61
|
+
* https://moment.github.io/luxon/#/parsing?id=table-of-tokens
|
|
62
|
+
*/
|
|
63
|
+
parseStrangeString({
|
|
64
|
+
dateString: '870-0-14-19 5 2023 6',
|
|
65
|
+
formatString: 'S-s-h-m d yyyy M',
|
|
66
|
+
timezone: timezones['America/Cancun'],
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
# Outputs
|
|
71
|
+
|
|
72
|
+
`FullDate` objects are easily converted into other formats (usually strings) with the included functions:
|
|
73
|
+
|
|
74
|
+
<!-- example-link: src/readme-examples/full-date-conversions.example.ts -->
|
|
75
|
+
|
|
76
|
+
```TypeScript
|
|
77
|
+
import {
|
|
78
|
+
HtmlInputElementTypeEnum,
|
|
79
|
+
createFullDate,
|
|
80
|
+
formatPresets,
|
|
81
|
+
timezones,
|
|
82
|
+
toHtmlInputString,
|
|
83
|
+
toIsoString,
|
|
84
|
+
toLocaleString,
|
|
85
|
+
toTimestamp,
|
|
86
|
+
} from 'date-vir';
|
|
87
|
+
|
|
88
|
+
const myFullDate = createFullDate('2023-06-05T14:19:00.870Z', timezones['America/Chicago']);
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Converts the given FullDate into a UTC unix timestamp with milliseconds. Since the output is a
|
|
92
|
+
* UTC timestamp, the number will be the same even if you convert your FullDate to a different time
|
|
93
|
+
* zone.
|
|
94
|
+
*/
|
|
95
|
+
toTimestamp(myFullDate); // returns 1685974740870
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Converts the given FullDate into a UTC ISO string. Since the output is in UTC, even if your
|
|
99
|
+
* FullDate object is shifted to a different timezone, the output of this function will be the
|
|
100
|
+
* same.
|
|
101
|
+
*/
|
|
102
|
+
toIsoString(myFullDate); // returns '2023-06-05T14:19:00.870Z'
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Converts the given FullDate to a locale-based formatted string. The second argument, the options
|
|
106
|
+
* argument, is optional and contains many options therein.
|
|
107
|
+
*/
|
|
108
|
+
toLocaleString(myFullDate, formatPresets.DatetimeFull); // returns 'June 5, 2023 at 9:19 AM CDT' in the en-us locale
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Formats the given FullDate to a string so that it is ready to be assigned to the value attribute
|
|
112
|
+
* of an <input type="date" /> element.
|
|
113
|
+
*/
|
|
114
|
+
toHtmlInputString(myFullDate, HtmlInputElementTypeEnum.Date); // returns '2023-06-05'
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
# Timezone
|
|
118
|
+
|
|
119
|
+
`date-vir` exposes a type-safe list of timezone names, including shortcuts for the user's timezone and the UTC timezone:
|
|
120
|
+
|
|
121
|
+
<!-- example-link: src/readme-examples/timezones.example.ts -->
|
|
122
|
+
|
|
123
|
+
```TypeScript
|
|
124
|
+
import {timezones, userTimezone, utcTimezone} from 'date-vir';
|
|
125
|
+
|
|
126
|
+
timezones['Africa/Abidjan'];
|
|
127
|
+
timezones['America/Los_Angeles'];
|
|
128
|
+
timezones['Etc/GMT+1'];
|
|
129
|
+
|
|
130
|
+
utcTimezone;
|
|
131
|
+
userTimezone;
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Note that the current list of timezone names included in this package was generated from Firefox 106.0.2.
|
|
135
|
+
|
|
136
|
+
# Luxon
|
|
137
|
+
|
|
138
|
+
The parsing and timezone conversions in `date-vir` utilize the [`luxon`](https://www.npmjs.com/luxon) package under the hood. If you wish to gain full `luxon` control over a `FullDate` object, `date-vir` provides an easy conversion function:
|
|
139
|
+
|
|
140
|
+
<!-- example-link: src/readme-examples/luxon-conversion.example.ts -->
|
|
141
|
+
|
|
142
|
+
```TypeScript
|
|
143
|
+
import {createFullDate, timezones, toLuxonDateTime} from 'date-vir';
|
|
144
|
+
|
|
145
|
+
const myFullDate = createFullDate('2023-06-05T14:19:00.870Z', timezones['America/Chicago']);
|
|
146
|
+
|
|
147
|
+
toLuxonDateTime(myFullDate);
|
|
148
|
+
```
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getNowFullDate = exports.toNewTimezone = exports.createFullDate = exports.createUtcFullDate = exports.createFullDateInUserTimezone = void 0;
|
|
4
|
+
const common_1 = require("@augment-vir/common");
|
|
5
|
+
const luxon_1 = require("luxon");
|
|
6
|
+
const timezones_1 = require("../timezone/timezones");
|
|
7
|
+
const is_valid_full_date_1 = require("./is-valid-full-date");
|
|
8
|
+
const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
|
|
9
|
+
function createFullDateInUserTimezone(dateLike) {
|
|
10
|
+
return createFullDate(dateLike, timezones_1.userTimezone);
|
|
11
|
+
}
|
|
12
|
+
exports.createFullDateInUserTimezone = createFullDateInUserTimezone;
|
|
13
|
+
function createUtcFullDate(dateLike) {
|
|
14
|
+
return createFullDate(dateLike, timezones_1.utcTimezone);
|
|
15
|
+
}
|
|
16
|
+
exports.createUtcFullDate = createUtcFullDate;
|
|
17
|
+
/**
|
|
18
|
+
* Parses the given dateLike and then reads the fullDate properties from that parsed value as if it
|
|
19
|
+
* were in the given timezone.
|
|
20
|
+
*/
|
|
21
|
+
function createFullDate(
|
|
22
|
+
/** The original date representation to be converted into a FullDate. */
|
|
23
|
+
dateLike,
|
|
24
|
+
/** The timezone that this date/time is meant for / originated from. */
|
|
25
|
+
timezone) {
|
|
26
|
+
const dateTime = convertDateLikeToLuxonDateTime(dateLike, timezone);
|
|
27
|
+
if (!dateTime || !dateTime.isValid) {
|
|
28
|
+
throw new Error(`Failed to parse date input '${dateLike}'`);
|
|
29
|
+
}
|
|
30
|
+
return (0, luxon_date_time_conversion_1.parseLuxonDateTime)(dateTime);
|
|
31
|
+
}
|
|
32
|
+
exports.createFullDate = createFullDate;
|
|
33
|
+
/**
|
|
34
|
+
* Creates and returns a new FullDate object with its values shifted to be the same original time
|
|
35
|
+
* but represented in a new timezone.
|
|
36
|
+
*/
|
|
37
|
+
function toNewTimezone(fullDate, timezone) {
|
|
38
|
+
return createFullDate(fullDate, timezone);
|
|
39
|
+
}
|
|
40
|
+
exports.toNewTimezone = toNewTimezone;
|
|
41
|
+
function convertDateLikeToLuxonDateTime(dateLike, timezone) {
|
|
42
|
+
if ((0, is_valid_full_date_1.isValidFullDate)(dateLike)) {
|
|
43
|
+
dateLike = (0, luxon_date_time_conversion_1.toLuxonDateTime)(dateLike).toMillis();
|
|
44
|
+
}
|
|
45
|
+
if (luxon_1.DateTime.isDateTime(dateLike)) {
|
|
46
|
+
return dateLike.setZone(timezone);
|
|
47
|
+
}
|
|
48
|
+
else if ((0, common_1.isRuntimeTypeOf)(dateLike, 'number')) {
|
|
49
|
+
return luxon_1.DateTime.fromMillis(dateLike, { zone: timezones_1.utcTimezone }).setZone(timezone);
|
|
50
|
+
}
|
|
51
|
+
else if ((0, common_1.isRuntimeTypeOf)(dateLike, 'string')) {
|
|
52
|
+
const dateTime = luxon_1.DateTime.fromISO(dateLike, { zone: timezone });
|
|
53
|
+
if (!dateTime.isValid) {
|
|
54
|
+
throw new Error(`Failed to parse date string '${dateLike}': unexpected string format. Please use parseStringToFullDate instead to have more control over the parsing format.`);
|
|
55
|
+
}
|
|
56
|
+
return dateTime;
|
|
57
|
+
}
|
|
58
|
+
else if (dateLike instanceof Date) {
|
|
59
|
+
const dateTime = luxon_1.DateTime.fromJSDate(dateLike);
|
|
60
|
+
return dateTime.setZone(timezone);
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
function getNowFullDate(timezone) {
|
|
65
|
+
return createFullDate(Date.now(), timezone);
|
|
66
|
+
}
|
|
67
|
+
exports.getNowFullDate = getNowFullDate;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatPresets = void 0;
|
|
4
|
+
const luxon_1 = require("luxon");
|
|
5
|
+
/**
|
|
6
|
+
* Format preset re-exported from luxon to be used with toLocaleString. To see examples of all
|
|
7
|
+
* presets, go here: https://moment.github.io/luxon/#/formatting?id=presets
|
|
8
|
+
*/
|
|
9
|
+
exports.formatPresets = {
|
|
10
|
+
DateShort: luxon_1.DateTime.DATE_SHORT,
|
|
11
|
+
DateMed: luxon_1.DateTime.DATE_MED,
|
|
12
|
+
DateMedWithWeekday: luxon_1.DateTime.DATE_MED_WITH_WEEKDAY,
|
|
13
|
+
DateFull: luxon_1.DateTime.DATE_FULL,
|
|
14
|
+
DateHuge: luxon_1.DateTime.DATE_HUGE,
|
|
15
|
+
TimeSimple: luxon_1.DateTime.TIME_SIMPLE,
|
|
16
|
+
TimeWithSeconds: luxon_1.DateTime.TIME_WITH_SECONDS,
|
|
17
|
+
TimeWithShortOffset: luxon_1.DateTime.TIME_WITH_SHORT_OFFSET,
|
|
18
|
+
TimeWithLongOffset: luxon_1.DateTime.TIME_WITH_LONG_OFFSET,
|
|
19
|
+
Time24Simple: luxon_1.DateTime.TIME_24_SIMPLE,
|
|
20
|
+
Time24WithSeconds: luxon_1.DateTime.TIME_24_WITH_SECONDS,
|
|
21
|
+
Time24WithShortOffset: luxon_1.DateTime.TIME_24_WITH_SHORT_OFFSET,
|
|
22
|
+
Time24WithLongOffset: luxon_1.DateTime.TIME_24_WITH_LONG_OFFSET,
|
|
23
|
+
DatetimeShort: luxon_1.DateTime.DATETIME_SHORT,
|
|
24
|
+
DatetimeShortWithSeconds: luxon_1.DateTime.DATETIME_SHORT_WITH_SECONDS,
|
|
25
|
+
DatetimeMed: luxon_1.DateTime.DATETIME_MED,
|
|
26
|
+
DatetimeMedWithSeconds: luxon_1.DateTime.DATETIME_MED_WITH_SECONDS,
|
|
27
|
+
DatetimeMedWithWeekday: luxon_1.DateTime.DATETIME_MED_WITH_WEEKDAY,
|
|
28
|
+
DatetimeFull: luxon_1.DateTime.DATETIME_FULL,
|
|
29
|
+
DatetimeFullWithSeconds: luxon_1.DateTime.DATETIME_FULL_WITH_SECONDS,
|
|
30
|
+
DatetimeHuge: luxon_1.DateTime.DATETIME_HUGE,
|
|
31
|
+
DatetimeHugeWithSeconds: luxon_1.DateTime.DATETIME_HUGE_WITH_SECONDS,
|
|
32
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fullDateShape = exports.dateOnlyShape = exports.timeOnlyShape = void 0;
|
|
4
|
+
const object_shape_tester_1 = require("object-shape-tester");
|
|
5
|
+
const timezones_1 = require("../timezone/timezones");
|
|
6
|
+
exports.timeOnlyShape = (0, object_shape_tester_1.defineShape)({
|
|
7
|
+
/** Hour of the day in 24 time: 0-23 */
|
|
8
|
+
hour: 14,
|
|
9
|
+
/** Minute of the hour: 0-59 */
|
|
10
|
+
minute: 19,
|
|
11
|
+
/** Second of the minute: 0-59 */
|
|
12
|
+
second: 0,
|
|
13
|
+
/** Millisecond of the second: 0-999 */
|
|
14
|
+
millisecond: 870,
|
|
15
|
+
/** The timezone that this date/time is meant for / originated from. */
|
|
16
|
+
timezone: timezones_1.utcTimezone,
|
|
17
|
+
});
|
|
18
|
+
exports.dateOnlyShape = (0, object_shape_tester_1.defineShape)({
|
|
19
|
+
/**
|
|
20
|
+
* The full, four digit year.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* 2023;
|
|
24
|
+
*/
|
|
25
|
+
year: 2023,
|
|
26
|
+
/** A month of the year: 1-12 */
|
|
27
|
+
month: 6,
|
|
28
|
+
/** A day of the month: 1-31 depending on the month */
|
|
29
|
+
day: 5,
|
|
30
|
+
/** The timezone that this date/time is meant for / originated from. */
|
|
31
|
+
timezone: timezones_1.utcTimezone,
|
|
32
|
+
});
|
|
33
|
+
exports.fullDateShape = (0, object_shape_tester_1.defineShape)((0, object_shape_tester_1.and)(exports.dateOnlyShape, exports.timeOnlyShape));
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidFullDate = exports.assertIsValidFullDate = void 0;
|
|
4
|
+
const object_shape_tester_1 = require("object-shape-tester");
|
|
5
|
+
const timezone_checks_1 = require("../timezone/timezone-checks");
|
|
6
|
+
const full_date_shape_1 = require("./full-date-shape");
|
|
7
|
+
const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
|
|
8
|
+
function assertIsValidFullDate(input) {
|
|
9
|
+
(0, object_shape_tester_1.assertValidShape)(input, full_date_shape_1.fullDateShape);
|
|
10
|
+
(0, timezone_checks_1.assertValidTimezone)(input.timezone);
|
|
11
|
+
/** Check that the input can be converted to a luxon date correctly. */
|
|
12
|
+
(0, luxon_date_time_conversion_1.toLuxonDateTime)(input);
|
|
13
|
+
}
|
|
14
|
+
exports.assertIsValidFullDate = assertIsValidFullDate;
|
|
15
|
+
function isValidFullDate(input) {
|
|
16
|
+
try {
|
|
17
|
+
assertIsValidFullDate(input);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.isValidFullDate = isValidFullDate;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseLuxonDateTime = exports.toLuxonDateTime = void 0;
|
|
4
|
+
const common_1 = require("@augment-vir/common");
|
|
5
|
+
const luxon_1 = require("luxon");
|
|
6
|
+
/**
|
|
7
|
+
* Converts a FullDate object into a Luxon DateTime library. This is only needed if you need complex
|
|
8
|
+
* operations on dates. It does, however, internally power a lot of the date-vir functionality as it
|
|
9
|
+
* uses the Luxon library for conversions between timezones.
|
|
10
|
+
*/
|
|
11
|
+
function toLuxonDateTime(fullDateInput) {
|
|
12
|
+
const dateTime = luxon_1.DateTime.fromObject((0, common_1.omitObjectKeys)(fullDateInput, ['timezone']), {
|
|
13
|
+
zone: fullDateInput.timezone,
|
|
14
|
+
});
|
|
15
|
+
/** Ignore this edge case in coverage cause idk how to trigger it. */
|
|
16
|
+
/* c8 ignore next 3 */
|
|
17
|
+
if (!dateTime.isValid) {
|
|
18
|
+
throw new Error(dateTime.invalidExplanation ?? undefined);
|
|
19
|
+
}
|
|
20
|
+
return dateTime;
|
|
21
|
+
}
|
|
22
|
+
exports.toLuxonDateTime = toLuxonDateTime;
|
|
23
|
+
/**
|
|
24
|
+
* Convert a Luxon DateTime object into a DateTime object. Used internally inside of date-vir, but
|
|
25
|
+
* could be helpful if you need it. Usually you should prefer using the createFullDate function
|
|
26
|
+
* instead.
|
|
27
|
+
*/
|
|
28
|
+
function parseLuxonDateTime(dateTimeInput) {
|
|
29
|
+
if (!dateTimeInput.isValid) {
|
|
30
|
+
throw new Error(`Invalid input: '${dateTimeInput.toISO()}'`);
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
day: dateTimeInput.day,
|
|
34
|
+
month: dateTimeInput.month,
|
|
35
|
+
year: dateTimeInput.year,
|
|
36
|
+
hour: dateTimeInput.hour,
|
|
37
|
+
minute: dateTimeInput.minute,
|
|
38
|
+
second: dateTimeInput.second,
|
|
39
|
+
millisecond: dateTimeInput.millisecond,
|
|
40
|
+
timezone: dateTimeInput.zoneName,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
exports.parseLuxonDateTime = parseLuxonDateTime;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseInputElementValue = exports.parseStrangeString = void 0;
|
|
4
|
+
const common_1 = require("@augment-vir/common");
|
|
5
|
+
const luxon_1 = require("luxon");
|
|
6
|
+
const create_full_date_1 = require("./create-full-date");
|
|
7
|
+
const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
|
|
8
|
+
/**
|
|
9
|
+
* Parses any strangely formatted dateString using the given formatString and timezone.
|
|
10
|
+
*
|
|
11
|
+
* This is much more flexible than createFullDate but createFullDate should be preferred in
|
|
12
|
+
* situations where it works.
|
|
13
|
+
*
|
|
14
|
+
* The formatString input is passed to Luxon. All format options are listed here:
|
|
15
|
+
* https://moment.github.io/luxon/#/parsing?id=table-of-tokens
|
|
16
|
+
*/
|
|
17
|
+
function parseStrangeString({ dateString, formatString, timezone, }) {
|
|
18
|
+
try {
|
|
19
|
+
const dateTime = luxon_1.DateTime.fromFormat(dateString, formatString, {
|
|
20
|
+
zone: timezone,
|
|
21
|
+
setZone: true,
|
|
22
|
+
});
|
|
23
|
+
return (0, luxon_date_time_conversion_1.parseLuxonDateTime)(dateTime);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw new Error(`Failed to parse string '${dateString}' with format '${formatString}'`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.parseStrangeString = parseStrangeString;
|
|
30
|
+
/**
|
|
31
|
+
* Read either an HTMLInputElement's value directly, or the value string retrieved from an
|
|
32
|
+
* HTMLInputElement. Handles both type="date" and type="time" <input> elements.
|
|
33
|
+
*/
|
|
34
|
+
function parseInputElementValue(elementOrValue, timezone) {
|
|
35
|
+
if (!elementOrValue) {
|
|
36
|
+
throw new Error('Cannot parse an empty element.');
|
|
37
|
+
}
|
|
38
|
+
const value = (0, common_1.isRuntimeTypeOf)(elementOrValue, 'string')
|
|
39
|
+
? elementOrValue
|
|
40
|
+
: elementOrValue.value;
|
|
41
|
+
return (0, create_full_date_1.createFullDate)(value, timezone);
|
|
42
|
+
}
|
|
43
|
+
exports.parseInputElementValue = parseInputElementValue;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toHtmlInputString = exports.HtmlInputElementTypeEnum = exports.toFormattedString = exports.toLocaleString = exports.toIsoString = exports.toTimestamp = void 0;
|
|
4
|
+
const common_1 = require("@augment-vir/common");
|
|
5
|
+
const locales_1 = require("../locales");
|
|
6
|
+
const timezones_1 = require("../timezone/timezones");
|
|
7
|
+
const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
|
|
8
|
+
/** Convert a FullDate into a unix timestamp with milliseconds. */
|
|
9
|
+
function toTimestamp(fullDate) {
|
|
10
|
+
return (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).toMillis();
|
|
11
|
+
}
|
|
12
|
+
exports.toTimestamp = toTimestamp;
|
|
13
|
+
/** Convert a FullDate into an ISO string in UTC; always ends in Z. */
|
|
14
|
+
function toIsoString(fullDate) {
|
|
15
|
+
const isoString = (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).setZone(timezones_1.utcTimezone).toISO();
|
|
16
|
+
/** Ignore this edge case in the types and idk how to trigger it. */
|
|
17
|
+
/* c8 ignore next 3 */
|
|
18
|
+
if (!isoString) {
|
|
19
|
+
throw new Error(`Unable to get ISO string for '${JSON.stringify(fullDate)}'`);
|
|
20
|
+
}
|
|
21
|
+
return isoString;
|
|
22
|
+
}
|
|
23
|
+
exports.toIsoString = toIsoString;
|
|
24
|
+
/** Converts the given FullDate into a string in the user's locale, or the given localeOverride. */
|
|
25
|
+
function toLocaleString(fullDate, formatOptions) {
|
|
26
|
+
return (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).toLocaleString({
|
|
27
|
+
...formatOptions,
|
|
28
|
+
}, {
|
|
29
|
+
locale: formatOptions?.locale ?? locales_1.userLocale,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
exports.toLocaleString = toLocaleString;
|
|
33
|
+
/**
|
|
34
|
+
* Provides arbitrary date formatting using Luxon. For full details on the options for the format
|
|
35
|
+
* string input, see https://moment.github.io/luxon/#/formatting?id=table-of-tokens
|
|
36
|
+
*
|
|
37
|
+
* You probably should **prefer toLocaleString** so your users see the format they expect. It
|
|
38
|
+
* already has many formatting options.
|
|
39
|
+
*/
|
|
40
|
+
function toFormattedString(fullDate, format, localeOverride = locales_1.userLocale) {
|
|
41
|
+
return (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).toFormat(format, { locale: localeOverride });
|
|
42
|
+
}
|
|
43
|
+
exports.toFormattedString = toFormattedString;
|
|
44
|
+
/**
|
|
45
|
+
* The different options for the "type" attribute in an <input> element that require specific
|
|
46
|
+
* formatting relevant to dates.
|
|
47
|
+
*/
|
|
48
|
+
var HtmlInputElementTypeEnum;
|
|
49
|
+
(function (HtmlInputElementTypeEnum) {
|
|
50
|
+
HtmlInputElementTypeEnum["Date"] = "date";
|
|
51
|
+
HtmlInputElementTypeEnum["Time"] = "time";
|
|
52
|
+
})(HtmlInputElementTypeEnum = exports.HtmlInputElementTypeEnum || (exports.HtmlInputElementTypeEnum = {}));
|
|
53
|
+
function toHtmlInputString(fullDate, inputType, includeSeconds) {
|
|
54
|
+
if (inputType === HtmlInputElementTypeEnum.Date) {
|
|
55
|
+
const dateParts = [
|
|
56
|
+
fullDate.year,
|
|
57
|
+
String(fullDate.month).padStart(2, '0'),
|
|
58
|
+
String(fullDate.day).padStart(2, '0'),
|
|
59
|
+
];
|
|
60
|
+
return dateParts.join('-');
|
|
61
|
+
}
|
|
62
|
+
else if (inputType === HtmlInputElementTypeEnum.Time) {
|
|
63
|
+
const timeParts = [
|
|
64
|
+
String(fullDate.hour).padStart(2, '0'),
|
|
65
|
+
String(fullDate.minute).padStart(2, '0'),
|
|
66
|
+
includeSeconds ? String(fullDate.second).padStart(2, '0') : undefined,
|
|
67
|
+
].filter((entry) => entry != undefined);
|
|
68
|
+
return timeParts.join(':');
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
throw new Error(`Unexpected inputTyped: '${inputType}'. Expected usage of HtmlInputElementTypeEnum or one of ${(0, common_1.getEnumTypedValues)(HtmlInputElementTypeEnum).join(', ')}.`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.toHtmlInputString = toHtmlInputString;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./full-date/create-full-date"), exports);
|
|
18
|
+
__exportStar(require("./full-date/date-like"), exports);
|
|
19
|
+
__exportStar(require("./full-date/format-presets"), exports);
|
|
20
|
+
__exportStar(require("./full-date/full-date-shape"), exports);
|
|
21
|
+
__exportStar(require("./full-date/is-valid-full-date"), exports);
|
|
22
|
+
__exportStar(require("./full-date/luxon-date-time-conversion"), exports);
|
|
23
|
+
__exportStar(require("./full-date/parsing"), exports);
|
|
24
|
+
__exportStar(require("./full-date/primitive-conversions"), exports);
|
|
25
|
+
__exportStar(require("./full-date/string-format-types"), exports);
|
|
26
|
+
__exportStar(require("./locales"), exports);
|
|
27
|
+
__exportStar(require("./timezone/timezone-checks"), exports);
|
|
28
|
+
__exportStar(require("./timezone/timezone-names"), exports);
|
|
29
|
+
__exportStar(require("./timezone/timezones"), exports);
|