@hapiboo/date 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/class.d.ts +49 -0
- package/dist/class.js +161 -0
- package/dist/holiday.d.ts +34 -0
- package/dist/holiday.js +80 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +12 -0
- package/dist/service.d.ts +8 -0
- package/dist/service.js +49 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.js +29 -0
- package/eslint.config.mjs +16 -0
- package/package.dev.json +30 -0
- package/package.json +29 -0
- package/package.public.json +29 -0
- package/src/class.ts +171 -0
- package/src/holiday.ts +102 -0
- package/src/index.ts +6 -0
- package/src/service.ts +43 -0
- package/src/utils.ts +26 -0
- package/tsconfig.base.json +18 -0
- package/tsconfig.dev.json +7 -0
- package/tsconfig.public.json +7 -0
package/dist/class.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export declare class DateObject {
|
|
2
|
+
private val;
|
|
3
|
+
private constructor();
|
|
4
|
+
static set(epochMilliseconds: number): DateObject;
|
|
5
|
+
static now(): DateObject;
|
|
6
|
+
static today(): DateObject;
|
|
7
|
+
static yesterday(): DateObject;
|
|
8
|
+
static startOfWeek(): DateObject;
|
|
9
|
+
static startOfMonth(): DateObject;
|
|
10
|
+
static startOfYear(): DateObject;
|
|
11
|
+
get _value(): string;
|
|
12
|
+
get hour(): number;
|
|
13
|
+
get day(): number;
|
|
14
|
+
get month(): number;
|
|
15
|
+
get year(): number;
|
|
16
|
+
get weekday(): number;
|
|
17
|
+
toString(): string;
|
|
18
|
+
toUtc(): string;
|
|
19
|
+
toLocalDateShort(locale: string): string;
|
|
20
|
+
toMonthShortString(): string;
|
|
21
|
+
toWeekdayString(): string;
|
|
22
|
+
private clone;
|
|
23
|
+
addMilisecond(count: number): DateObject;
|
|
24
|
+
addSecond(count: number): DateObject;
|
|
25
|
+
addMinute(count: number): DateObject;
|
|
26
|
+
addHour(count: number): DateObject;
|
|
27
|
+
addDay(count: number): DateObject;
|
|
28
|
+
addMonth(count: number): DateObject;
|
|
29
|
+
addYear(count: number): DateObject;
|
|
30
|
+
resetYear(): DateObject;
|
|
31
|
+
resetDay(): DateObject;
|
|
32
|
+
removeMinutes(): DateObject;
|
|
33
|
+
removeHours(): DateObject;
|
|
34
|
+
removeDays(): DateObject;
|
|
35
|
+
equals(dateToCompare: DateObject): boolean;
|
|
36
|
+
sameDay(dateToCompare: DateObject): boolean;
|
|
37
|
+
isDateBetween(startDate: DateObject, endDate: DateObject): boolean;
|
|
38
|
+
isGreater(dateToCompare: DateObject): boolean;
|
|
39
|
+
isGreaterOrEqual(dateToCompare: DateObject): boolean;
|
|
40
|
+
isSmaller(dateToCompare: DateObject): boolean;
|
|
41
|
+
isSmallerOrEqual(dateToCompare: DateObject): boolean;
|
|
42
|
+
compare(dateToCompare: DateObject): number;
|
|
43
|
+
isWeekday(): boolean;
|
|
44
|
+
isWeekend(): boolean;
|
|
45
|
+
isHoliday(): boolean;
|
|
46
|
+
valueOf(): number;
|
|
47
|
+
getDaysDifference(dateToCompare: DateObject): number;
|
|
48
|
+
getHoursDifference(dateToCompare: DateObject): number;
|
|
49
|
+
}
|
package/dist/class.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DateObject = void 0;
|
|
4
|
+
const luxon_1 = require("luxon");
|
|
5
|
+
const service_1 = require("./service");
|
|
6
|
+
const holiday_1 = require("./holiday");
|
|
7
|
+
class DateObject {
|
|
8
|
+
constructor(val) {
|
|
9
|
+
this.val = val;
|
|
10
|
+
}
|
|
11
|
+
static set(epochMilliseconds) {
|
|
12
|
+
return new DateObject(luxon_1.DateTime.fromMillis(epochMilliseconds));
|
|
13
|
+
}
|
|
14
|
+
static now() {
|
|
15
|
+
return new DateObject(luxon_1.DateTime.local());
|
|
16
|
+
}
|
|
17
|
+
static today() {
|
|
18
|
+
const now = luxon_1.DateTime.local();
|
|
19
|
+
return service_1.dateService.date(now.year, now.month, now.day);
|
|
20
|
+
}
|
|
21
|
+
static yesterday() {
|
|
22
|
+
return DateObject.today().addDay(-1);
|
|
23
|
+
}
|
|
24
|
+
static startOfWeek() {
|
|
25
|
+
const today = DateObject.today();
|
|
26
|
+
return today.addDay(1 - today.weekday);
|
|
27
|
+
}
|
|
28
|
+
static startOfMonth() {
|
|
29
|
+
const now = luxon_1.DateTime.local();
|
|
30
|
+
return service_1.dateService.date(now.year, now.month, 1);
|
|
31
|
+
}
|
|
32
|
+
static startOfYear() {
|
|
33
|
+
const now = luxon_1.DateTime.local();
|
|
34
|
+
return service_1.dateService.date(now.year, 1, 1);
|
|
35
|
+
}
|
|
36
|
+
get _value() {
|
|
37
|
+
return this.toString();
|
|
38
|
+
}
|
|
39
|
+
get hour() {
|
|
40
|
+
return this.val.hour;
|
|
41
|
+
}
|
|
42
|
+
get day() {
|
|
43
|
+
return this.val.day;
|
|
44
|
+
}
|
|
45
|
+
get month() {
|
|
46
|
+
return this.val.month;
|
|
47
|
+
}
|
|
48
|
+
get year() {
|
|
49
|
+
return this.val.year;
|
|
50
|
+
}
|
|
51
|
+
get weekday() {
|
|
52
|
+
return this.val.weekday;
|
|
53
|
+
}
|
|
54
|
+
toString() {
|
|
55
|
+
return this.val.toString();
|
|
56
|
+
}
|
|
57
|
+
toUtc() {
|
|
58
|
+
return this.val.toUTC().toString();
|
|
59
|
+
}
|
|
60
|
+
toLocalDateShort(locale) {
|
|
61
|
+
return this.val.setLocale(locale).toLocaleString(luxon_1.DateTime.DATE_SHORT);
|
|
62
|
+
}
|
|
63
|
+
toMonthShortString() {
|
|
64
|
+
return this.val.toFormat('LLL').toUpperCase();
|
|
65
|
+
}
|
|
66
|
+
toWeekdayString() {
|
|
67
|
+
return this.val.toFormat('ccc');
|
|
68
|
+
}
|
|
69
|
+
clone() {
|
|
70
|
+
return luxon_1.DateTime.local(this.val.year, this.val.month, this.val.day, this.val.hour, this.val.minute, this.val.second, this.val.millisecond);
|
|
71
|
+
}
|
|
72
|
+
addMilisecond(count) {
|
|
73
|
+
return new DateObject(this.clone().plus(luxon_1.Duration.fromObject({ milliseconds: count })));
|
|
74
|
+
}
|
|
75
|
+
addSecond(count) {
|
|
76
|
+
return new DateObject(this.clone().plus(luxon_1.Duration.fromObject({ seconds: count })));
|
|
77
|
+
}
|
|
78
|
+
addMinute(count) {
|
|
79
|
+
return new DateObject(this.clone().plus(luxon_1.Duration.fromObject({ minutes: count })));
|
|
80
|
+
}
|
|
81
|
+
addHour(count) {
|
|
82
|
+
return new DateObject(this.clone().plus(luxon_1.Duration.fromObject({ hours: count })));
|
|
83
|
+
}
|
|
84
|
+
addDay(count) {
|
|
85
|
+
return new DateObject(this.clone().plus(luxon_1.Duration.fromObject({ days: count })));
|
|
86
|
+
}
|
|
87
|
+
addMonth(count) {
|
|
88
|
+
return new DateObject(this.clone().plus(luxon_1.Duration.fromObject({ months: count })));
|
|
89
|
+
}
|
|
90
|
+
addYear(count) {
|
|
91
|
+
return new DateObject(this.clone().plus(luxon_1.Duration.fromObject({ years: count })));
|
|
92
|
+
}
|
|
93
|
+
resetYear() {
|
|
94
|
+
return new DateObject(this.clone().set({ year: 2000 }));
|
|
95
|
+
}
|
|
96
|
+
resetDay() {
|
|
97
|
+
return new DateObject(this.resetYear().val.set({ month: 1, day: 1 }));
|
|
98
|
+
}
|
|
99
|
+
removeMinutes() {
|
|
100
|
+
return new DateObject(this.clone().set({ minute: 0, second: 0, millisecond: 0 }));
|
|
101
|
+
}
|
|
102
|
+
removeHours() {
|
|
103
|
+
return new DateObject(this.removeMinutes().val.set({ hour: 0 }));
|
|
104
|
+
}
|
|
105
|
+
removeDays() {
|
|
106
|
+
return new DateObject(this.removeHours().val.set({ day: 1 }));
|
|
107
|
+
}
|
|
108
|
+
equals(dateToCompare) {
|
|
109
|
+
return +this.val === +dateToCompare.val;
|
|
110
|
+
}
|
|
111
|
+
sameDay(dateToCompare) {
|
|
112
|
+
return this.val.hasSame(dateToCompare.val, 'day') &&
|
|
113
|
+
this.val.hasSame(dateToCompare.val, 'month') &&
|
|
114
|
+
this.val.hasSame(dateToCompare.val, 'year');
|
|
115
|
+
}
|
|
116
|
+
isDateBetween(startDate, endDate) {
|
|
117
|
+
return this.val >= startDate.val && this.val <= endDate.val;
|
|
118
|
+
}
|
|
119
|
+
isGreater(dateToCompare) {
|
|
120
|
+
return this.val > dateToCompare.val;
|
|
121
|
+
}
|
|
122
|
+
isGreaterOrEqual(dateToCompare) {
|
|
123
|
+
return this.val >= dateToCompare.val;
|
|
124
|
+
}
|
|
125
|
+
isSmaller(dateToCompare) {
|
|
126
|
+
return this.val < dateToCompare.val;
|
|
127
|
+
}
|
|
128
|
+
isSmallerOrEqual(dateToCompare) {
|
|
129
|
+
return this.val <= dateToCompare.val;
|
|
130
|
+
}
|
|
131
|
+
compare(dateToCompare) {
|
|
132
|
+
if (this.equals(dateToCompare)) {
|
|
133
|
+
return 0;
|
|
134
|
+
}
|
|
135
|
+
else if (this.isGreater(dateToCompare)) {
|
|
136
|
+
return 1;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
return -1;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
isWeekday() {
|
|
143
|
+
return this.weekday < 6;
|
|
144
|
+
}
|
|
145
|
+
isWeekend() {
|
|
146
|
+
return this.weekday > 5;
|
|
147
|
+
}
|
|
148
|
+
isHoliday() {
|
|
149
|
+
return holiday_1.holidayService.isHoliday(this);
|
|
150
|
+
}
|
|
151
|
+
valueOf() {
|
|
152
|
+
return this.val.valueOf();
|
|
153
|
+
}
|
|
154
|
+
getDaysDifference(dateToCompare) {
|
|
155
|
+
return luxon_1.Interval.fromDateTimes(this.val, dateToCompare.val).count('day');
|
|
156
|
+
}
|
|
157
|
+
getHoursDifference(dateToCompare) {
|
|
158
|
+
return luxon_1.Interval.fromDateTimes(this.val, dateToCompare.val).toDuration(['hours', 'minutes']).hours;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.DateObject = DateObject;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DateObject } from './class';
|
|
2
|
+
export declare class Holiday {
|
|
3
|
+
name: string;
|
|
4
|
+
month: number;
|
|
5
|
+
day: number | {
|
|
6
|
+
weekday: number;
|
|
7
|
+
count: number;
|
|
8
|
+
};
|
|
9
|
+
constructor(name: string, month: number, day: number | {
|
|
10
|
+
weekday: number;
|
|
11
|
+
count: number;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export declare namespace holidayService {
|
|
15
|
+
type KnownHolidays = {
|
|
16
|
+
ChristmasEve: Holiday;
|
|
17
|
+
ChristmasDay: Holiday;
|
|
18
|
+
Christmas2ndDay: Holiday;
|
|
19
|
+
NewYearDay: Holiday;
|
|
20
|
+
USIndependenceDay: Holiday;
|
|
21
|
+
USThanksgivingDay: Holiday;
|
|
22
|
+
USMemorialDay: Holiday;
|
|
23
|
+
USLaborDay: Holiday;
|
|
24
|
+
PLIndependenceDay: Holiday;
|
|
25
|
+
PLLaborDay: Holiday;
|
|
26
|
+
PLConstitutionDay: Holiday;
|
|
27
|
+
PLAllSaintsDay: Holiday;
|
|
28
|
+
};
|
|
29
|
+
export const known: KnownHolidays;
|
|
30
|
+
export function registerHoliday(holiday: Holiday): void;
|
|
31
|
+
export function isHoliday(date: DateObject): boolean;
|
|
32
|
+
export function checkHoliday(date: DateObject, holiday: Holiday): boolean;
|
|
33
|
+
export {};
|
|
34
|
+
}
|
package/dist/holiday.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.holidayService = exports.Holiday = void 0;
|
|
4
|
+
const service_1 = require("./service");
|
|
5
|
+
class Holiday {
|
|
6
|
+
constructor(name, month, day) {
|
|
7
|
+
this.name = name;
|
|
8
|
+
this.month = month;
|
|
9
|
+
this.day = day;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.Holiday = Holiday;
|
|
13
|
+
function getNthDay(year, month, weekday, count) {
|
|
14
|
+
const firstDayOfMonth = service_1.dateService.date(year, month, 1);
|
|
15
|
+
// is target day before or after first day
|
|
16
|
+
const offsetThreshold = firstDayOfMonth.weekday - weekday;
|
|
17
|
+
let offsetFromTargetDay;
|
|
18
|
+
if (offsetThreshold > 0) {
|
|
19
|
+
// get to target day if target is after first day
|
|
20
|
+
offsetFromTargetDay = 7 - offsetThreshold;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
// reverse threshold to get to target from first day
|
|
24
|
+
offsetFromTargetDay = offsetThreshold * -1;
|
|
25
|
+
}
|
|
26
|
+
const firstOccurenceOfTargetDay = firstDayOfMonth.addDay(offsetFromTargetDay);
|
|
27
|
+
const nthDay = firstOccurenceOfTargetDay.addDay((count - 1) * 7);
|
|
28
|
+
return nthDay;
|
|
29
|
+
}
|
|
30
|
+
var holidayService;
|
|
31
|
+
(function (holidayService) {
|
|
32
|
+
const _list = [];
|
|
33
|
+
holidayService.known = {
|
|
34
|
+
ChristmasEve: new Holiday('Christmas Day', 12, 24),
|
|
35
|
+
ChristmasDay: new Holiday('Christmas Day', 12, 25),
|
|
36
|
+
Christmas2ndDay: new Holiday('Christmas Day', 12, 26),
|
|
37
|
+
NewYearDay: new Holiday('New Year\'s Day', 1, 2),
|
|
38
|
+
USIndependenceDay: new Holiday('Independence Day', 7, 4),
|
|
39
|
+
USThanksgivingDay: new Holiday('Thanksgiving Day', 11, { weekday: 4, count: 4 }),
|
|
40
|
+
USMemorialDay: new Holiday('Memorial Day', 5, { weekday: 1, count: -1 }),
|
|
41
|
+
USLaborDay: new Holiday('Labor Day', 9, { weekday: 1, count: 1 }),
|
|
42
|
+
PLIndependenceDay: new Holiday('Independence Day', 11, 11),
|
|
43
|
+
PLConstitutionDay: new Holiday('Constitution Day', 5, 3),
|
|
44
|
+
PLLaborDay: new Holiday('Labor Day', 5, 1),
|
|
45
|
+
PLAllSaintsDay: new Holiday('All Saints Day', 11, 1),
|
|
46
|
+
};
|
|
47
|
+
function registerHoliday(holiday) {
|
|
48
|
+
_list.push(holiday);
|
|
49
|
+
}
|
|
50
|
+
holidayService.registerHoliday = registerHoliday;
|
|
51
|
+
function isHoliday(date) {
|
|
52
|
+
return _list.some((holiday) => {
|
|
53
|
+
return checkHoliday(date, holiday);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
holidayService.isHoliday = isHoliday;
|
|
57
|
+
function checkHoliday(date, holiday) {
|
|
58
|
+
const justDate = date.removeHours();
|
|
59
|
+
let holy;
|
|
60
|
+
if (typeof holiday.day === 'number') {
|
|
61
|
+
//fixed holiday
|
|
62
|
+
holy = service_1.dateService.date(justDate.year, holiday.month, holiday.day);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
//moveable holiday
|
|
66
|
+
if (holiday.day.count > 1) {
|
|
67
|
+
//we're counting from beginning
|
|
68
|
+
//let's get nth weekday in specific month
|
|
69
|
+
holy = getNthDay(justDate.year, holiday.month, holiday.day.weekday, holiday.day.count);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
//we're counting from the end
|
|
73
|
+
//let's get started from the first weekday in next month
|
|
74
|
+
holy = getNthDay(holiday.month === 12 ? justDate.year + 1 : justDate.year, holiday.month === 12 ? 1 : holiday.month + 1, holiday.day.weekday, 1).addDay(7 * holiday.day.count);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return justDate.sameDay(holy);
|
|
78
|
+
}
|
|
79
|
+
holidayService.checkHoliday = checkHoliday;
|
|
80
|
+
})(holidayService || (exports.holidayService = holidayService = {}));
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dateUtils = exports.holidayService = exports.dateService = exports.Holiday = exports.DateObject = void 0;
|
|
4
|
+
const service_1 = require("./service");
|
|
5
|
+
Object.defineProperty(exports, "dateService", { enumerable: true, get: function () { return service_1.dateService; } });
|
|
6
|
+
const class_1 = require("./class");
|
|
7
|
+
Object.defineProperty(exports, "DateObject", { enumerable: true, get: function () { return class_1.DateObject; } });
|
|
8
|
+
const utils_1 = require("./utils");
|
|
9
|
+
Object.defineProperty(exports, "dateUtils", { enumerable: true, get: function () { return utils_1.dateUtils; } });
|
|
10
|
+
const holiday_1 = require("./holiday");
|
|
11
|
+
Object.defineProperty(exports, "holidayService", { enumerable: true, get: function () { return holiday_1.holidayService; } });
|
|
12
|
+
Object.defineProperty(exports, "Holiday", { enumerable: true, get: function () { return holiday_1.Holiday; } });
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DateObject } from './class';
|
|
2
|
+
export declare namespace dateService {
|
|
3
|
+
function initialize(defaultZoneName: string, defaultLocaleName: string): void;
|
|
4
|
+
function parseUtcDate(datetime: string): DateObject | undefined;
|
|
5
|
+
function date(year: number, month: number, day: number): DateObject;
|
|
6
|
+
function datetime(year: number, month: number, day: number, hour: number, minute: number): DateObject;
|
|
7
|
+
function daytime(day: DateObject, hour?: number, minute?: number): DateObject;
|
|
8
|
+
}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dateService = void 0;
|
|
4
|
+
const luxon_1 = require("luxon");
|
|
5
|
+
const class_1 = require("./class");
|
|
6
|
+
luxon_1.Settings.defaultZone = 'utc';
|
|
7
|
+
var dateService;
|
|
8
|
+
(function (dateService) {
|
|
9
|
+
function initialize(defaultZoneName, defaultLocaleName) {
|
|
10
|
+
luxon_1.Settings.defaultZone = defaultZoneName;
|
|
11
|
+
luxon_1.Settings.defaultLocale = defaultLocaleName;
|
|
12
|
+
}
|
|
13
|
+
dateService.initialize = initialize;
|
|
14
|
+
function parseUtcDate(datetime) {
|
|
15
|
+
if (datetime) {
|
|
16
|
+
const temp = luxon_1.DateTime.fromISO(datetime);
|
|
17
|
+
if (temp) {
|
|
18
|
+
if (temp.isValid) {
|
|
19
|
+
return class_1.DateObject.set(temp.setZone(luxon_1.Settings.defaultZone).valueOf());
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
console.error(`Invalid date parsing value: '${datetime}'`);
|
|
23
|
+
console.error(temp.invalidReason);
|
|
24
|
+
console.error(temp.invalidExplanation);
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
dateService.parseUtcDate = parseUtcDate;
|
|
37
|
+
function date(year, month, day) {
|
|
38
|
+
return class_1.DateObject.set(luxon_1.DateTime.local(year, month, day).valueOf());
|
|
39
|
+
}
|
|
40
|
+
dateService.date = date;
|
|
41
|
+
function datetime(year, month, day, hour, minute) {
|
|
42
|
+
return class_1.DateObject.set(luxon_1.DateTime.local(year, month, day, hour, minute).valueOf());
|
|
43
|
+
}
|
|
44
|
+
dateService.datetime = datetime;
|
|
45
|
+
function daytime(day, hour = 0, minute = 0) {
|
|
46
|
+
return datetime(day.year, day.month, day.day, hour, minute);
|
|
47
|
+
}
|
|
48
|
+
dateService.daytime = daytime;
|
|
49
|
+
})(dateService || (exports.dateService = dateService = {}));
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DateObject } from './class';
|
|
2
|
+
export declare namespace dateUtils {
|
|
3
|
+
function checkPeriod(value: DateObject, start: DateObject, stop?: DateObject): boolean;
|
|
4
|
+
function checkDatePeriod(value: DateObject, start: DateObject, stop: DateObject): boolean;
|
|
5
|
+
function checkSameYearPeriod(value: DateObject, start: DateObject, stop: DateObject): boolean;
|
|
6
|
+
function checkSameDayPeriod(value: DateObject, start: DateObject, stop: DateObject): boolean;
|
|
7
|
+
}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dateUtils = void 0;
|
|
4
|
+
const service_1 = require("./service");
|
|
5
|
+
var dateUtils;
|
|
6
|
+
(function (dateUtils) {
|
|
7
|
+
function checkPeriod(value, start, stop) {
|
|
8
|
+
if (!stop) {
|
|
9
|
+
stop = service_1.dateService.date(3000, 12, 31);
|
|
10
|
+
}
|
|
11
|
+
return checkDatePeriod(value, start, stop);
|
|
12
|
+
}
|
|
13
|
+
dateUtils.checkPeriod = checkPeriod;
|
|
14
|
+
function checkDatePeriod(value, start, stop) {
|
|
15
|
+
return value >= start && value <= stop;
|
|
16
|
+
}
|
|
17
|
+
dateUtils.checkDatePeriod = checkDatePeriod;
|
|
18
|
+
function checkSameYearPeriod(value, start, stop) {
|
|
19
|
+
return checkDatePeriod(value, start, stop);
|
|
20
|
+
}
|
|
21
|
+
dateUtils.checkSameYearPeriod = checkSameYearPeriod;
|
|
22
|
+
function checkSameDayPeriod(value, start, stop) {
|
|
23
|
+
const val = value.resetDay();
|
|
24
|
+
const str = start.resetDay();
|
|
25
|
+
const stp = stop.resetDay();
|
|
26
|
+
return checkDatePeriod(val, str, stp);
|
|
27
|
+
}
|
|
28
|
+
dateUtils.checkSameDayPeriod = checkSameDayPeriod;
|
|
29
|
+
})(dateUtils || (exports.dateUtils = dateUtils = {}));
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import tseslint from "typescript-eslint";
|
|
2
|
+
import { defineConfig } from "eslint/config";
|
|
3
|
+
|
|
4
|
+
export default defineConfig([
|
|
5
|
+
...tseslint.configs.recommended,
|
|
6
|
+
{
|
|
7
|
+
files: ["**/*.{ts,mts,cts}"],
|
|
8
|
+
rules: {
|
|
9
|
+
"@typescript-eslint/no-namespace": "off",
|
|
10
|
+
indent: ['error', 4],
|
|
11
|
+
'linebreak-style': ['error', 'unix'],
|
|
12
|
+
quotes: ['error', 'single'],
|
|
13
|
+
semi: ['error', 'always'],
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
]);
|
package/package.dev.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hapiboo/date",
|
|
3
|
+
"version": "2.0.1-dev",
|
|
4
|
+
"description": "MK13 Studio Hapiboo - date services",
|
|
5
|
+
"author": "MK13 Studio",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"s_public": "cp package.public.json package.json",
|
|
11
|
+
"s_dev": "cp package.dev.json package.json",
|
|
12
|
+
"s_clean": "rm -f pnpm-lock.yaml && rm -rf node_modules dist",
|
|
13
|
+
"s_dependency": "cd .. && cd __core && pnpm s_build",
|
|
14
|
+
"s_build": "tsc --project tsconfig.dev.json",
|
|
15
|
+
"s_lint": "eslint 'src/*.ts'",
|
|
16
|
+
"s_lint_fix": "eslint 'src/*.ts' --fix",
|
|
17
|
+
"s_prepare": "pnpm s_clean && pnpm i && pnpm s_lint && pnpm s_build"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"luxon": "^3.7.1"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/luxon": "^3.6.2",
|
|
24
|
+
"@typescript-eslint/eslint-plugin": "^8.38.0",
|
|
25
|
+
"@typescript-eslint/parser": "^8.38.0",
|
|
26
|
+
"eslint": "^9.32.0",
|
|
27
|
+
"typescript": "5.8.3",
|
|
28
|
+
"typescript-eslint": "^8.38.0"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hapiboo/date",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "MK13 Studio Hapiboo - date services",
|
|
5
|
+
"author": "MK13 Studio",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"luxon": "^3.7.1"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/luxon": "^3.6.2",
|
|
14
|
+
"@typescript-eslint/eslint-plugin": "^8.38.0",
|
|
15
|
+
"@typescript-eslint/parser": "^8.38.0",
|
|
16
|
+
"eslint": "^9.32.0",
|
|
17
|
+
"typescript": "5.8.3",
|
|
18
|
+
"typescript-eslint": "^8.38.0"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"s_public": "cp package.public.json package.json",
|
|
22
|
+
"s_dev": "cp package.dev.json package.json",
|
|
23
|
+
"s_clean": "rm -f pnpm-lock.yaml && rm -rf node_modules dist",
|
|
24
|
+
"s_build": "tsc --project tsconfig.public.json",
|
|
25
|
+
"s_lint": "eslint 'src/*.ts'",
|
|
26
|
+
"s_check": "bash ../../scripts/publish.sh ./",
|
|
27
|
+
"s_publish": "pnpm s_clean && pnpm i -w=false && pnpm s_lint && pnpm s_build && pnpm s_check && pnpm s_dev"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hapiboo/date",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "MK13 Studio Hapiboo - date services",
|
|
5
|
+
"author": "MK13 Studio",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"s_public": "cp package.public.json package.json",
|
|
11
|
+
"s_dev": "cp package.dev.json package.json",
|
|
12
|
+
"s_clean": "rm -f pnpm-lock.yaml && rm -rf node_modules dist",
|
|
13
|
+
"s_build": "tsc --project tsconfig.public.json",
|
|
14
|
+
"s_lint": "eslint 'src/*.ts'",
|
|
15
|
+
"s_check": "bash ../../scripts/publish.sh ./",
|
|
16
|
+
"s_publish": "pnpm s_clean && pnpm i -w=false && pnpm s_lint && pnpm s_build && pnpm s_check && pnpm s_dev"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"luxon": "^3.7.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/luxon": "^3.6.2",
|
|
23
|
+
"@typescript-eslint/eslint-plugin": "^8.38.0",
|
|
24
|
+
"@typescript-eslint/parser": "^8.38.0",
|
|
25
|
+
"eslint": "^9.32.0",
|
|
26
|
+
"typescript": "5.8.3",
|
|
27
|
+
"typescript-eslint": "^8.38.0"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/class.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { DateTime, Duration, Interval } from 'luxon';
|
|
2
|
+
import { dateService } from './service';
|
|
3
|
+
import { holidayService } from './holiday';
|
|
4
|
+
|
|
5
|
+
export class DateObject {
|
|
6
|
+
private val: DateTime;
|
|
7
|
+
|
|
8
|
+
private constructor(val: DateTime) {
|
|
9
|
+
this.val = val;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static set(epochMilliseconds: number): DateObject {
|
|
13
|
+
return new DateObject(DateTime.fromMillis(epochMilliseconds));
|
|
14
|
+
}
|
|
15
|
+
static now(): DateObject {
|
|
16
|
+
return new DateObject(DateTime.local());
|
|
17
|
+
}
|
|
18
|
+
static today(): DateObject {
|
|
19
|
+
const now = DateTime.local();
|
|
20
|
+
return dateService.date(now.year, now.month, now.day);
|
|
21
|
+
}
|
|
22
|
+
static yesterday(): DateObject {
|
|
23
|
+
return DateObject.today().addDay(-1);
|
|
24
|
+
}
|
|
25
|
+
static startOfWeek(): DateObject {
|
|
26
|
+
const today = DateObject.today();
|
|
27
|
+
return today.addDay(1 - today.weekday);
|
|
28
|
+
}
|
|
29
|
+
static startOfMonth(): DateObject {
|
|
30
|
+
const now = DateTime.local();
|
|
31
|
+
return dateService.date(now.year, now.month, 1);
|
|
32
|
+
}
|
|
33
|
+
static startOfYear(): DateObject {
|
|
34
|
+
const now = DateTime.local();
|
|
35
|
+
return dateService.date(now.year, 1, 1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get _value(): string {
|
|
39
|
+
return this.toString();
|
|
40
|
+
}
|
|
41
|
+
get hour(): number {
|
|
42
|
+
return this.val.hour;
|
|
43
|
+
}
|
|
44
|
+
get day(): number {
|
|
45
|
+
return this.val.day;
|
|
46
|
+
}
|
|
47
|
+
get month(): number {
|
|
48
|
+
return this.val.month;
|
|
49
|
+
}
|
|
50
|
+
get year(): number {
|
|
51
|
+
return this.val.year;
|
|
52
|
+
}
|
|
53
|
+
get weekday(): number {
|
|
54
|
+
return this.val.weekday;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
toString(): string {
|
|
58
|
+
return this.val.toString();
|
|
59
|
+
}
|
|
60
|
+
toUtc(): string {
|
|
61
|
+
return this.val.toUTC().toString();
|
|
62
|
+
}
|
|
63
|
+
toLocalDateShort(locale: string): string {
|
|
64
|
+
return this.val.setLocale(locale).toLocaleString(DateTime.DATE_SHORT);
|
|
65
|
+
}
|
|
66
|
+
toMonthShortString(): string {
|
|
67
|
+
return this.val.toFormat('LLL').toUpperCase();
|
|
68
|
+
}
|
|
69
|
+
toWeekdayString(): string {
|
|
70
|
+
return this.val.toFormat('ccc');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private clone() : DateTime {
|
|
74
|
+
return DateTime.local(this.val.year, this.val.month, this.val.day, this.val.hour, this.val.minute, this.val.second, this.val.millisecond);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
addMilisecond(count: number): DateObject {
|
|
78
|
+
return new DateObject(this.clone().plus(Duration.fromObject({milliseconds: count})));
|
|
79
|
+
}
|
|
80
|
+
addSecond(count: number): DateObject {
|
|
81
|
+
return new DateObject(this.clone().plus(Duration.fromObject({seconds: count})));
|
|
82
|
+
}
|
|
83
|
+
addMinute(count: number): DateObject {
|
|
84
|
+
return new DateObject(this.clone().plus(Duration.fromObject({minutes: count})));
|
|
85
|
+
}
|
|
86
|
+
addHour(count: number): DateObject {
|
|
87
|
+
return new DateObject(this.clone().plus(Duration.fromObject({hours: count})));
|
|
88
|
+
}
|
|
89
|
+
addDay(count: number): DateObject {
|
|
90
|
+
return new DateObject(this.clone().plus(Duration.fromObject({days: count})));
|
|
91
|
+
}
|
|
92
|
+
addMonth(count: number): DateObject {
|
|
93
|
+
return new DateObject(this.clone().plus(Duration.fromObject({months: count})));
|
|
94
|
+
}
|
|
95
|
+
addYear(count: number): DateObject {
|
|
96
|
+
return new DateObject(this.clone().plus(Duration.fromObject({years: count})));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
resetYear(): DateObject {
|
|
100
|
+
return new DateObject(this.clone().set({ year: 2000 }));
|
|
101
|
+
}
|
|
102
|
+
resetDay(): DateObject {
|
|
103
|
+
return new DateObject(this.resetYear().val.set({ month: 1, day: 1 }));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
removeMinutes(): DateObject {
|
|
107
|
+
return new DateObject(this.clone().set({ minute: 0, second: 0, millisecond: 0 }));
|
|
108
|
+
}
|
|
109
|
+
removeHours(): DateObject {
|
|
110
|
+
return new DateObject(this.removeMinutes().val.set({ hour: 0 }));
|
|
111
|
+
}
|
|
112
|
+
removeDays(): DateObject {
|
|
113
|
+
return new DateObject(this.removeHours().val.set({ day: 1 }));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
equals(dateToCompare: DateObject): boolean {
|
|
117
|
+
return +this.val === +dateToCompare.val;
|
|
118
|
+
}
|
|
119
|
+
sameDay(dateToCompare: DateObject): boolean {
|
|
120
|
+
return this.val.hasSame(dateToCompare.val, 'day') &&
|
|
121
|
+
this.val.hasSame(dateToCompare.val, 'month') &&
|
|
122
|
+
this.val.hasSame(dateToCompare.val, 'year');
|
|
123
|
+
}
|
|
124
|
+
isDateBetween(startDate: DateObject, endDate: DateObject): boolean {
|
|
125
|
+
return this.val >= startDate.val && this.val <= endDate.val;
|
|
126
|
+
}
|
|
127
|
+
isGreater(dateToCompare: DateObject): boolean {
|
|
128
|
+
return this.val > dateToCompare.val;
|
|
129
|
+
}
|
|
130
|
+
isGreaterOrEqual(dateToCompare: DateObject): boolean {
|
|
131
|
+
return this.val >= dateToCompare.val;
|
|
132
|
+
}
|
|
133
|
+
isSmaller(dateToCompare: DateObject): boolean {
|
|
134
|
+
return this.val < dateToCompare.val;
|
|
135
|
+
}
|
|
136
|
+
isSmallerOrEqual(dateToCompare: DateObject): boolean {
|
|
137
|
+
return this.val <= dateToCompare.val;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
compare(dateToCompare: DateObject): number {
|
|
141
|
+
if (this.equals(dateToCompare)) {
|
|
142
|
+
return 0;
|
|
143
|
+
} else if (this.isGreater(dateToCompare)) {
|
|
144
|
+
return 1;
|
|
145
|
+
} else {
|
|
146
|
+
return -1;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
isWeekday(): boolean {
|
|
151
|
+
return this.weekday < 6;
|
|
152
|
+
}
|
|
153
|
+
isWeekend(): boolean {
|
|
154
|
+
return this.weekday > 5;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
isHoliday(): boolean {
|
|
158
|
+
return holidayService.isHoliday(this);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
valueOf(): number {
|
|
162
|
+
return this.val.valueOf();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
getDaysDifference(dateToCompare: DateObject): number {
|
|
166
|
+
return Interval.fromDateTimes(this.val, dateToCompare.val).count('day');
|
|
167
|
+
}
|
|
168
|
+
getHoursDifference(dateToCompare: DateObject): number {
|
|
169
|
+
return Interval.fromDateTimes(this.val, dateToCompare.val).toDuration(['hours', 'minutes']).hours;
|
|
170
|
+
}
|
|
171
|
+
}
|
package/src/holiday.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { dateService } from './service';
|
|
2
|
+
import { DateObject } from './class';
|
|
3
|
+
|
|
4
|
+
export class Holiday {
|
|
5
|
+
name: string;
|
|
6
|
+
month: number;
|
|
7
|
+
day: number | { weekday: number, count: number};
|
|
8
|
+
|
|
9
|
+
constructor(name: string, month: number, day: number | { weekday: number, count: number}) {
|
|
10
|
+
this.name = name;
|
|
11
|
+
this.month = month;
|
|
12
|
+
this.day = day;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function getNthDay(year: number, month: number, weekday: number, count: number): DateObject {
|
|
17
|
+
const firstDayOfMonth = dateService.date(year, month, 1);
|
|
18
|
+
|
|
19
|
+
// is target day before or after first day
|
|
20
|
+
const offsetThreshold = firstDayOfMonth.weekday - weekday;
|
|
21
|
+
|
|
22
|
+
let offsetFromTargetDay: number;
|
|
23
|
+
if (offsetThreshold > 0) {
|
|
24
|
+
// get to target day if target is after first day
|
|
25
|
+
offsetFromTargetDay = 7 - offsetThreshold;
|
|
26
|
+
} else {
|
|
27
|
+
// reverse threshold to get to target from first day
|
|
28
|
+
offsetFromTargetDay = offsetThreshold * -1;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const firstOccurenceOfTargetDay = firstDayOfMonth.addDay(offsetFromTargetDay);
|
|
32
|
+
const nthDay = firstOccurenceOfTargetDay.addDay((count - 1) * 7);
|
|
33
|
+
|
|
34
|
+
return nthDay;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export namespace holidayService {
|
|
38
|
+
const _list: Holiday[] = [];
|
|
39
|
+
|
|
40
|
+
type KnownHolidays = {
|
|
41
|
+
ChristmasEve: Holiday;
|
|
42
|
+
ChristmasDay: Holiday;
|
|
43
|
+
Christmas2ndDay: Holiday;
|
|
44
|
+
NewYearDay: Holiday;
|
|
45
|
+
USIndependenceDay: Holiday;
|
|
46
|
+
USThanksgivingDay: Holiday;
|
|
47
|
+
USMemorialDay: Holiday;
|
|
48
|
+
USLaborDay: Holiday;
|
|
49
|
+
PLIndependenceDay: Holiday;
|
|
50
|
+
PLLaborDay: Holiday;
|
|
51
|
+
PLConstitutionDay: Holiday;
|
|
52
|
+
PLAllSaintsDay: Holiday;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const known: KnownHolidays = {
|
|
56
|
+
ChristmasEve: new Holiday('Christmas Day', 12, 24),
|
|
57
|
+
ChristmasDay: new Holiday('Christmas Day', 12, 25),
|
|
58
|
+
Christmas2ndDay: new Holiday('Christmas Day', 12, 26),
|
|
59
|
+
NewYearDay: new Holiday('New Year\'s Day', 1, 2),
|
|
60
|
+
USIndependenceDay: new Holiday('Independence Day', 7, 4),
|
|
61
|
+
USThanksgivingDay: new Holiday('Thanksgiving Day', 11, { weekday: 4, count: 4 }),
|
|
62
|
+
USMemorialDay: new Holiday('Memorial Day', 5, { weekday: 1, count: -1 }),
|
|
63
|
+
USLaborDay: new Holiday('Labor Day', 9, { weekday: 1, count: 1 }),
|
|
64
|
+
PLIndependenceDay: new Holiday('Independence Day', 11, 11),
|
|
65
|
+
PLConstitutionDay: new Holiday('Constitution Day', 5, 3),
|
|
66
|
+
PLLaborDay: new Holiday('Labor Day', 5, 1),
|
|
67
|
+
PLAllSaintsDay: new Holiday('All Saints Day', 11, 1),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export function registerHoliday(holiday: Holiday){
|
|
71
|
+
_list.push(holiday);
|
|
72
|
+
}
|
|
73
|
+
export function isHoliday(date: DateObject): boolean {
|
|
74
|
+
return _list.some((holiday) => {
|
|
75
|
+
return checkHoliday(date, holiday);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
export function checkHoliday(date: DateObject, holiday: Holiday): boolean{
|
|
79
|
+
const justDate = date.removeHours();
|
|
80
|
+
let holy: DateObject;
|
|
81
|
+
|
|
82
|
+
if (typeof holiday.day === 'number') {
|
|
83
|
+
//fixed holiday
|
|
84
|
+
holy = dateService.date(justDate.year, holiday.month, holiday.day);
|
|
85
|
+
} else {
|
|
86
|
+
//moveable holiday
|
|
87
|
+
if (holiday.day.count > 1) {
|
|
88
|
+
//we're counting from beginning
|
|
89
|
+
//let's get nth weekday in specific month
|
|
90
|
+
holy = getNthDay(justDate.year, holiday.month, holiday.day.weekday, holiday.day.count);
|
|
91
|
+
} else {
|
|
92
|
+
//we're counting from the end
|
|
93
|
+
//let's get started from the first weekday in next month
|
|
94
|
+
holy = getNthDay(
|
|
95
|
+
holiday.month === 12 ? justDate.year + 1 : justDate.year,
|
|
96
|
+
holiday.month === 12 ? 1 : holiday.month + 1,
|
|
97
|
+
holiday.day.weekday, 1).addDay(7 * holiday.day.count);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return justDate.sameDay(holy);
|
|
101
|
+
}
|
|
102
|
+
}
|
package/src/index.ts
ADDED
package/src/service.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { DateTime, Settings } from 'luxon';
|
|
2
|
+
import { DateObject } from './class';
|
|
3
|
+
|
|
4
|
+
Settings.defaultZone = 'utc';
|
|
5
|
+
|
|
6
|
+
export namespace dateService {
|
|
7
|
+
|
|
8
|
+
export function initialize(defaultZoneName: string, defaultLocaleName: string) {
|
|
9
|
+
Settings.defaultZone = defaultZoneName;
|
|
10
|
+
Settings.defaultLocale = defaultLocaleName;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function parseUtcDate(datetime: string) : DateObject | undefined {
|
|
14
|
+
if (datetime) {
|
|
15
|
+
const temp = DateTime.fromISO(datetime);
|
|
16
|
+
if (temp) {
|
|
17
|
+
if (temp.isValid) {
|
|
18
|
+
return DateObject.set(temp.setZone(Settings.defaultZone).valueOf());
|
|
19
|
+
} else {
|
|
20
|
+
console.error(`Invalid date parsing value: '${datetime}'`);
|
|
21
|
+
console.error(temp.invalidReason);
|
|
22
|
+
console.error(temp.invalidExplanation);
|
|
23
|
+
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
} else {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function date(year: number, month: number, day: number) : DateObject {
|
|
35
|
+
return DateObject.set(DateTime.local(year, month, day).valueOf());
|
|
36
|
+
}
|
|
37
|
+
export function datetime(year: number, month: number, day: number, hour: number, minute: number): DateObject {
|
|
38
|
+
return DateObject.set(DateTime.local(year, month, day, hour, minute).valueOf());
|
|
39
|
+
}
|
|
40
|
+
export function daytime(day: DateObject, hour: number = 0, minute: number = 0): DateObject {
|
|
41
|
+
return datetime(day.year, day.month, day.day, hour, minute);
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { dateService } from './service';
|
|
2
|
+
import { DateObject } from './class';
|
|
3
|
+
|
|
4
|
+
export namespace dateUtils {
|
|
5
|
+
|
|
6
|
+
export function checkPeriod(value: DateObject, start: DateObject, stop?: DateObject): boolean {
|
|
7
|
+
if (!stop) {
|
|
8
|
+
stop = dateService.date(3000, 12, 31);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return checkDatePeriod(value, start, stop);
|
|
12
|
+
}
|
|
13
|
+
export function checkDatePeriod(value: DateObject, start: DateObject, stop: DateObject): boolean {
|
|
14
|
+
return value >= start && value <= stop;
|
|
15
|
+
}
|
|
16
|
+
export function checkSameYearPeriod(value: DateObject, start: DateObject, stop: DateObject): boolean {
|
|
17
|
+
return checkDatePeriod(value, start, stop);
|
|
18
|
+
}
|
|
19
|
+
export function checkSameDayPeriod(value: DateObject, start: DateObject, stop: DateObject): boolean {
|
|
20
|
+
const val = value.resetDay();
|
|
21
|
+
const str = start.resetDay();
|
|
22
|
+
const stp = stop.resetDay();
|
|
23
|
+
|
|
24
|
+
return checkDatePeriod(val, str, stp);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["ES2020"],
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"rootDir": "./src",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"moduleResolution": "node",
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"declaration": true,
|
|
15
|
+
},
|
|
16
|
+
"include": ["src/**/*"],
|
|
17
|
+
"exclude": ["node_modules", "dist"]
|
|
18
|
+
}
|