@f-o-t/datetime 0.1.1 → 0.1.4
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/dist/core/datetime.d.ts +341 -0
- package/dist/core/datetime.d.ts.map +1 -0
- package/dist/core/factory.d.ts +30 -0
- package/dist/core/factory.d.ts.map +1 -0
- package/dist/errors.d.ts +43 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/index-77f5wgyc.js +41 -0
- package/dist/index-77f5wgyc.js.map +10 -0
- package/dist/index-9jdtsp4s.js +55 -0
- package/dist/index-9jdtsp4s.js.map +10 -0
- package/dist/index-a78jd9k6.js +64 -0
- package/dist/index-a78jd9k6.js.map +10 -0
- package/dist/index-rtm7bpky.js +86 -0
- package/dist/index-rtm7bpky.js.map +10 -0
- package/dist/index-v3cytzbp.js +105 -0
- package/dist/index-v3cytzbp.js.map +11 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +390 -0
- package/dist/index.js.map +13 -0
- package/dist/plugins/business-days/index.d.ts +45 -0
- package/dist/plugins/business-days/index.d.ts.map +1 -0
- package/dist/plugins/business-days/index.js +10 -0
- package/dist/plugins/business-days/index.js.map +9 -0
- package/dist/plugins/format/index.d.ts +54 -0
- package/dist/plugins/format/index.d.ts.map +1 -0
- package/dist/plugins/format/index.js +10 -0
- package/dist/plugins/format/index.js.map +9 -0
- package/dist/plugins/format/tokens.d.ts +20 -0
- package/dist/plugins/format/tokens.d.ts.map +1 -0
- package/dist/plugins/index.d.ts +14 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/plugin-base.d.ts +49 -0
- package/dist/plugins/plugin-base.d.ts.map +1 -0
- package/dist/plugins/relative-time/index.d.ts +61 -0
- package/dist/plugins/relative-time/index.d.ts.map +1 -0
- package/dist/plugins/relative-time/index.js +10 -0
- package/dist/plugins/relative-time/index.js.map +9 -0
- package/dist/plugins/timezone/index.d.ts +59 -0
- package/dist/plugins/timezone/index.d.ts.map +1 -0
- package/dist/plugins/timezone/index.js +10 -0
- package/dist/plugins/timezone/index.js.map +9 -0
- package/dist/schemas.d.ts +66 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/types.d.ts +96 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +50 -49
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
createPlugin
|
|
4
|
+
} from "./index-77f5wgyc.js";
|
|
5
|
+
|
|
6
|
+
// src/plugins/relative-time/index.ts
|
|
7
|
+
var THRESHOLDS = {
|
|
8
|
+
second: 45,
|
|
9
|
+
minute: 90,
|
|
10
|
+
hour: 2700,
|
|
11
|
+
day: 75600,
|
|
12
|
+
month: 2592000,
|
|
13
|
+
year: 31536000
|
|
14
|
+
};
|
|
15
|
+
function formatRelativeTime(diff, isPast) {
|
|
16
|
+
const absDiff = Math.abs(diff);
|
|
17
|
+
if (absDiff === 0 || absDiff < THRESHOLDS.second) {
|
|
18
|
+
return isPast || diff === 0 ? "a few seconds ago" : "in a few seconds";
|
|
19
|
+
} else if (absDiff < THRESHOLDS.minute) {
|
|
20
|
+
return isPast ? "a minute ago" : "in a minute";
|
|
21
|
+
} else if (absDiff < THRESHOLDS.hour) {
|
|
22
|
+
const value = Math.round(absDiff / 60);
|
|
23
|
+
const unit = value === 1 ? "minute" : "minutes";
|
|
24
|
+
return isPast ? `${value} ${unit} ago` : `in ${value} ${unit}`;
|
|
25
|
+
} else if (absDiff < THRESHOLDS.day) {
|
|
26
|
+
const value = Math.round(absDiff / 3600);
|
|
27
|
+
if (value === 1) {
|
|
28
|
+
return isPast ? "an hour ago" : "in an hour";
|
|
29
|
+
}
|
|
30
|
+
const unit = "hours";
|
|
31
|
+
return isPast ? `${value} ${unit} ago` : `in ${value} ${unit}`;
|
|
32
|
+
} else if (absDiff < THRESHOLDS.month) {
|
|
33
|
+
const value = Math.round(absDiff / 86400);
|
|
34
|
+
if (value === 1) {
|
|
35
|
+
return isPast ? "a day ago" : "in a day";
|
|
36
|
+
}
|
|
37
|
+
const unit = "days";
|
|
38
|
+
return isPast ? `${value} ${unit} ago` : `in ${value} ${unit}`;
|
|
39
|
+
} else if (absDiff < THRESHOLDS.year) {
|
|
40
|
+
const value = Math.round(absDiff / 2592000);
|
|
41
|
+
if (value === 1) {
|
|
42
|
+
return isPast ? "a month ago" : "in a month";
|
|
43
|
+
}
|
|
44
|
+
const unit = "months";
|
|
45
|
+
return isPast ? `${value} ${unit} ago` : `in ${value} ${unit}`;
|
|
46
|
+
} else {
|
|
47
|
+
const value = Math.round(absDiff / 31536000);
|
|
48
|
+
if (value === 1) {
|
|
49
|
+
return isPast ? "a year ago" : "in a year";
|
|
50
|
+
}
|
|
51
|
+
const unit = "years";
|
|
52
|
+
return isPast ? `${value} ${unit} ago` : `in ${value} ${unit}`;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
var relativeTimePlugin = createPlugin("relative-time", (DateTimeClass) => {
|
|
56
|
+
DateTimeClass.prototype.fromNow = function() {
|
|
57
|
+
const now = new DateTimeClass;
|
|
58
|
+
const diffMs = this.valueOf() - now.valueOf();
|
|
59
|
+
const diffSeconds = diffMs / 1000;
|
|
60
|
+
const isPast = diffSeconds < 0;
|
|
61
|
+
return formatRelativeTime(diffSeconds, isPast);
|
|
62
|
+
};
|
|
63
|
+
DateTimeClass.prototype.toNow = function() {
|
|
64
|
+
const now = new DateTimeClass;
|
|
65
|
+
const diffMs = this.valueOf() - now.valueOf();
|
|
66
|
+
const diffSeconds = diffMs / 1000;
|
|
67
|
+
const isPast = diffSeconds < 0;
|
|
68
|
+
return formatRelativeTime(diffSeconds, !isPast);
|
|
69
|
+
};
|
|
70
|
+
DateTimeClass.prototype.from = function(other) {
|
|
71
|
+
const diffMs = this.valueOf() - other.valueOf();
|
|
72
|
+
const diffSeconds = diffMs / 1000;
|
|
73
|
+
const isPast = diffSeconds < 0;
|
|
74
|
+
return formatRelativeTime(diffSeconds, isPast);
|
|
75
|
+
};
|
|
76
|
+
DateTimeClass.prototype.to = function(other) {
|
|
77
|
+
const diffMs = this.valueOf() - other.valueOf();
|
|
78
|
+
const diffSeconds = diffMs / 1000;
|
|
79
|
+
const isPast = diffSeconds < 0;
|
|
80
|
+
return formatRelativeTime(diffSeconds, !isPast);
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
export { relativeTimePlugin };
|
|
85
|
+
|
|
86
|
+
//# debugId=74801CA1F705E1AD64756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/plugins/relative-time/index.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { DateTime } from \"../../core/datetime\";\nimport type { DateTimeClass } from \"../../types\";\nimport { createPlugin } from \"../plugin-base\";\n\n/**\n * Time thresholds for relative time formatting (in seconds)\n */\nconst THRESHOLDS = {\n second: 45, // 0-45 seconds\n minute: 90, // 45-90 seconds\n hour: 2700, // 45 minutes - 1.5 hours\n day: 75600, // 21 hours threshold for switching to days\n month: 2592000, // ~30 days\n year: 31536000, // ~365 days\n};\n\n/**\n * Formats a relative time string\n * @param diff - Time difference in seconds\n * @param isPast - Whether the time is in the past\n * @returns Human-readable relative time string\n */\nfunction formatRelativeTime(diff: number, isPast: boolean): string {\n const absDiff = Math.abs(diff);\n\n // Handle zero difference\n if (absDiff === 0 || absDiff < THRESHOLDS.second) {\n return isPast || diff === 0 ? \"a few seconds ago\" : \"in a few seconds\";\n } else if (absDiff < THRESHOLDS.minute) {\n return isPast ? \"a minute ago\" : \"in a minute\";\n } else if (absDiff < THRESHOLDS.hour) {\n const value = Math.round(absDiff / 60);\n const unit = value === 1 ? \"minute\" : \"minutes\";\n return isPast ? `${value} ${unit} ago` : `in ${value} ${unit}`;\n } else if (absDiff < THRESHOLDS.day) {\n const value = Math.round(absDiff / 3600);\n if (value === 1) {\n return isPast ? \"an hour ago\" : \"in an hour\";\n }\n const unit = \"hours\";\n return isPast ? `${value} ${unit} ago` : `in ${value} ${unit}`;\n } else if (absDiff < THRESHOLDS.month) {\n const value = Math.round(absDiff / 86400);\n if (value === 1) {\n return isPast ? \"a day ago\" : \"in a day\";\n }\n const unit = \"days\";\n return isPast ? `${value} ${unit} ago` : `in ${value} ${unit}`;\n } else if (absDiff < THRESHOLDS.year) {\n const value = Math.round(absDiff / 2592000);\n if (value === 1) {\n return isPast ? \"a month ago\" : \"in a month\";\n }\n const unit = \"months\";\n return isPast ? `${value} ${unit} ago` : `in ${value} ${unit}`;\n } else {\n const value = Math.round(absDiff / 31536000);\n if (value === 1) {\n return isPast ? \"a year ago\" : \"in a year\";\n }\n const unit = \"years\";\n return isPast ? `${value} ${unit} ago` : `in ${value} ${unit}`;\n }\n}\n\n/**\n * Extended DateTime interface with relative time methods\n */\ndeclare module \"../../core/datetime\" {\n interface DateTime {\n /**\n * Returns a human-readable string representing the time from now\n * @returns Relative time string (e.g., \"2 hours ago\", \"in 3 days\")\n *\n * @example\n * ```ts\n * dt.fromNow() // \"2 hours ago\"\n * dt.fromNow() // \"in 3 days\"\n * ```\n */\n fromNow(): string;\n\n /**\n * Returns a human-readable string representing the time to now\n * Opposite of fromNow()\n * @returns Relative time string\n *\n * @example\n * ```ts\n * dt.toNow() // \"in 2 hours\"\n * dt.toNow() // \"3 days ago\"\n * ```\n */\n toNow(): string;\n\n /**\n * Returns a human-readable string representing the time from another DateTime\n * @param other - DateTime instance to compare against\n * @returns Relative time string\n *\n * @example\n * ```ts\n * dt1.from(dt2) // \"2 hours ago\"\n * dt1.from(dt2) // \"in 3 days\"\n * ```\n */\n from(other: DateTime): string;\n\n /**\n * Returns a human-readable string representing the time to another DateTime\n * Opposite of from()\n * @param other - DateTime instance to compare against\n * @returns Relative time string\n *\n * @example\n * ```ts\n * dt1.to(dt2) // \"in 2 hours\"\n * dt1.to(dt2) // \"3 days ago\"\n * ```\n */\n to(other: DateTime): string;\n }\n}\n\n/**\n * Relative Time plugin for DateTime\n * Adds methods for human-readable relative time formatting\n */\nexport const relativeTimePlugin = createPlugin(\n \"relative-time\",\n (DateTimeClass: DateTimeClass) => {\n // Add instance methods\n DateTimeClass.prototype.fromNow = function (): string {\n const now = new DateTimeClass();\n const diffMs = this.valueOf() - now.valueOf();\n const diffSeconds = diffMs / 1000;\n const isPast = diffSeconds < 0;\n\n return formatRelativeTime(diffSeconds, isPast);\n };\n\n DateTimeClass.prototype.toNow = function (): string {\n const now = new DateTimeClass();\n const diffMs = this.valueOf() - now.valueOf();\n const diffSeconds = diffMs / 1000;\n const isPast = diffSeconds < 0;\n\n // Reverse the direction for toNow\n return formatRelativeTime(diffSeconds, !isPast);\n };\n\n DateTimeClass.prototype.from = function (other: DateTime): string {\n const diffMs = this.valueOf() - other.valueOf();\n const diffSeconds = diffMs / 1000;\n const isPast = diffSeconds < 0;\n\n return formatRelativeTime(diffSeconds, isPast);\n };\n\n DateTimeClass.prototype.to = function (other: DateTime): string {\n const diffMs = this.valueOf() - other.valueOf();\n const diffSeconds = diffMs / 1000;\n const isPast = diffSeconds < 0;\n\n // Reverse the direction for to\n return formatRelativeTime(diffSeconds, !isPast);\n };\n },\n);\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;AAOA,IAAM,aAAa;AAAA,EAChB,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,KAAK;AAAA,EACL,OAAO;AAAA,EACP,MAAM;AACT;AAQA,SAAS,kBAAkB,CAAC,MAAc,QAAyB;AAAA,EAChE,MAAM,UAAU,KAAK,IAAI,IAAI;AAAA,EAG7B,IAAI,YAAY,KAAK,UAAU,WAAW,QAAQ;AAAA,IAC/C,OAAO,UAAU,SAAS,IAAI,sBAAsB;AAAA,EACvD,EAAO,SAAI,UAAU,WAAW,QAAQ;AAAA,IACrC,OAAO,SAAS,iBAAiB;AAAA,EACpC,EAAO,SAAI,UAAU,WAAW,MAAM;AAAA,IACnC,MAAM,QAAQ,KAAK,MAAM,UAAU,EAAE;AAAA,IACrC,MAAM,OAAO,UAAU,IAAI,WAAW;AAAA,IACtC,OAAO,SAAS,GAAG,SAAS,aAAa,MAAM,SAAS;AAAA,EAC3D,EAAO,SAAI,UAAU,WAAW,KAAK;AAAA,IAClC,MAAM,QAAQ,KAAK,MAAM,UAAU,IAAI;AAAA,IACvC,IAAI,UAAU,GAAG;AAAA,MACd,OAAO,SAAS,gBAAgB;AAAA,IACnC;AAAA,IACA,MAAM,OAAO;AAAA,IACb,OAAO,SAAS,GAAG,SAAS,aAAa,MAAM,SAAS;AAAA,EAC3D,EAAO,SAAI,UAAU,WAAW,OAAO;AAAA,IACpC,MAAM,QAAQ,KAAK,MAAM,UAAU,KAAK;AAAA,IACxC,IAAI,UAAU,GAAG;AAAA,MACd,OAAO,SAAS,cAAc;AAAA,IACjC;AAAA,IACA,MAAM,OAAO;AAAA,IACb,OAAO,SAAS,GAAG,SAAS,aAAa,MAAM,SAAS;AAAA,EAC3D,EAAO,SAAI,UAAU,WAAW,MAAM;AAAA,IACnC,MAAM,QAAQ,KAAK,MAAM,UAAU,OAAO;AAAA,IAC1C,IAAI,UAAU,GAAG;AAAA,MACd,OAAO,SAAS,gBAAgB;AAAA,IACnC;AAAA,IACA,MAAM,OAAO;AAAA,IACb,OAAO,SAAS,GAAG,SAAS,aAAa,MAAM,SAAS;AAAA,EAC3D,EAAO;AAAA,IACJ,MAAM,QAAQ,KAAK,MAAM,UAAU,QAAQ;AAAA,IAC3C,IAAI,UAAU,GAAG;AAAA,MACd,OAAO,SAAS,eAAe;AAAA,IAClC;AAAA,IACA,MAAM,OAAO;AAAA,IACb,OAAO,SAAS,GAAG,SAAS,aAAa,MAAM,SAAS;AAAA;AAAA;AAmEvD,IAAM,qBAAqB,aAC/B,iBACA,CAAC,kBAAiC;AAAA,EAE/B,cAAc,UAAU,UAAU,QAAS,GAAW;AAAA,IACnD,MAAM,MAAM,IAAI;AAAA,IAChB,MAAM,SAAS,KAAK,QAAQ,IAAI,IAAI,QAAQ;AAAA,IAC5C,MAAM,cAAc,SAAS;AAAA,IAC7B,MAAM,SAAS,cAAc;AAAA,IAE7B,OAAO,mBAAmB,aAAa,MAAM;AAAA;AAAA,EAGhD,cAAc,UAAU,QAAQ,QAAS,GAAW;AAAA,IACjD,MAAM,MAAM,IAAI;AAAA,IAChB,MAAM,SAAS,KAAK,QAAQ,IAAI,IAAI,QAAQ;AAAA,IAC5C,MAAM,cAAc,SAAS;AAAA,IAC7B,MAAM,SAAS,cAAc;AAAA,IAG7B,OAAO,mBAAmB,aAAa,CAAC,MAAM;AAAA;AAAA,EAGjD,cAAc,UAAU,OAAO,QAAS,CAAC,OAAyB;AAAA,IAC/D,MAAM,SAAS,KAAK,QAAQ,IAAI,MAAM,QAAQ;AAAA,IAC9C,MAAM,cAAc,SAAS;AAAA,IAC7B,MAAM,SAAS,cAAc;AAAA,IAE7B,OAAO,mBAAmB,aAAa,MAAM;AAAA;AAAA,EAGhD,cAAc,UAAU,KAAK,QAAS,CAAC,OAAyB;AAAA,IAC7D,MAAM,SAAS,KAAK,QAAQ,IAAI,MAAM,QAAQ;AAAA,IAC9C,MAAM,cAAc,SAAS;AAAA,IAC7B,MAAM,SAAS,cAAc;AAAA,IAG7B,OAAO,mBAAmB,aAAa,CAAC,MAAM;AAAA;AAAA,CAGvD;",
|
|
8
|
+
"debugId": "74801CA1F705E1AD64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
createPlugin
|
|
4
|
+
} from "./index-77f5wgyc.js";
|
|
5
|
+
|
|
6
|
+
// src/plugins/format/tokens.ts
|
|
7
|
+
function pad(num, length) {
|
|
8
|
+
return num.toString().padStart(length, "0");
|
|
9
|
+
}
|
|
10
|
+
var MONTH_NAMES = [
|
|
11
|
+
"January",
|
|
12
|
+
"February",
|
|
13
|
+
"March",
|
|
14
|
+
"April",
|
|
15
|
+
"May",
|
|
16
|
+
"June",
|
|
17
|
+
"July",
|
|
18
|
+
"August",
|
|
19
|
+
"September",
|
|
20
|
+
"October",
|
|
21
|
+
"November",
|
|
22
|
+
"December"
|
|
23
|
+
];
|
|
24
|
+
var MONTH_NAMES_SHORT = [
|
|
25
|
+
"Jan",
|
|
26
|
+
"Feb",
|
|
27
|
+
"Mar",
|
|
28
|
+
"Apr",
|
|
29
|
+
"May",
|
|
30
|
+
"Jun",
|
|
31
|
+
"Jul",
|
|
32
|
+
"Aug",
|
|
33
|
+
"Sep",
|
|
34
|
+
"Oct",
|
|
35
|
+
"Nov",
|
|
36
|
+
"Dec"
|
|
37
|
+
];
|
|
38
|
+
var DAY_NAMES = [
|
|
39
|
+
"Sunday",
|
|
40
|
+
"Monday",
|
|
41
|
+
"Tuesday",
|
|
42
|
+
"Wednesday",
|
|
43
|
+
"Thursday",
|
|
44
|
+
"Friday",
|
|
45
|
+
"Saturday"
|
|
46
|
+
];
|
|
47
|
+
var DAY_NAMES_SHORT = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
48
|
+
var DAY_NAMES_MIN = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
|
|
49
|
+
var FORMAT_TOKENS = {
|
|
50
|
+
YYYY: (dt) => dt.year().toString(),
|
|
51
|
+
YY: (dt) => pad(dt.year() % 100, 2),
|
|
52
|
+
MMMM: (dt) => MONTH_NAMES[dt.month()],
|
|
53
|
+
MMM: (dt) => MONTH_NAMES_SHORT[dt.month()],
|
|
54
|
+
MM: (dt) => pad(dt.month() + 1, 2),
|
|
55
|
+
M: (dt) => (dt.month() + 1).toString(),
|
|
56
|
+
DD: (dt) => pad(dt.date(), 2),
|
|
57
|
+
D: (dt) => dt.date().toString(),
|
|
58
|
+
dddd: (dt) => DAY_NAMES[dt.day()],
|
|
59
|
+
ddd: (dt) => DAY_NAMES_SHORT[dt.day()],
|
|
60
|
+
dd: (dt) => DAY_NAMES_MIN[dt.day()],
|
|
61
|
+
HH: (dt) => pad(dt.hour(), 2),
|
|
62
|
+
H: (dt) => dt.hour().toString(),
|
|
63
|
+
hh: (dt) => {
|
|
64
|
+
const hour = dt.hour();
|
|
65
|
+
const hour12 = hour % 12 || 12;
|
|
66
|
+
return pad(hour12, 2);
|
|
67
|
+
},
|
|
68
|
+
h: (dt) => {
|
|
69
|
+
const hour = dt.hour();
|
|
70
|
+
const hour12 = hour % 12 || 12;
|
|
71
|
+
return hour12.toString();
|
|
72
|
+
},
|
|
73
|
+
mm: (dt) => pad(dt.minute(), 2),
|
|
74
|
+
m: (dt) => dt.minute().toString(),
|
|
75
|
+
ss: (dt) => pad(dt.second(), 2),
|
|
76
|
+
s: (dt) => dt.second().toString(),
|
|
77
|
+
SSS: (dt) => pad(dt.millisecond(), 3),
|
|
78
|
+
A: (dt) => dt.hour() >= 12 ? "PM" : "AM",
|
|
79
|
+
a: (dt) => dt.hour() >= 12 ? "pm" : "am",
|
|
80
|
+
d: (dt) => dt.day().toString()
|
|
81
|
+
};
|
|
82
|
+
var TOKEN_REGEX = /\[([^\]]+)\]|YYYY|MMMM|MMM|MM|M|dddd|ddd|DD|dd|D|d|HH|hh|H|h|mm|m|ss|s|SSS|YY|A|a/g;
|
|
83
|
+
function parseFormat(dt, formatStr) {
|
|
84
|
+
return formatStr.replace(TOKEN_REGEX, (match, escapedText) => {
|
|
85
|
+
if (escapedText !== undefined) {
|
|
86
|
+
return escapedText;
|
|
87
|
+
}
|
|
88
|
+
const tokenFn = FORMAT_TOKENS[match];
|
|
89
|
+
return tokenFn ? tokenFn(dt) : match;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/plugins/format/index.ts
|
|
94
|
+
var formatPlugin = createPlugin("format", (DateTimeClass) => {
|
|
95
|
+
DateTimeClass.prototype.format = function(formatStr) {
|
|
96
|
+
if (!formatStr) {
|
|
97
|
+
return this.toISO();
|
|
98
|
+
}
|
|
99
|
+
return parseFormat(this, formatStr);
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
export { formatPlugin };
|
|
104
|
+
|
|
105
|
+
//# debugId=CB8C67E7BEEEFF1C64756E2164756E21
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/plugins/format/tokens.ts", "../src/plugins/format/index.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { DateTime } from \"../../core/datetime\";\n\n/**\n * Pads a number with leading zeros\n */\nfunction pad(num: number, length: number): string {\n return num.toString().padStart(length, \"0\");\n}\n\n/**\n * Month names (full)\n */\nconst MONTH_NAMES = [\n \"January\",\n \"February\",\n \"March\",\n \"April\",\n \"May\",\n \"June\",\n \"July\",\n \"August\",\n \"September\",\n \"October\",\n \"November\",\n \"December\",\n];\n\n/**\n * Month names (abbreviated)\n */\nconst MONTH_NAMES_SHORT = [\n \"Jan\",\n \"Feb\",\n \"Mar\",\n \"Apr\",\n \"May\",\n \"Jun\",\n \"Jul\",\n \"Aug\",\n \"Sep\",\n \"Oct\",\n \"Nov\",\n \"Dec\",\n];\n\n/**\n * Day of week names (full)\n */\nconst DAY_NAMES = [\n \"Sunday\",\n \"Monday\",\n \"Tuesday\",\n \"Wednesday\",\n \"Thursday\",\n \"Friday\",\n \"Saturday\",\n];\n\n/**\n * Day of week names (abbreviated)\n */\nconst DAY_NAMES_SHORT = [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\"];\n\n/**\n * Day of week names (two letters)\n */\nconst DAY_NAMES_MIN = [\"Su\", \"Mo\", \"Tu\", \"We\", \"Th\", \"Fr\", \"Sa\"];\n\n/**\n * Format token definitions\n * Maps token strings to functions that extract the value from a DateTime instance\n */\nexport const FORMAT_TOKENS: Record<string, (dt: DateTime) => string> = {\n // Year\n YYYY: (dt) => dt.year().toString(),\n YY: (dt) => pad(dt.year() % 100, 2),\n\n // Month\n MMMM: (dt) => MONTH_NAMES[dt.month()]!,\n MMM: (dt) => MONTH_NAMES_SHORT[dt.month()]!,\n MM: (dt) => pad(dt.month() + 1, 2),\n M: (dt) => (dt.month() + 1).toString(),\n\n // Day of Month\n DD: (dt) => pad(dt.date(), 2),\n D: (dt) => dt.date().toString(),\n\n // Day of Week\n dddd: (dt) => DAY_NAMES[dt.day()]!,\n ddd: (dt) => DAY_NAMES_SHORT[dt.day()]!,\n dd: (dt) => DAY_NAMES_MIN[dt.day()]!,\n\n // Hour (24-hour)\n HH: (dt) => pad(dt.hour(), 2),\n H: (dt) => dt.hour().toString(),\n\n // Hour (12-hour)\n hh: (dt) => {\n const hour = dt.hour();\n const hour12 = hour % 12 || 12;\n return pad(hour12, 2);\n },\n h: (dt) => {\n const hour = dt.hour();\n const hour12 = hour % 12 || 12;\n return hour12.toString();\n },\n\n // Minute\n mm: (dt) => pad(dt.minute(), 2),\n m: (dt) => dt.minute().toString(),\n\n // Second\n ss: (dt) => pad(dt.second(), 2),\n s: (dt) => dt.second().toString(),\n\n // Millisecond\n SSS: (dt) => pad(dt.millisecond(), 3),\n\n // AM/PM\n A: (dt) => (dt.hour() >= 12 ? \"PM\" : \"AM\"),\n a: (dt) => (dt.hour() >= 12 ? \"pm\" : \"am\"),\n\n // Day of week number\n d: (dt) => dt.day().toString(),\n};\n\n/**\n * Regular expression to match format tokens\n * Matches escaped text first, then longest tokens to avoid partial matches\n * Order is critical: longer tokens must come before shorter ones\n */\nexport const TOKEN_REGEX =\n /\\[([^\\]]+)\\]|YYYY|MMMM|MMM|MM|M|dddd|ddd|DD|dd|D|d|HH|hh|H|h|mm|m|ss|s|SSS|YY|A|a/g;\n\n/**\n * Parses a format string and replaces tokens with actual values\n * @param dt - DateTime instance\n * @param formatStr - Format string with tokens\n * @returns Formatted date string\n */\nexport function parseFormat(dt: DateTime, formatStr: string): string {\n return formatStr.replace(TOKEN_REGEX, (match, escapedText) => {\n // If it's escaped text in brackets, return without brackets\n if (escapedText !== undefined) {\n return escapedText;\n }\n\n // Otherwise, look up the token and apply it\n const tokenFn = FORMAT_TOKENS[match];\n return tokenFn ? tokenFn(dt) : match;\n });\n}\n",
|
|
6
|
+
"import type { DateTime } from \"../../core/datetime\";\nimport type { DateTimeClass } from \"../../types\";\nimport { createPlugin } from \"../plugin-base\";\nimport { parseFormat } from \"./tokens\";\n\n/**\n * Default format string\n */\nconst DEFAULT_FORMAT = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n\n/**\n * Extended DateTime interface with format method\n */\ndeclare module \"../../core/datetime\" {\n interface DateTime {\n /**\n * Formats the date using a format string\n * @param formatStr - Format string with tokens (default: ISO format)\n * @returns Formatted date string\n *\n * @example\n * ```ts\n * dt.format(\"YYYY-MM-DD\") // \"2024-01-15\"\n * dt.format(\"MM/DD/YYYY\") // \"01/15/2024\"\n * dt.format(\"MMMM D, YYYY\") // \"January 15, 2024\"\n * dt.format(\"h:mm A\") // \"2:30 PM\"\n * dt.format(\"[Year:] YYYY\") // \"Year: 2024\"\n * ```\n *\n * Available tokens:\n * - YYYY: 4-digit year\n * - YY: 2-digit year\n * - MMMM: Full month name\n * - MMM: Abbreviated month name\n * - MM: 2-digit month\n * - M: Month number\n * - DD: 2-digit day of month\n * - D: Day of month\n * - dddd: Full day name\n * - ddd: Abbreviated day name\n * - dd: Min day name\n * - d: Day of week (0-6)\n * - HH: 2-digit hour (24-hour)\n * - H: Hour (24-hour)\n * - hh: 2-digit hour (12-hour)\n * - h: Hour (12-hour)\n * - mm: 2-digit minute\n * - m: Minute\n * - ss: 2-digit second\n * - s: Second\n * - SSS: Millisecond\n * - A: AM/PM (uppercase)\n * - a: am/pm (lowercase)\n * - [text]: Escaped text (literal)\n */\n format(formatStr?: string): string;\n }\n}\n\n/**\n * Format plugin for DateTime\n * Adds format() method for custom date formatting\n */\nexport const formatPlugin = createPlugin(\n \"format\",\n (DateTimeClass: DateTimeClass) => {\n // Add instance method\n DateTimeClass.prototype.format = function (formatStr?: string): string {\n // If no format string provided, return ISO string\n if (!formatStr) {\n return this.toISO();\n }\n\n // Parse the format string and replace tokens\n return parseFormat(this, formatStr);\n };\n },\n);\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": "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",
|
|
9
|
+
"debugId": "CB8C67E7BEEEFF1C64756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { DateTime } from "./core/datetime.ts";
|
|
2
|
+
export { datetime } from "./core/factory.ts";
|
|
3
|
+
export { InvalidDateError, PluginError } from "./errors.ts";
|
|
4
|
+
export { createPlugin, isPlugin, isValidPluginName } from "./plugins/index.ts";
|
|
5
|
+
export { DateInputSchema } from "./schemas.ts";
|
|
6
|
+
export type { DateInput, DateTimeClass, DateTimeConfig, DateTimePlugin, FormatOptions, ParseOptions, TimeUnit, } from "./types.ts";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAG/E,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,YAAY,EACT,SAAS,EACT,aAAa,EACb,cAAc,EACd,cAAc,EACd,aAAa,EACb,YAAY,EACZ,QAAQ,GACV,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import"./index-a78jd9k6.js";
|
|
3
|
+
import"./index-9jdtsp4s.js";
|
|
4
|
+
import"./index-v3cytzbp.js";
|
|
5
|
+
import"./index-rtm7bpky.js";
|
|
6
|
+
import {
|
|
7
|
+
createPlugin,
|
|
8
|
+
isPlugin,
|
|
9
|
+
isValidPluginName
|
|
10
|
+
} from "./index-77f5wgyc.js";
|
|
11
|
+
|
|
12
|
+
// src/errors.ts
|
|
13
|
+
class DateTimeError extends Error {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "DateTimeError";
|
|
17
|
+
if (Error.captureStackTrace) {
|
|
18
|
+
Error.captureStackTrace(this, this.constructor);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class InvalidDateError extends DateTimeError {
|
|
24
|
+
input;
|
|
25
|
+
constructor(message = "Invalid date", input) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.input = input;
|
|
28
|
+
this.name = "InvalidDateError";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
class PluginError extends DateTimeError {
|
|
32
|
+
pluginName;
|
|
33
|
+
constructor(message, pluginName) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.pluginName = pluginName;
|
|
36
|
+
this.name = "PluginError";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/schemas.ts
|
|
41
|
+
import { z } from "zod";
|
|
42
|
+
var TimeUnitSchema = z.enum([
|
|
43
|
+
"millisecond",
|
|
44
|
+
"second",
|
|
45
|
+
"minute",
|
|
46
|
+
"hour",
|
|
47
|
+
"day",
|
|
48
|
+
"week",
|
|
49
|
+
"month",
|
|
50
|
+
"year"
|
|
51
|
+
]);
|
|
52
|
+
var DateInputSchema = z.union([
|
|
53
|
+
z.date(),
|
|
54
|
+
z.string(),
|
|
55
|
+
z.number(),
|
|
56
|
+
z.custom((val) => val !== null && typeof val === "object" && ("toDate" in val) && typeof val.toDate === "function", {
|
|
57
|
+
message: "Expected DateTime instance"
|
|
58
|
+
})
|
|
59
|
+
]);
|
|
60
|
+
var DateTimeConfigSchema = z.object({
|
|
61
|
+
timezone: z.string().optional(),
|
|
62
|
+
locale: z.string().optional(),
|
|
63
|
+
utc: z.boolean().optional(),
|
|
64
|
+
strict: z.boolean().optional()
|
|
65
|
+
});
|
|
66
|
+
var FormatOptionsSchema = z.object({
|
|
67
|
+
locale: z.string().optional(),
|
|
68
|
+
timezone: z.string().optional()
|
|
69
|
+
});
|
|
70
|
+
var ParseOptionsSchema = z.object({
|
|
71
|
+
strict: z.boolean().optional(),
|
|
72
|
+
format: z.string().optional(),
|
|
73
|
+
timezone: z.string().optional()
|
|
74
|
+
});
|
|
75
|
+
var ISODateSchema = z.string().regex(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?(Z|[+-]\d{2}:\d{2})$/, {
|
|
76
|
+
message: "Invalid ISO 8601 datetime format. Expected: YYYY-MM-DDTHH:mm:ss.sssZ"
|
|
77
|
+
});
|
|
78
|
+
var ISODateOnlySchema = z.string().regex(/^\d{4}-\d{2}-\d{2}$/, {
|
|
79
|
+
message: "Invalid ISO 8601 date format. Expected: YYYY-MM-DD"
|
|
80
|
+
});
|
|
81
|
+
var ISOTimeOnlySchema = z.string().regex(/^\d{2}:\d{2}:\d{2}(\.\d{1,3})?$/, {
|
|
82
|
+
message: "Invalid ISO 8601 time format. Expected: HH:mm:ss or HH:mm:ss.sss"
|
|
83
|
+
});
|
|
84
|
+
var DateTimePluginSchema = z.object({
|
|
85
|
+
name: z.string().min(1, "Plugin name cannot be empty"),
|
|
86
|
+
install: z.function()
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// src/core/datetime.ts
|
|
90
|
+
class DateTime {
|
|
91
|
+
_date;
|
|
92
|
+
static plugins = new Map;
|
|
93
|
+
constructor(input) {
|
|
94
|
+
if (input === undefined) {
|
|
95
|
+
this._date = new Date;
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (typeof input === "number" && Number.isNaN(input)) {
|
|
99
|
+
this._date = new Date(NaN);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (input instanceof Date && Number.isNaN(input.getTime())) {
|
|
103
|
+
this._date = new Date(input.getTime());
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const validation = DateInputSchema.safeParse(input);
|
|
107
|
+
if (!validation.success) {
|
|
108
|
+
throw new InvalidDateError(`Invalid date input: ${validation.error.message}`, input);
|
|
109
|
+
}
|
|
110
|
+
if (input instanceof Date) {
|
|
111
|
+
this._date = new Date(input.getTime());
|
|
112
|
+
} else if (typeof input === "string") {
|
|
113
|
+
this._date = new Date(input);
|
|
114
|
+
} else if (typeof input === "number") {
|
|
115
|
+
this._date = new Date(input);
|
|
116
|
+
} else if (this.isDateTimeInstance(input)) {
|
|
117
|
+
this._date = new Date(input.valueOf());
|
|
118
|
+
} else {
|
|
119
|
+
throw new InvalidDateError("Unsupported date input type", input);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
isDateTimeInstance(val) {
|
|
123
|
+
return val !== null && typeof val === "object" && "toDate" in val && typeof val.toDate === "function";
|
|
124
|
+
}
|
|
125
|
+
isValid() {
|
|
126
|
+
return !Number.isNaN(this._date.getTime());
|
|
127
|
+
}
|
|
128
|
+
toDate() {
|
|
129
|
+
return new Date(this._date.getTime());
|
|
130
|
+
}
|
|
131
|
+
toISO() {
|
|
132
|
+
return this._date.toISOString();
|
|
133
|
+
}
|
|
134
|
+
valueOf() {
|
|
135
|
+
return this._date.getTime();
|
|
136
|
+
}
|
|
137
|
+
addMilliseconds(amount) {
|
|
138
|
+
return new DateTime(this.valueOf() + amount);
|
|
139
|
+
}
|
|
140
|
+
addSeconds(amount) {
|
|
141
|
+
return this.addMilliseconds(amount * 1000);
|
|
142
|
+
}
|
|
143
|
+
addMinutes(amount) {
|
|
144
|
+
return this.addMilliseconds(amount * 60 * 1000);
|
|
145
|
+
}
|
|
146
|
+
addHours(amount) {
|
|
147
|
+
return this.addMilliseconds(amount * 60 * 60 * 1000);
|
|
148
|
+
}
|
|
149
|
+
addDays(amount) {
|
|
150
|
+
const newDate = this.toDate();
|
|
151
|
+
newDate.setDate(newDate.getDate() + amount);
|
|
152
|
+
return new DateTime(newDate);
|
|
153
|
+
}
|
|
154
|
+
addWeeks(amount) {
|
|
155
|
+
return this.addDays(amount * 7);
|
|
156
|
+
}
|
|
157
|
+
addMonths(amount) {
|
|
158
|
+
const newDate = this.toDate();
|
|
159
|
+
newDate.setMonth(newDate.getMonth() + amount);
|
|
160
|
+
return new DateTime(newDate);
|
|
161
|
+
}
|
|
162
|
+
addYears(amount) {
|
|
163
|
+
const newDate = this.toDate();
|
|
164
|
+
newDate.setFullYear(newDate.getFullYear() + amount);
|
|
165
|
+
return new DateTime(newDate);
|
|
166
|
+
}
|
|
167
|
+
subtractMilliseconds(amount) {
|
|
168
|
+
return this.addMilliseconds(-amount);
|
|
169
|
+
}
|
|
170
|
+
subtractSeconds(amount) {
|
|
171
|
+
return this.addSeconds(-amount);
|
|
172
|
+
}
|
|
173
|
+
subtractMinutes(amount) {
|
|
174
|
+
return this.addMinutes(-amount);
|
|
175
|
+
}
|
|
176
|
+
subtractHours(amount) {
|
|
177
|
+
return this.addHours(-amount);
|
|
178
|
+
}
|
|
179
|
+
subtractDays(amount) {
|
|
180
|
+
return this.addDays(-amount);
|
|
181
|
+
}
|
|
182
|
+
subtractWeeks(amount) {
|
|
183
|
+
return this.addWeeks(-amount);
|
|
184
|
+
}
|
|
185
|
+
subtractMonths(amount) {
|
|
186
|
+
return this.addMonths(-amount);
|
|
187
|
+
}
|
|
188
|
+
subtractYears(amount) {
|
|
189
|
+
return this.addYears(-amount);
|
|
190
|
+
}
|
|
191
|
+
isBefore(other) {
|
|
192
|
+
return this.valueOf() < other.valueOf();
|
|
193
|
+
}
|
|
194
|
+
isAfter(other) {
|
|
195
|
+
return this.valueOf() > other.valueOf();
|
|
196
|
+
}
|
|
197
|
+
isSame(other) {
|
|
198
|
+
return this.valueOf() === other.valueOf();
|
|
199
|
+
}
|
|
200
|
+
isSameOrBefore(other) {
|
|
201
|
+
return this.valueOf() <= other.valueOf();
|
|
202
|
+
}
|
|
203
|
+
isSameOrAfter(other) {
|
|
204
|
+
return this.valueOf() >= other.valueOf();
|
|
205
|
+
}
|
|
206
|
+
isBetween(start, end, inclusive = false) {
|
|
207
|
+
const thisTime = this.valueOf();
|
|
208
|
+
const startTime = start.valueOf();
|
|
209
|
+
const endTime = end.valueOf();
|
|
210
|
+
if (inclusive) {
|
|
211
|
+
return thisTime >= startTime && thisTime <= endTime;
|
|
212
|
+
}
|
|
213
|
+
return thisTime > startTime && thisTime < endTime;
|
|
214
|
+
}
|
|
215
|
+
year() {
|
|
216
|
+
return this._date.getUTCFullYear();
|
|
217
|
+
}
|
|
218
|
+
month() {
|
|
219
|
+
return this._date.getUTCMonth();
|
|
220
|
+
}
|
|
221
|
+
date() {
|
|
222
|
+
return this._date.getUTCDate();
|
|
223
|
+
}
|
|
224
|
+
day() {
|
|
225
|
+
return this._date.getUTCDay();
|
|
226
|
+
}
|
|
227
|
+
hour() {
|
|
228
|
+
return this._date.getUTCHours();
|
|
229
|
+
}
|
|
230
|
+
minute() {
|
|
231
|
+
return this._date.getUTCMinutes();
|
|
232
|
+
}
|
|
233
|
+
second() {
|
|
234
|
+
return this._date.getUTCSeconds();
|
|
235
|
+
}
|
|
236
|
+
millisecond() {
|
|
237
|
+
return this._date.getUTCMilliseconds();
|
|
238
|
+
}
|
|
239
|
+
setYear(year) {
|
|
240
|
+
const newDate = this.toDate();
|
|
241
|
+
newDate.setUTCFullYear(year);
|
|
242
|
+
return new DateTime(newDate);
|
|
243
|
+
}
|
|
244
|
+
setMonth(month) {
|
|
245
|
+
const newDate = this.toDate();
|
|
246
|
+
newDate.setUTCMonth(month);
|
|
247
|
+
return new DateTime(newDate);
|
|
248
|
+
}
|
|
249
|
+
setDate(date) {
|
|
250
|
+
const newDate = this.toDate();
|
|
251
|
+
newDate.setUTCDate(date);
|
|
252
|
+
return new DateTime(newDate);
|
|
253
|
+
}
|
|
254
|
+
setHour(hour) {
|
|
255
|
+
const newDate = this.toDate();
|
|
256
|
+
newDate.setUTCHours(hour);
|
|
257
|
+
return new DateTime(newDate);
|
|
258
|
+
}
|
|
259
|
+
setMinute(minute) {
|
|
260
|
+
const newDate = this.toDate();
|
|
261
|
+
newDate.setUTCMinutes(minute);
|
|
262
|
+
return new DateTime(newDate);
|
|
263
|
+
}
|
|
264
|
+
setSecond(second) {
|
|
265
|
+
const newDate = this.toDate();
|
|
266
|
+
newDate.setUTCSeconds(second);
|
|
267
|
+
return new DateTime(newDate);
|
|
268
|
+
}
|
|
269
|
+
setMillisecond(millisecond) {
|
|
270
|
+
const newDate = this.toDate();
|
|
271
|
+
newDate.setUTCMilliseconds(millisecond);
|
|
272
|
+
return new DateTime(newDate);
|
|
273
|
+
}
|
|
274
|
+
startOfDay() {
|
|
275
|
+
const newDate = this.toDate();
|
|
276
|
+
newDate.setUTCHours(0, 0, 0, 0);
|
|
277
|
+
return new DateTime(newDate);
|
|
278
|
+
}
|
|
279
|
+
endOfDay() {
|
|
280
|
+
const newDate = this.toDate();
|
|
281
|
+
newDate.setUTCHours(23, 59, 59, 999);
|
|
282
|
+
return new DateTime(newDate);
|
|
283
|
+
}
|
|
284
|
+
startOfHour() {
|
|
285
|
+
const newDate = this.toDate();
|
|
286
|
+
newDate.setUTCMinutes(0, 0, 0);
|
|
287
|
+
return new DateTime(newDate);
|
|
288
|
+
}
|
|
289
|
+
endOfHour() {
|
|
290
|
+
const newDate = this.toDate();
|
|
291
|
+
newDate.setUTCMinutes(59, 59, 999);
|
|
292
|
+
return new DateTime(newDate);
|
|
293
|
+
}
|
|
294
|
+
startOfWeek() {
|
|
295
|
+
const newDate = this.toDate();
|
|
296
|
+
const day = newDate.getUTCDay();
|
|
297
|
+
newDate.setUTCDate(newDate.getUTCDate() - day);
|
|
298
|
+
newDate.setUTCHours(0, 0, 0, 0);
|
|
299
|
+
return new DateTime(newDate);
|
|
300
|
+
}
|
|
301
|
+
endOfWeek() {
|
|
302
|
+
const newDate = this.toDate();
|
|
303
|
+
const day = newDate.getUTCDay();
|
|
304
|
+
newDate.setUTCDate(newDate.getUTCDate() + (6 - day));
|
|
305
|
+
newDate.setUTCHours(23, 59, 59, 999);
|
|
306
|
+
return new DateTime(newDate);
|
|
307
|
+
}
|
|
308
|
+
startOfMonth() {
|
|
309
|
+
const newDate = this.toDate();
|
|
310
|
+
newDate.setUTCDate(1);
|
|
311
|
+
newDate.setUTCHours(0, 0, 0, 0);
|
|
312
|
+
return new DateTime(newDate);
|
|
313
|
+
}
|
|
314
|
+
endOfMonth() {
|
|
315
|
+
const newDate = this.toDate();
|
|
316
|
+
newDate.setUTCMonth(newDate.getUTCMonth() + 1, 0);
|
|
317
|
+
newDate.setUTCHours(23, 59, 59, 999);
|
|
318
|
+
return new DateTime(newDate);
|
|
319
|
+
}
|
|
320
|
+
startOfYear() {
|
|
321
|
+
const newDate = this.toDate();
|
|
322
|
+
newDate.setUTCMonth(0, 1);
|
|
323
|
+
newDate.setUTCHours(0, 0, 0, 0);
|
|
324
|
+
return new DateTime(newDate);
|
|
325
|
+
}
|
|
326
|
+
endOfYear() {
|
|
327
|
+
const newDate = this.toDate();
|
|
328
|
+
newDate.setUTCMonth(11, 31);
|
|
329
|
+
newDate.setUTCHours(23, 59, 59, 999);
|
|
330
|
+
return new DateTime(newDate);
|
|
331
|
+
}
|
|
332
|
+
diff(other, unit = "millisecond") {
|
|
333
|
+
const diff = this.valueOf() - other.valueOf();
|
|
334
|
+
switch (unit) {
|
|
335
|
+
case "millisecond":
|
|
336
|
+
return diff;
|
|
337
|
+
case "second":
|
|
338
|
+
return diff / 1000;
|
|
339
|
+
case "minute":
|
|
340
|
+
return diff / (1000 * 60);
|
|
341
|
+
case "hour":
|
|
342
|
+
return diff / (1000 * 60 * 60);
|
|
343
|
+
case "day":
|
|
344
|
+
return diff / (1000 * 60 * 60 * 24);
|
|
345
|
+
case "week":
|
|
346
|
+
return diff / (1000 * 60 * 60 * 24 * 7);
|
|
347
|
+
case "month": {
|
|
348
|
+
const thisDate = this.toDate();
|
|
349
|
+
const otherDate = other.toDate();
|
|
350
|
+
const yearDiff = thisDate.getUTCFullYear() - otherDate.getUTCFullYear();
|
|
351
|
+
const monthDiff = thisDate.getUTCMonth() - otherDate.getUTCMonth();
|
|
352
|
+
return yearDiff * 12 + monthDiff;
|
|
353
|
+
}
|
|
354
|
+
case "year": {
|
|
355
|
+
return diff / (1000 * 60 * 60 * 24 * 365.25);
|
|
356
|
+
}
|
|
357
|
+
default:
|
|
358
|
+
return diff;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
static extend(plugin, options) {
|
|
362
|
+
if (DateTime.plugins.has(plugin.name)) {
|
|
363
|
+
throw new PluginError(`Plugin ${plugin.name} is already registered`, plugin.name);
|
|
364
|
+
}
|
|
365
|
+
DateTime.plugins.set(plugin.name, plugin);
|
|
366
|
+
plugin.install(DateTime, options);
|
|
367
|
+
}
|
|
368
|
+
static hasPlugin(name) {
|
|
369
|
+
return DateTime.plugins.has(name);
|
|
370
|
+
}
|
|
371
|
+
static getPlugin(name) {
|
|
372
|
+
return DateTime.plugins.get(name);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
// src/core/factory.ts
|
|
376
|
+
function datetime(input) {
|
|
377
|
+
return new DateTime(input);
|
|
378
|
+
}
|
|
379
|
+
export {
|
|
380
|
+
isValidPluginName,
|
|
381
|
+
isPlugin,
|
|
382
|
+
datetime,
|
|
383
|
+
createPlugin,
|
|
384
|
+
PluginError,
|
|
385
|
+
InvalidDateError,
|
|
386
|
+
DateTime,
|
|
387
|
+
DateInputSchema
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
//# debugId=D8EEE46614FED6E064756E2164756E21
|