@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
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export enum DateTimeFormat
|
|
2
|
+
{
|
|
3
|
+
yearMonthDayHourMinuteSecond = "yyyy-MM-dd HH:mm:ss",
|
|
4
|
+
yearMonthDayHourMinute = "yyyy-MM-dd HH:mm",
|
|
5
|
+
yearMonthDayHour = "yyyy-MM-dd HH",
|
|
6
|
+
yearMonthDay = "yyyy-MM-dd",
|
|
7
|
+
yearMonth = "yyyy-MM",
|
|
8
|
+
year = "yyyy",
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const DateTimeFormat_DEFAULT = DateTimeFormat.yearMonthDayHourMinuteSecond;
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export type DateTimeFormatExt =
|
|
15
|
+
"DD HH:mm:ss" // Jul 2, 2023 15:30:20
|
|
16
|
+
| "MMMM d, HH:mm:ss" // Jul 2 15:30:20
|
|
17
|
+
| "DD HH:mm" // Jul 2, 2023 15:30
|
|
18
|
+
| "MMMM d, HH:mm" // Jul 2 15:30
|
|
19
|
+
| "yyyy/LL/dd" // 2023/07/21
|
|
20
|
+
| "yyyy/LL/dd HH:mm:ss"
|
|
21
|
+
| "yyyy/LL/dd HH:mm"
|
|
22
|
+
| "yyyy-MM-dd" // 2023-07-21
|
|
23
|
+
| "HH:mm:ss" // 15:30:20
|
|
24
|
+
| "HH:mm" // 15:30
|
|
25
|
+
| "DDD" // July 21, 2023
|
|
26
|
+
| "DD" // Jul 21, 2023
|
|
27
|
+
| "yyyy-MM" // 2023-07
|
|
28
|
+
| "MMMM yyyy" // July 2023
|
|
29
|
+
| "DDDD" // Sunday, July 9, 2023
|
|
30
|
+
| "EEEE DD" // Friday Aug 4, 2023
|
|
31
|
+
| "LLL yyyy" // Jul 2025
|
|
32
|
+
| "LLLL yyyy" // July 2025
|
|
33
|
+
| "MMMM d" // November 2
|
|
34
|
+
| "LLL d" // Nov 2
|
|
35
|
+
;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { given } from "@nivinjoseph/n-defensive";
|
|
2
|
+
import { DateTime } from "./date-time.js";
|
|
3
|
+
import { Serializable, serialize, Duration, Schema } from "@nivinjoseph/n-util";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@serialize("Ndate")
|
|
7
|
+
export class DateTimeSpan extends Serializable<DateTimeSpanSchema>
|
|
8
|
+
{
|
|
9
|
+
private readonly _start: DateTime;
|
|
10
|
+
private readonly _end: DateTime;
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@serialize
|
|
14
|
+
public get start(): DateTime { return this._start; }
|
|
15
|
+
|
|
16
|
+
@serialize
|
|
17
|
+
public get end(): DateTime { return this._end; }
|
|
18
|
+
|
|
19
|
+
public get duration(): Duration { return this._end.timeDiff(this._start); }
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
public constructor(data: DateTimeSpanSchema)
|
|
23
|
+
{
|
|
24
|
+
super(data);
|
|
25
|
+
|
|
26
|
+
const { start, end } = data;
|
|
27
|
+
|
|
28
|
+
given(start, "start").ensureHasValue().ensureIsType(DateTime);
|
|
29
|
+
this._start = start;
|
|
30
|
+
|
|
31
|
+
given(end, "end").ensureHasValue().ensureIsType(DateTime)
|
|
32
|
+
.ensure(t => t.isSameOrAfter(start), "must be same or after start");
|
|
33
|
+
this._end = end;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
Checks if the given DateTime is within this DateTimeSpan (inclusive of start and end).
|
|
39
|
+
|
|
40
|
+
Use cases:
|
|
41
|
+
|
|
42
|
+
this: start ─────────────── end
|
|
43
|
+
↑
|
|
44
|
+
dateTime
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
|
|
48
|
+
dateTime (DateTime): The DateTime to check.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
|
|
52
|
+
boolean: True if dateTime is within the span [start, end], false otherwise.
|
|
53
|
+
*/
|
|
54
|
+
public contains(dateTime: DateTime): boolean
|
|
55
|
+
{
|
|
56
|
+
given(dateTime, "dateTime").ensureHasValue().ensureIsObject();
|
|
57
|
+
|
|
58
|
+
return dateTime.isBetween(this._start, this._end);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
Checks if this DateTimeSpan completely encompasses another DateTimeSpan.
|
|
63
|
+
|
|
64
|
+
Use cases:
|
|
65
|
+
|
|
66
|
+
this: start ─────────────────────── end
|
|
67
|
+
other: start ─── end
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
|
|
71
|
+
boolean: True if this span completely contains the other span.
|
|
72
|
+
*/
|
|
73
|
+
public encompasses(other: DateTimeSpan): boolean
|
|
74
|
+
{
|
|
75
|
+
return this._start.isSameOrBefore(other._start) && this._end.isSameOrAfter(other._end);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
Checks if two DateTimeSpans have any intersection or overlap.
|
|
80
|
+
|
|
81
|
+
Use cases:
|
|
82
|
+
|
|
83
|
+
This encompasses other:
|
|
84
|
+
this: start ─────────────────────── end
|
|
85
|
+
other: start ─── end
|
|
86
|
+
|
|
87
|
+
Other encompasses this:
|
|
88
|
+
this: start ─── end
|
|
89
|
+
other: start ─────────────────────── end
|
|
90
|
+
|
|
91
|
+
Partial overlap - this starts in other:
|
|
92
|
+
this: start ─────── end
|
|
93
|
+
other: start ─────── end
|
|
94
|
+
|
|
95
|
+
Partial overlap - this ends in other:
|
|
96
|
+
this: start ─────── end
|
|
97
|
+
other: start ─────── end
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
|
|
101
|
+
boolean: True if spans overlap or intersect, false if completely separate.
|
|
102
|
+
*/
|
|
103
|
+
public infringes(other: DateTimeSpan): boolean
|
|
104
|
+
{
|
|
105
|
+
// if start and end of self is contained in other
|
|
106
|
+
// or other encompasses self
|
|
107
|
+
// or self encompasses other
|
|
108
|
+
|
|
109
|
+
if (this.encompasses(other) || other.encompasses(this))
|
|
110
|
+
return true;
|
|
111
|
+
|
|
112
|
+
return other.contains(this._start) || other.contains(this._end);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public equals(other: DateTimeSpan | null): boolean
|
|
116
|
+
{
|
|
117
|
+
given(other, "other").ensureIsType(DateTimeSpan);
|
|
118
|
+
|
|
119
|
+
if (other == null)
|
|
120
|
+
return false;
|
|
121
|
+
|
|
122
|
+
if (other === this)
|
|
123
|
+
return true;
|
|
124
|
+
|
|
125
|
+
return this._start.equals(other._start) && this._end.equals(other._end);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
export type DateTimeSpanSchema = Schema<DateTimeSpan, "start" | "end">;
|