@nivinjoseph/n-date 1.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/.editorconfig +14 -0
- package/.vscode/launch.json +56 -0
- package/.vscode/settings.json +111 -0
- package/.vscode/tasks.json +12 -0
- package/.yarn/releases/yarn-4.14.1.cjs +940 -0
- package/.yarnrc.yml +8 -0
- package/LICENSE +21 -0
- package/README.md +21 -0
- package/dist/date-time-format.d.ts +11 -0
- package/dist/date-time-format.d.ts.map +1 -0
- package/dist/date-time-format.js +11 -0
- package/dist/date-time-format.js.map +1 -0
- package/dist/date-time-span.d.ts +70 -0
- package/dist/date-time-span.d.ts.map +1 -0
- package/dist/date-time-span.js +122 -0
- package/dist/date-time-span.js.map +1 -0
- package/dist/date-time.d.ts +391 -0
- package/dist/date-time.d.ts.map +1 -0
- package/dist/date-time.js +753 -0
- package/dist/date-time.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/tsconfig.json +13 -0
- package/docs/README.md +33 -0
- package/docs/date-time-span.md +72 -0
- package/docs/date-time.md +169 -0
- package/docs/formats.md +56 -0
- package/docs/getting-started.md +151 -0
- package/eslint.config.js +596 -0
- package/package.json +57 -0
- package/src/date-time-format.ts +35 -0
- package/src/date-time-span.ts +130 -0
- package/src/date-time.ts +950 -0
- package/src/index.ts +3 -0
- package/test/date-time-comparison.test.ts +1579 -0
- package/test/date-time-create.test.ts +1147 -0
- package/test/date-time-math.test.ts +324 -0
- package/test/date-time-properties.test.ts +200 -0
- package/test/date-time-utility.test.ts +432 -0
- package/test/date-time-validations.test.ts +521 -0
- package/tsconfig.json +31 -0
package/.yarnrc.yml
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nivin Joseph
|
|
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,21 @@
|
|
|
1
|
+
# n-date
|
|
2
|
+
|
|
3
|
+
Date utility functions for TypeScript/JavaScript applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @nivinjoseph/n-date
|
|
9
|
+
|
|
10
|
+
# or
|
|
11
|
+
|
|
12
|
+
yarn add @nivinjoseph/n-date
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Contributing
|
|
16
|
+
|
|
17
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
18
|
+
|
|
19
|
+
## License
|
|
20
|
+
|
|
21
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare enum DateTimeFormat {
|
|
2
|
+
yearMonthDayHourMinuteSecond = "yyyy-MM-dd HH:mm:ss",
|
|
3
|
+
yearMonthDayHourMinute = "yyyy-MM-dd HH:mm",
|
|
4
|
+
yearMonthDayHour = "yyyy-MM-dd HH",
|
|
5
|
+
yearMonthDay = "yyyy-MM-dd",
|
|
6
|
+
yearMonth = "yyyy-MM",
|
|
7
|
+
year = "yyyy"
|
|
8
|
+
}
|
|
9
|
+
export declare const DateTimeFormat_DEFAULT = DateTimeFormat.yearMonthDayHourMinuteSecond;
|
|
10
|
+
export type DateTimeFormatExt = "DD HH:mm:ss" | "MMMM d, HH:mm:ss" | "DD HH:mm" | "MMMM d, HH:mm" | "yyyy/LL/dd" | "yyyy/LL/dd HH:mm:ss" | "yyyy/LL/dd HH:mm" | "yyyy-MM-dd" | "HH:mm:ss" | "HH:mm" | "DDD" | "DD" | "yyyy-MM" | "MMMM yyyy" | "DDDD" | "EEEE DD" | "LLL yyyy" | "LLLL yyyy" | "MMMM d" | "LLL d";
|
|
11
|
+
//# sourceMappingURL=date-time-format.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-time-format.d.ts","sourceRoot":"","sources":["../src/date-time-format.ts"],"names":[],"mappings":"AAAA,oBAAY,cAAc;IAEtB,4BAA4B,wBAAwB;IACpD,sBAAsB,qBAAqB;IAC3C,gBAAgB,kBAAkB;IAClC,YAAY,eAAe;IAC3B,SAAS,YAAY;IACrB,IAAI,SAAS;CAChB;AAED,eAAO,MAAM,sBAAsB,8CAA8C,CAAC;AAGlF,MAAM,MAAM,iBAAiB,GACzB,aAAa,GACX,kBAAkB,GAClB,UAAU,GACV,eAAe,GACf,YAAY,GACZ,qBAAqB,GACrB,kBAAkB,GAClB,YAAY,GACZ,UAAU,GACV,OAAO,GACP,KAAK,GACL,IAAI,GACJ,SAAS,GACT,WAAW,GACX,MAAM,GACN,SAAS,GACT,UAAU,GACV,WAAW,GACX,QAAQ,GACR,OAAO,CACR"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export var DateTimeFormat;
|
|
2
|
+
(function (DateTimeFormat) {
|
|
3
|
+
DateTimeFormat["yearMonthDayHourMinuteSecond"] = "yyyy-MM-dd HH:mm:ss";
|
|
4
|
+
DateTimeFormat["yearMonthDayHourMinute"] = "yyyy-MM-dd HH:mm";
|
|
5
|
+
DateTimeFormat["yearMonthDayHour"] = "yyyy-MM-dd HH";
|
|
6
|
+
DateTimeFormat["yearMonthDay"] = "yyyy-MM-dd";
|
|
7
|
+
DateTimeFormat["yearMonth"] = "yyyy-MM";
|
|
8
|
+
DateTimeFormat["year"] = "yyyy";
|
|
9
|
+
})(DateTimeFormat || (DateTimeFormat = {}));
|
|
10
|
+
export const DateTimeFormat_DEFAULT = DateTimeFormat.yearMonthDayHourMinuteSecond;
|
|
11
|
+
//# sourceMappingURL=date-time-format.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-time-format.js","sourceRoot":"","sources":["../src/date-time-format.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,cAQX;AARD,WAAY,cAAc;IAEtB,sEAAoD,CAAA;IACpD,6DAA2C,CAAA;IAC3C,oDAAkC,CAAA;IAClC,6CAA2B,CAAA;IAC3B,uCAAqB,CAAA;IACrB,+BAAa,CAAA;AACjB,CAAC,EARW,cAAc,KAAd,cAAc,QAQzB;AAED,MAAM,CAAC,MAAM,sBAAsB,GAAG,cAAc,CAAC,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { DateTime } from "./date-time.js";
|
|
2
|
+
import { Serializable, Duration, Schema } from "@nivinjoseph/n-util";
|
|
3
|
+
export declare class DateTimeSpan extends Serializable<DateTimeSpanSchema> {
|
|
4
|
+
private readonly _start;
|
|
5
|
+
private readonly _end;
|
|
6
|
+
get start(): DateTime;
|
|
7
|
+
get end(): DateTime;
|
|
8
|
+
get duration(): Duration;
|
|
9
|
+
constructor(data: DateTimeSpanSchema);
|
|
10
|
+
/**
|
|
11
|
+
Checks if the given DateTime is within this DateTimeSpan (inclusive of start and end).
|
|
12
|
+
|
|
13
|
+
Use cases:
|
|
14
|
+
|
|
15
|
+
this: start ─────────────── end
|
|
16
|
+
↑
|
|
17
|
+
dateTime
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
|
|
21
|
+
dateTime (DateTime): The DateTime to check.
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
|
|
25
|
+
boolean: True if dateTime is within the span [start, end], false otherwise.
|
|
26
|
+
*/
|
|
27
|
+
contains(dateTime: DateTime): boolean;
|
|
28
|
+
/**
|
|
29
|
+
Checks if this DateTimeSpan completely encompasses another DateTimeSpan.
|
|
30
|
+
|
|
31
|
+
Use cases:
|
|
32
|
+
|
|
33
|
+
this: start ─────────────────────── end
|
|
34
|
+
other: start ─── end
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
|
|
38
|
+
boolean: True if this span completely contains the other span.
|
|
39
|
+
*/
|
|
40
|
+
encompasses(other: DateTimeSpan): boolean;
|
|
41
|
+
/**
|
|
42
|
+
Checks if two DateTimeSpans have any intersection or overlap.
|
|
43
|
+
|
|
44
|
+
Use cases:
|
|
45
|
+
|
|
46
|
+
This encompasses other:
|
|
47
|
+
this: start ─────────────────────── end
|
|
48
|
+
other: start ─── end
|
|
49
|
+
|
|
50
|
+
Other encompasses this:
|
|
51
|
+
this: start ─── end
|
|
52
|
+
other: start ─────────────────────── end
|
|
53
|
+
|
|
54
|
+
Partial overlap - this starts in other:
|
|
55
|
+
this: start ─────── end
|
|
56
|
+
other: start ─────── end
|
|
57
|
+
|
|
58
|
+
Partial overlap - this ends in other:
|
|
59
|
+
this: start ─────── end
|
|
60
|
+
other: start ─────── end
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
|
|
64
|
+
boolean: True if spans overlap or intersect, false if completely separate.
|
|
65
|
+
*/
|
|
66
|
+
infringes(other: DateTimeSpan): boolean;
|
|
67
|
+
equals(other: DateTimeSpan | null): boolean;
|
|
68
|
+
}
|
|
69
|
+
export type DateTimeSpanSchema = Schema<DateTimeSpan, "start" | "end">;
|
|
70
|
+
//# sourceMappingURL=date-time-span.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-time-span.d.ts","sourceRoot":"","sources":["../src/date-time-span.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAa,QAAQ,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAGhF,qBACa,YAAa,SAAQ,YAAY,CAAC,kBAAkB,CAAC;IAE9D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAW;IAClC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAW;IAGhC,IACW,KAAK,IAAI,QAAQ,CAAwB;IAEpD,IACW,GAAG,IAAI,QAAQ,CAAsB;IAEhD,IAAW,QAAQ,IAAI,QAAQ,CAA4C;gBAGxD,IAAI,EAAE,kBAAkB;IAe3C;;;;;;;;;;;;;;;;MAgBE;IACK,QAAQ,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO;IAO5C;;;;;;;;;;;MAWE;IACK,WAAW,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAKhD;;;;;;;;;;;;;;;;;;;;;;;;MAwBE;IACK,SAAS,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAYvC,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO;CAYrD;AAGD,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAC,YAAY,EAAE,OAAO,GAAG,KAAK,CAAC,CAAC"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { __esDecorate, __runInitializers } from "tslib";
|
|
2
|
+
import { given } from "@nivinjoseph/n-defensive";
|
|
3
|
+
import { DateTime } from "./date-time.js";
|
|
4
|
+
import { Serializable, serialize } from "@nivinjoseph/n-util";
|
|
5
|
+
let DateTimeSpan = (() => {
|
|
6
|
+
let _classDecorators = [serialize("Ndate")];
|
|
7
|
+
let _classDescriptor;
|
|
8
|
+
let _classExtraInitializers = [];
|
|
9
|
+
let _classThis;
|
|
10
|
+
let _classSuper = Serializable;
|
|
11
|
+
let _instanceExtraInitializers = [];
|
|
12
|
+
let _get_start_decorators;
|
|
13
|
+
let _get_end_decorators;
|
|
14
|
+
var DateTimeSpan = class extends _classSuper {
|
|
15
|
+
static { _classThis = this; }
|
|
16
|
+
static {
|
|
17
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
18
|
+
_get_start_decorators = [serialize];
|
|
19
|
+
_get_end_decorators = [serialize];
|
|
20
|
+
__esDecorate(this, null, _get_start_decorators, { kind: "getter", name: "start", static: false, private: false, access: { has: obj => "start" in obj, get: obj => obj.start }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
21
|
+
__esDecorate(this, null, _get_end_decorators, { kind: "getter", name: "end", static: false, private: false, access: { has: obj => "end" in obj, get: obj => obj.end }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
22
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
23
|
+
DateTimeSpan = _classThis = _classDescriptor.value;
|
|
24
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
25
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
26
|
+
}
|
|
27
|
+
_start = __runInitializers(this, _instanceExtraInitializers);
|
|
28
|
+
_end;
|
|
29
|
+
get start() { return this._start; }
|
|
30
|
+
get end() { return this._end; }
|
|
31
|
+
get duration() { return this._end.timeDiff(this._start); }
|
|
32
|
+
constructor(data) {
|
|
33
|
+
super(data);
|
|
34
|
+
const { start, end } = data;
|
|
35
|
+
given(start, "start").ensureHasValue().ensureIsType(DateTime);
|
|
36
|
+
this._start = start;
|
|
37
|
+
given(end, "end").ensureHasValue().ensureIsType(DateTime)
|
|
38
|
+
.ensure(t => t.isSameOrAfter(start), "must be same or after start");
|
|
39
|
+
this._end = end;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
Checks if the given DateTime is within this DateTimeSpan (inclusive of start and end).
|
|
43
|
+
|
|
44
|
+
Use cases:
|
|
45
|
+
|
|
46
|
+
this: start ─────────────── end
|
|
47
|
+
↑
|
|
48
|
+
dateTime
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
|
|
52
|
+
dateTime (DateTime): The DateTime to check.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
|
|
56
|
+
boolean: True if dateTime is within the span [start, end], false otherwise.
|
|
57
|
+
*/
|
|
58
|
+
contains(dateTime) {
|
|
59
|
+
given(dateTime, "dateTime").ensureHasValue().ensureIsObject();
|
|
60
|
+
return dateTime.isBetween(this._start, this._end);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
Checks if this DateTimeSpan completely encompasses another DateTimeSpan.
|
|
64
|
+
|
|
65
|
+
Use cases:
|
|
66
|
+
|
|
67
|
+
this: start ─────────────────────── end
|
|
68
|
+
other: start ─── end
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
|
|
72
|
+
boolean: True if this span completely contains the other span.
|
|
73
|
+
*/
|
|
74
|
+
encompasses(other) {
|
|
75
|
+
return this._start.isSameOrBefore(other._start) && this._end.isSameOrAfter(other._end);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
Checks if two DateTimeSpans have any intersection or overlap.
|
|
79
|
+
|
|
80
|
+
Use cases:
|
|
81
|
+
|
|
82
|
+
This encompasses other:
|
|
83
|
+
this: start ─────────────────────── end
|
|
84
|
+
other: start ─── end
|
|
85
|
+
|
|
86
|
+
Other encompasses this:
|
|
87
|
+
this: start ─── end
|
|
88
|
+
other: start ─────────────────────── end
|
|
89
|
+
|
|
90
|
+
Partial overlap - this starts in other:
|
|
91
|
+
this: start ─────── end
|
|
92
|
+
other: start ─────── end
|
|
93
|
+
|
|
94
|
+
Partial overlap - this ends in other:
|
|
95
|
+
this: start ─────── end
|
|
96
|
+
other: start ─────── end
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
|
|
100
|
+
boolean: True if spans overlap or intersect, false if completely separate.
|
|
101
|
+
*/
|
|
102
|
+
infringes(other) {
|
|
103
|
+
// if start and end of self is contained in other
|
|
104
|
+
// or other encompasses self
|
|
105
|
+
// or self encompasses other
|
|
106
|
+
if (this.encompasses(other) || other.encompasses(this))
|
|
107
|
+
return true;
|
|
108
|
+
return other.contains(this._start) || other.contains(this._end);
|
|
109
|
+
}
|
|
110
|
+
equals(other) {
|
|
111
|
+
given(other, "other").ensureIsType(DateTimeSpan);
|
|
112
|
+
if (other == null)
|
|
113
|
+
return false;
|
|
114
|
+
if (other === this)
|
|
115
|
+
return true;
|
|
116
|
+
return this._start.equals(other._start) && this._end.equals(other._end);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
return DateTimeSpan = _classThis;
|
|
120
|
+
})();
|
|
121
|
+
export { DateTimeSpan };
|
|
122
|
+
//# sourceMappingURL=date-time-span.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-time-span.js","sourceRoot":"","sources":["../src/date-time-span.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,SAAS,EAAoB,MAAM,qBAAqB,CAAC;IAInE,YAAY;4BADxB,SAAS,CAAC,OAAO,CAAC;;;;sBACe,YAAY;;;;4BAApB,SAAQ,WAAgC;;;;qCAM7D,SAAS;mCAGT,SAAS;YAFV,sKAAW,KAAK,6DAAoC;YAGpD,gKAAW,GAAG,6DAAkC;YAVpD,6KAwHC;;;YAxHY,uDAAY;;QAEJ,MAAM,GAFd,mDAAY,CAEa;QACjB,IAAI,CAAW;QAIhC,IAAW,KAAK,KAAe,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;QAGpD,IAAW,GAAG,KAAe,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAEhD,IAAW,QAAQ,KAAe,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QAG3E,YAAmB,IAAwB;YAEvC,KAAK,CAAC,IAAI,CAAC,CAAC;YAEZ,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;YAE5B,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,cAAc,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;YAC9D,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;YAEpB,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,cAAc,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC;iBACpD,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,6BAA6B,CAAC,CAAC;YACxE,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QACpB,CAAC;QAGD;;;;;;;;;;;;;;;;UAgBE;QACK,QAAQ,CAAC,QAAkB;YAE9B,KAAK,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE,CAAC;YAE9D,OAAO,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QACtD,CAAC;QAED;;;;;;;;;;;UAWE;QACK,WAAW,CAAC,KAAmB;YAElC,OAAO,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC3F,CAAC;QAED;;;;;;;;;;;;;;;;;;;;;;;;UAwBE;QACK,SAAS,CAAC,KAAmB;YAEhC,iDAAiD;YACjD,4BAA4B;YAC5B,4BAA4B;YAE5B,IAAI,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC;gBAClD,OAAO,IAAI,CAAC;YAEhB,OAAO,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpE,CAAC;QAEM,MAAM,CAAC,KAA0B;YAEpC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;YAEjD,IAAI,KAAK,IAAI,IAAI;gBACb,OAAO,KAAK,CAAC;YAEjB,IAAI,KAAK,KAAK,IAAI;gBACd,OAAO,IAAI,CAAC;YAEhB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC5E,CAAC;;;;SAvHQ,YAAY"}
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import { Serializable, Duration, Schema } from "@nivinjoseph/n-util";
|
|
2
|
+
import { DateTimeFormat, DateTimeFormatExt } from "./date-time-format.js";
|
|
3
|
+
/**
|
|
4
|
+
* A robust date and time handling system with timezone support.
|
|
5
|
+
* This class provides comprehensive functionality for date/time manipulation, comparison, and formatting.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const now = DateTime.now("UTC");
|
|
10
|
+
* const future = now.addTime(Duration.fromHours(2));
|
|
11
|
+
* const isAfter = future.isAfter(now);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare class DateTime extends Serializable<DateTimeSchema> {
|
|
15
|
+
private static _fixedNow;
|
|
16
|
+
private static _relativeNow;
|
|
17
|
+
private readonly _value;
|
|
18
|
+
private readonly _zone;
|
|
19
|
+
private readonly _dateTime;
|
|
20
|
+
private readonly _timestamp;
|
|
21
|
+
private readonly _dateCode;
|
|
22
|
+
private readonly _timeCode;
|
|
23
|
+
private readonly _dateValue;
|
|
24
|
+
private readonly _timeValue;
|
|
25
|
+
/**
|
|
26
|
+
* Gets the system's local timezone.
|
|
27
|
+
*
|
|
28
|
+
* @returns The local timezone identifier.
|
|
29
|
+
*/
|
|
30
|
+
static get currentZone(): string;
|
|
31
|
+
/**
|
|
32
|
+
* Gets the formatted date and time string.
|
|
33
|
+
*/
|
|
34
|
+
get value(): string;
|
|
35
|
+
/**
|
|
36
|
+
* Gets the timezone identifier.
|
|
37
|
+
*/
|
|
38
|
+
get zone(): string;
|
|
39
|
+
/**
|
|
40
|
+
* Gets the Unix timestamp in seconds.
|
|
41
|
+
*/
|
|
42
|
+
get timestamp(): number;
|
|
43
|
+
/**
|
|
44
|
+
* Gets the date code in YYYYMMDD format.
|
|
45
|
+
*/
|
|
46
|
+
get dateCode(): string;
|
|
47
|
+
/**
|
|
48
|
+
* Gets the time code in HHMMSS format.
|
|
49
|
+
*/
|
|
50
|
+
get timeCode(): string;
|
|
51
|
+
/**
|
|
52
|
+
* Gets the date value in YYYY-MM-DD format.
|
|
53
|
+
*/
|
|
54
|
+
get dateValue(): string;
|
|
55
|
+
/**
|
|
56
|
+
* Gets the time value in HH:mm:ss format.
|
|
57
|
+
*/
|
|
58
|
+
get timeValue(): string;
|
|
59
|
+
/**
|
|
60
|
+
* Gets whether this DateTime is in the past.
|
|
61
|
+
*/
|
|
62
|
+
get isPast(): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Gets whether this DateTime is in the future.
|
|
65
|
+
*/
|
|
66
|
+
get isFuture(): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a new DateTime instance.
|
|
69
|
+
*
|
|
70
|
+
* @param data - The DateTime data containing value and zone.
|
|
71
|
+
* @throws Error if the value or zone is invalid.
|
|
72
|
+
*/
|
|
73
|
+
constructor(data: DateTimeSchema);
|
|
74
|
+
/**
|
|
75
|
+
* Sets a fixed timestamp for testing purposes. All calls to DateTime.now() will return this fixed time.
|
|
76
|
+
*
|
|
77
|
+
* @param timestamp - The Unix timestamp in seconds to use as the fixed "now" time.
|
|
78
|
+
* @throws Error if timestamp is not a valid number.
|
|
79
|
+
*/
|
|
80
|
+
static useFixedNow(timestamp: number): void;
|
|
81
|
+
/**
|
|
82
|
+
* Sets a relative timestamp for testing purposes. DateTime.now() will return times relative to this base timestamp,
|
|
83
|
+
* advancing as real time advances.
|
|
84
|
+
*
|
|
85
|
+
* @param timestamp - The Unix timestamp in seconds to use as the base "now" time.
|
|
86
|
+
* @throws Error if timestamp is not a valid number.
|
|
87
|
+
*/
|
|
88
|
+
static useRelativeNow(timestamp: number): void;
|
|
89
|
+
/**
|
|
90
|
+
* Resets any fixed or relative "now" time set by useFixedNow or useRelativeNow.
|
|
91
|
+
* DateTime.now() will return the actual current time after calling this method.
|
|
92
|
+
*/
|
|
93
|
+
static resetFixedOrRelativeNow(): void;
|
|
94
|
+
/**
|
|
95
|
+
* Creates a DateTime instance for the current time.
|
|
96
|
+
*
|
|
97
|
+
* @param zone - The timezone identifier. If not specified, UTC is used.
|
|
98
|
+
* @returns A new DateTime instance representing the current time.
|
|
99
|
+
*/
|
|
100
|
+
static now(zone?: string): DateTime;
|
|
101
|
+
/**
|
|
102
|
+
* Creates a DateTime from a Unix timestamp.
|
|
103
|
+
*
|
|
104
|
+
* @param timestamp - The number of seconds since the Unix epoch.
|
|
105
|
+
* @param zone - The timezone identifier.
|
|
106
|
+
* @returns A new DateTime instance.
|
|
107
|
+
*/
|
|
108
|
+
static createFromTimestamp(timestamp: number, zone: string): DateTime;
|
|
109
|
+
/**
|
|
110
|
+
* Creates a DateTime from milliseconds since the Unix epoch.
|
|
111
|
+
*
|
|
112
|
+
* @param milliseconds - The number of milliseconds since the Unix epoch.
|
|
113
|
+
* @param zone - The timezone identifier.
|
|
114
|
+
* @returns A new DateTime instance.
|
|
115
|
+
*/
|
|
116
|
+
static createFromMilliSecondsSinceEpoch(milliseconds: number, zone: string): DateTime;
|
|
117
|
+
/**
|
|
118
|
+
* Creates a DateTime from date and time codes.
|
|
119
|
+
*
|
|
120
|
+
* @param dateCode - The date code in YYYYMMDD format.
|
|
121
|
+
* @param timeCode - The time code in HHMM format.
|
|
122
|
+
* @param zone - The timezone identifier.
|
|
123
|
+
* @returns A new DateTime instance.
|
|
124
|
+
*/
|
|
125
|
+
static createFromCodes(dateCode: string, timeCode: string, zone: string): DateTime;
|
|
126
|
+
/**
|
|
127
|
+
* Creates a DateTime from date and time values.
|
|
128
|
+
*
|
|
129
|
+
* @param dateValue - The date in YYYY-MM-DD format.
|
|
130
|
+
* @param timeValue - The time in HH:mm format.
|
|
131
|
+
* @param zone - The timezone identifier.
|
|
132
|
+
* @returns A new DateTime instance.
|
|
133
|
+
*/
|
|
134
|
+
static createFromValues(dateValue: string, timeValue: string, zone: string): DateTime;
|
|
135
|
+
/**
|
|
136
|
+
* Returns the earlier of two DateTime instances.
|
|
137
|
+
*
|
|
138
|
+
* @param dateTime1 - The first DateTime instance.
|
|
139
|
+
* @param dateTime2 - The second DateTime instance.
|
|
140
|
+
* @returns The earlier DateTime instance.
|
|
141
|
+
*/
|
|
142
|
+
static min(dateTime1: DateTime, dateTime2: DateTime): DateTime;
|
|
143
|
+
/**
|
|
144
|
+
* Returns the later of two DateTime instances.
|
|
145
|
+
*
|
|
146
|
+
* @param dateTime1 - The first DateTime instance.
|
|
147
|
+
* @param dateTime2 - The second DateTime instance.
|
|
148
|
+
* @returns The later DateTime instance.
|
|
149
|
+
*/
|
|
150
|
+
static max(dateTime1: DateTime, dateTime2: DateTime): DateTime;
|
|
151
|
+
/**
|
|
152
|
+
* Validates if a string matches the DateTime format "yyyy-MM-dd HH:mm".
|
|
153
|
+
*
|
|
154
|
+
* @param value - The string to validate.
|
|
155
|
+
* @returns True if the string matches the format, false otherwise.
|
|
156
|
+
*/
|
|
157
|
+
static validateDateTimeFormat(value: string, format: DateTimeFormat): boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Validates if a string matches the date format "yyyy-MM-dd".
|
|
160
|
+
*
|
|
161
|
+
* @param value - The string to validate.
|
|
162
|
+
* @returns True if the string matches the format, false otherwise.
|
|
163
|
+
*/
|
|
164
|
+
static validateDateFormat(value: string): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Validates if a string matches the time format "HH:mm".
|
|
167
|
+
*
|
|
168
|
+
* @param value - The string to validate.
|
|
169
|
+
* @returns True if the string matches the format, false otherwise.
|
|
170
|
+
*/
|
|
171
|
+
static validateTimeFormat(value: string): boolean;
|
|
172
|
+
/**
|
|
173
|
+
* Validates if a string is a valid timezone.
|
|
174
|
+
*
|
|
175
|
+
* @param zone - The timezone string to validate.
|
|
176
|
+
* @returns True if the timezone is valid, false otherwise.
|
|
177
|
+
*/
|
|
178
|
+
static validateTimeZone(zone: string): boolean;
|
|
179
|
+
/**
|
|
180
|
+
* Validates a timezone string.
|
|
181
|
+
*
|
|
182
|
+
* @param zone - The timezone string to validate.
|
|
183
|
+
* @throws Error if the timezone is invalid.
|
|
184
|
+
* @private
|
|
185
|
+
*/
|
|
186
|
+
private static _validateZone;
|
|
187
|
+
/**
|
|
188
|
+
* Gets the numeric value of this DateTime.
|
|
189
|
+
*
|
|
190
|
+
* @returns The milliseconds since the Unix epoch.
|
|
191
|
+
*/
|
|
192
|
+
valueOf(): number;
|
|
193
|
+
/**
|
|
194
|
+
* Compares this DateTime with another for equality.
|
|
195
|
+
*
|
|
196
|
+
* @param value - The DateTime to compare with.
|
|
197
|
+
* @returns True if the DateTime instances are equal, false otherwise.
|
|
198
|
+
*/
|
|
199
|
+
equals(value?: DateTime | null): boolean;
|
|
200
|
+
/**
|
|
201
|
+
* Returns the string representation of this DateTime.
|
|
202
|
+
*
|
|
203
|
+
* @returns The string representation in the format "YYYY-MM-DD HH:mm:ss zone".
|
|
204
|
+
*/
|
|
205
|
+
toString(): string;
|
|
206
|
+
/**
|
|
207
|
+
* Returns the date and time string.
|
|
208
|
+
*
|
|
209
|
+
* @returns The string in the format "YYYY-MM-DD HH:mm:ss".
|
|
210
|
+
*/
|
|
211
|
+
toStringDateTime(): string;
|
|
212
|
+
/**
|
|
213
|
+
* Returns the ISO string representation.
|
|
214
|
+
*
|
|
215
|
+
* @returns The ISO 8601 string representation.
|
|
216
|
+
*/
|
|
217
|
+
toStringISO(): string;
|
|
218
|
+
/**
|
|
219
|
+
* Formats a DateTime object to a string using the specified format
|
|
220
|
+
* @param dateTime The DateTime object to format
|
|
221
|
+
* @param format The format to use (defaults to yearMonthDayHourMinute: "yyyy-MM-dd HH:mm")
|
|
222
|
+
* @returns The formatted datetime string
|
|
223
|
+
*/
|
|
224
|
+
format(format?: DateTimeFormat): string;
|
|
225
|
+
/**
|
|
226
|
+
* Formats a DateTime object to a string using extended format capabilities.
|
|
227
|
+
* This method leverages Luxon's full formatting capabilities for more complex formatting needs.
|
|
228
|
+
*
|
|
229
|
+
* @param format - The format string to use. Can be a predefined DateTimeFormatExt or any custom Luxon format string.
|
|
230
|
+
* @returns The formatted datetime string.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```typescript
|
|
234
|
+
* const dt = DateTime.now("America/New_York");
|
|
235
|
+
* dt.formatExt("DD HH:mm:ss"); // "Jul 2, 2023 15:30:20"
|
|
236
|
+
* dt.formatExt("MMMM d, yyyy"); // "July 2, 2023"
|
|
237
|
+
* dt.formatExt("EEEE DD"); // "Friday Jul 2, 2023"
|
|
238
|
+
*
|
|
239
|
+
* export type DateTimeFormatExt =
|
|
240
|
+
"DD HH:mm:ss" // Jul 2, 2023 15:30:20
|
|
241
|
+
| "MMMM d, HH:mm:ss" // Jul 2 15:30:20
|
|
242
|
+
| "DD HH:mm" // Jul 2, 2023 15:30
|
|
243
|
+
| "MMMM d, HH:mm" // Jul 2 15:30
|
|
244
|
+
| "yyyy/LL/dd" // 2023/07/21
|
|
245
|
+
| "yyyy/LL/dd HH:mm:ss"
|
|
246
|
+
| "yyyy/LL/dd HH:mm"
|
|
247
|
+
| "yyyy-MM-dd" // 2023-07-21
|
|
248
|
+
| "HH:mm:ss" // 15:30:20
|
|
249
|
+
| "HH:mm" // 15:30
|
|
250
|
+
| "DDD" // July 21, 2023
|
|
251
|
+
| "DD" // Jul 21, 2023
|
|
252
|
+
| "yyyy-MM" // 2023-07
|
|
253
|
+
| "MMMM yyyy" // July 2023
|
|
254
|
+
| "DDDD" // Sunday, July 9, 2023
|
|
255
|
+
| "EEEE DD" // Friday Aug 4, 2023
|
|
256
|
+
| "LLL yyyy" // Jul 2025
|
|
257
|
+
| "LLLL yyyy" // July 2025
|
|
258
|
+
| "MMMM d" // November 2
|
|
259
|
+
| "LLL d" // Nov 2
|
|
260
|
+
;
|
|
261
|
+
*
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
formatExt(format: DateTimeFormatExt | string): string;
|
|
265
|
+
/**
|
|
266
|
+
* Checks if this DateTime represents the same instant as another.
|
|
267
|
+
*
|
|
268
|
+
* @param value - The DateTime to compare with.
|
|
269
|
+
* @returns True if the DateTime instances represent the same instant, false otherwise.
|
|
270
|
+
*/
|
|
271
|
+
isSame(value: DateTime): boolean;
|
|
272
|
+
/**
|
|
273
|
+
* Checks if this DateTime is before another.
|
|
274
|
+
*
|
|
275
|
+
* @param value - The DateTime to compare with.
|
|
276
|
+
* @returns True if this DateTime is before the other, false otherwise.
|
|
277
|
+
*/
|
|
278
|
+
isBefore(value: DateTime): boolean;
|
|
279
|
+
/**
|
|
280
|
+
* Checks if this DateTime is the same or before another.
|
|
281
|
+
*
|
|
282
|
+
* @param value - The DateTime to compare with.
|
|
283
|
+
* @returns True if this DateTime is the same or before the other, false otherwise.
|
|
284
|
+
*/
|
|
285
|
+
isSameOrBefore(value: DateTime): boolean;
|
|
286
|
+
/**
|
|
287
|
+
* Checks if this DateTime is after another.
|
|
288
|
+
*
|
|
289
|
+
* @param value - The DateTime to compare with.
|
|
290
|
+
* @returns True if this DateTime is after the other, false otherwise.
|
|
291
|
+
*/
|
|
292
|
+
isAfter(value: DateTime): boolean;
|
|
293
|
+
/**
|
|
294
|
+
* Checks if this DateTime is the same or after another.
|
|
295
|
+
*
|
|
296
|
+
* @param value - The DateTime to compare with.
|
|
297
|
+
* @returns True if this DateTime is the same or after the other, false otherwise.
|
|
298
|
+
*/
|
|
299
|
+
isSameOrAfter(value: DateTime): boolean;
|
|
300
|
+
/**
|
|
301
|
+
* Checks if this DateTime is between two others.
|
|
302
|
+
*
|
|
303
|
+
* @param start - The start DateTime.
|
|
304
|
+
* @param end - The end DateTime.
|
|
305
|
+
* @returns True if this DateTime is between start and end, false otherwise.
|
|
306
|
+
* @throws Error if end is before start.
|
|
307
|
+
*/
|
|
308
|
+
isBetween(start: DateTime, end: DateTime): boolean;
|
|
309
|
+
/**
|
|
310
|
+
* Calculates the time difference between this DateTime and another.
|
|
311
|
+
*
|
|
312
|
+
* @param value - The DateTime to compare with.
|
|
313
|
+
* @returns A Duration representing the time difference.
|
|
314
|
+
*/
|
|
315
|
+
timeDiff(value: DateTime): Duration;
|
|
316
|
+
/**
|
|
317
|
+
* Calculates the days difference between this DateTime and another.
|
|
318
|
+
*
|
|
319
|
+
* @param value - The DateTime to compare with.
|
|
320
|
+
* @returns The number of days difference.
|
|
321
|
+
*/
|
|
322
|
+
daysDiff(value: DateTime): number;
|
|
323
|
+
/**
|
|
324
|
+
* Checks if this DateTime is on the same day as another.
|
|
325
|
+
*
|
|
326
|
+
* @param value - The DateTime to compare with.
|
|
327
|
+
* @returns True if the DateTime instances are on the same day, false otherwise.
|
|
328
|
+
*/
|
|
329
|
+
isSameDay(value: DateTime): boolean;
|
|
330
|
+
/**
|
|
331
|
+
* Adds a duration to this DateTime. this accounts for shift in DST
|
|
332
|
+
*
|
|
333
|
+
* @param time - The duration to add.
|
|
334
|
+
* @returns A new DateTime instance with the duration added.
|
|
335
|
+
*/
|
|
336
|
+
addTime(time: Duration): DateTime;
|
|
337
|
+
/**
|
|
338
|
+
* Subtracts a duration from this DateTime. this accounts for shift in DST
|
|
339
|
+
*
|
|
340
|
+
* @param time - The duration to subtract.
|
|
341
|
+
* @returns A new DateTime instance with the duration subtracted.
|
|
342
|
+
*/
|
|
343
|
+
subtractTime(time: Duration): DateTime;
|
|
344
|
+
/**
|
|
345
|
+
* Adds days to this DateTime. this doesn't change time based on DST
|
|
346
|
+
*
|
|
347
|
+
* @param days - The number of days to add.
|
|
348
|
+
* @returns A new DateTime instance with the days added.
|
|
349
|
+
* @throws Error if days is not a positive integer.
|
|
350
|
+
*/
|
|
351
|
+
addDays(days: number): DateTime;
|
|
352
|
+
/**
|
|
353
|
+
* Subtracts days from this DateTime. this doesn't change time based on DST
|
|
354
|
+
*
|
|
355
|
+
* @param days - The number of days to subtract.
|
|
356
|
+
* @returns A new DateTime instance with the days subtracted.
|
|
357
|
+
* @throws Error if days is not a positive integer.
|
|
358
|
+
*/
|
|
359
|
+
subtractDays(days: number): DateTime;
|
|
360
|
+
/**
|
|
361
|
+
* Gets an array of DateTime instances for each day of the month.
|
|
362
|
+
*
|
|
363
|
+
* @returns An array of DateTime instances, where:
|
|
364
|
+
* - First element is the start of the month (e.g., "2023-06-01 00:00")
|
|
365
|
+
* - Last element is the end of the month (e.g., "2023-06-30 23:59")
|
|
366
|
+
* - Elements in between represent the start of each day (e.g., "2023-06-11 00:00")
|
|
367
|
+
*/
|
|
368
|
+
getDaysOfMonth(): Array<DateTime>;
|
|
369
|
+
/**
|
|
370
|
+
* Converts this DateTime to a different timezone.
|
|
371
|
+
*
|
|
372
|
+
* @param zone - The target timezone.
|
|
373
|
+
* @returns A new DateTime instance in the specified timezone.
|
|
374
|
+
* @throws Error if the timezone is invalid.
|
|
375
|
+
*/
|
|
376
|
+
convertToZone(zone: string): DateTime;
|
|
377
|
+
/**
|
|
378
|
+
* Checks if this DateTime is within a time range.
|
|
379
|
+
*
|
|
380
|
+
* @param startTimeCode - The start time code in HHMM format.
|
|
381
|
+
* @param endTimeCode - The end time code in HHMM format.
|
|
382
|
+
* @returns True if this DateTime is within the time range, false otherwise.
|
|
383
|
+
* @throws Error if the time codes are invalid or if endTimeCode is before startTimeCode.
|
|
384
|
+
*/
|
|
385
|
+
isWithinTimeRange(startTimeCode: string, endTimeCode: string): boolean;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Schema type for DateTime serialization.
|
|
389
|
+
*/
|
|
390
|
+
export type DateTimeSchema = Schema<DateTime, "value" | "zone">;
|
|
391
|
+
//# sourceMappingURL=date-time.d.ts.map
|