@push.rocks/smarttime 4.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/dist_bundle/bundle.js +15434 -0
- package/dist_bundle/bundle.js.map +7 -0
- package/dist_ts/00_commitinfo_data.d.ts +8 -0
- package/dist_ts/00_commitinfo_data.js +9 -0
- package/dist_ts/index.d.ts +8 -0
- package/dist_ts/index.js +9 -0
- package/dist_ts/smarttime.classes.cronjob.d.ts +22 -0
- package/dist_ts/smarttime.classes.cronjob.js +44 -0
- package/dist_ts/smarttime.classes.cronmanager.d.ts +18 -0
- package/dist_ts/smarttime.classes.cronmanager.js +71 -0
- package/dist_ts/smarttime.classes.cronparser.d.ts +7 -0
- package/dist_ts/smarttime.classes.cronparser.js +74 -0
- package/dist_ts/smarttime.classes.extendeddate.d.ts +42 -0
- package/dist_ts/smarttime.classes.extendeddate.js +114 -0
- package/dist_ts/smarttime.classes.hrtmeasurement.d.ts +22 -0
- package/dist_ts/smarttime.classes.hrtmeasurement.js +43 -0
- package/dist_ts/smarttime.classes.interval.d.ts +12 -0
- package/dist_ts/smarttime.classes.interval.js +34 -0
- package/dist_ts/smarttime.classes.timer.d.ts +38 -0
- package/dist_ts/smarttime.classes.timer.js +54 -0
- package/dist_ts/smarttime.classes.timestamp.d.ts +51 -0
- package/dist_ts/smarttime.classes.timestamp.js +81 -0
- package/dist_ts/smarttime.plugins.d.ts +8 -0
- package/dist_ts/smarttime.plugins.js +13 -0
- package/dist_ts/smarttime.units.d.ts +18 -0
- package/dist_ts/smarttime.units.js +50 -0
- package/license +21 -0
- package/npmextra.json +17 -0
- package/package.json +48 -0
- package/readme.md +63 -0
- package/ts/00_commitinfo_data.ts +8 -0
- package/ts/index.ts +8 -0
- package/ts/smarttime.classes.cronjob.ts +58 -0
- package/ts/smarttime.classes.cronmanager.ts +85 -0
- package/ts/smarttime.classes.cronparser.ts +88 -0
- package/ts/smarttime.classes.extendeddate.ts +151 -0
- package/ts/smarttime.classes.hrtmeasurement.ts +43 -0
- package/ts/smarttime.classes.interval.ts +43 -0
- package/ts/smarttime.classes.timer.ts +86 -0
- package/ts/smarttime.classes.timestamp.ts +100 -0
- package/ts/smarttime.plugins.ts +16 -0
- package/ts/smarttime.units.ts +62 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import * as plugins from './smarttime.plugins.js';
|
|
2
|
+
|
|
3
|
+
export class CronParser {
|
|
4
|
+
public cronExpression: string;
|
|
5
|
+
public get cronArray() {
|
|
6
|
+
return this.cronExpression.split(' ');
|
|
7
|
+
}
|
|
8
|
+
constructor(cronExpressionArg: string) {
|
|
9
|
+
this.cronExpression = cronExpressionArg;
|
|
10
|
+
if (this.cronArray.length < 6) {
|
|
11
|
+
throw new Error('CronParser needs second level accuracy');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
private getNextPartMatch(cronPart: string, startValue: number, moduloArg: number) {
|
|
16
|
+
if (cronPart === '*') {
|
|
17
|
+
return startValue;
|
|
18
|
+
}
|
|
19
|
+
if (cronPart.includes('/')) {
|
|
20
|
+
const every = parseInt(cronPart.split('/')[1], 10);
|
|
21
|
+
const findEvenMatch = (recursionStartArg: number): number => {
|
|
22
|
+
if (recursionStartArg % every === 0) {
|
|
23
|
+
return recursionStartArg;
|
|
24
|
+
} else {
|
|
25
|
+
return findEvenMatch(recursionStartArg + 1);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
return findEvenMatch(startValue);
|
|
29
|
+
}
|
|
30
|
+
if (parseInt(cronPart, 10) || cronPart === '0') {
|
|
31
|
+
const match = parseInt(cronPart, 10);
|
|
32
|
+
return match;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public msToNext() {
|
|
37
|
+
const cronArray = this.cronArray;
|
|
38
|
+
const secondExpression = cronArray[0];
|
|
39
|
+
const minuteExpression = cronArray[1];
|
|
40
|
+
const hourExpression = cronArray[2];
|
|
41
|
+
const dayExpression = cronArray[3];
|
|
42
|
+
const monthExpression = cronArray[4];
|
|
43
|
+
const yearExpression = cronArray[5];
|
|
44
|
+
|
|
45
|
+
let currentDate = new Date();
|
|
46
|
+
let currentSecond = currentDate.getSeconds() + 1;
|
|
47
|
+
let currentMinute = currentDate.getMinutes();
|
|
48
|
+
let currentHour = currentDate.getHours();
|
|
49
|
+
let currentDay = currentDate.getDate();
|
|
50
|
+
let currentMonth = currentDate.getMonth();
|
|
51
|
+
let currentYear = currentDate.getFullYear();
|
|
52
|
+
|
|
53
|
+
const targetSecond = this.getNextPartMatch(secondExpression, currentSecond, 59);
|
|
54
|
+
if (targetSecond < currentSecond) {
|
|
55
|
+
currentMinute = (currentMinute + 1) % 59;
|
|
56
|
+
}
|
|
57
|
+
const targetMinute = this.getNextPartMatch(minuteExpression, currentMinute, 59);
|
|
58
|
+
if (targetMinute < currentMinute) {
|
|
59
|
+
currentHour = (currentHour + 1) % 23;
|
|
60
|
+
}
|
|
61
|
+
const targetHour = this.getNextPartMatch(hourExpression, currentHour, 23);
|
|
62
|
+
if (targetHour < currentHour) {
|
|
63
|
+
currentDay = (currentDay + 1) % 30;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const targetDay = currentDay;
|
|
67
|
+
if (targetDay < currentDay) {
|
|
68
|
+
currentMonth = (currentMonth + 1) % 11;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const targetMonth = currentMonth;
|
|
72
|
+
if (targetMonth < currentMonth) {
|
|
73
|
+
currentYear = currentYear + 1;
|
|
74
|
+
}
|
|
75
|
+
const targetYear = currentYear;
|
|
76
|
+
|
|
77
|
+
const targetDate = new Date(
|
|
78
|
+
targetYear,
|
|
79
|
+
targetMonth,
|
|
80
|
+
targetDay,
|
|
81
|
+
targetHour,
|
|
82
|
+
targetMinute,
|
|
83
|
+
targetSecond
|
|
84
|
+
);
|
|
85
|
+
const targetTime = targetDate.getTime();
|
|
86
|
+
return targetTime - Date.now();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import * as plugins from './smarttime.plugins.js';
|
|
2
|
+
import * as units from './smarttime.units.js';
|
|
3
|
+
|
|
4
|
+
export type TAvailableZone = 'Europe/Berlin';
|
|
5
|
+
|
|
6
|
+
export interface IDateUnits {
|
|
7
|
+
year: number;
|
|
8
|
+
yearString: string;
|
|
9
|
+
month: number;
|
|
10
|
+
monthString: string;
|
|
11
|
+
monthName: string;
|
|
12
|
+
day: number;
|
|
13
|
+
dayString: string;
|
|
14
|
+
dayOfTheWeek: number;
|
|
15
|
+
dayOfTheWeekName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class ExtendedDate extends Date {
|
|
19
|
+
// STATIC factories
|
|
20
|
+
public static fromMillis(milliSeconds: number) {
|
|
21
|
+
return new ExtendedDate(milliSeconds);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public static fromDate(dateArg: Date) {
|
|
25
|
+
return new ExtendedDate(dateArg.getTime());
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public static fromEuropeanDate(europeanDate: string) {
|
|
29
|
+
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate);
|
|
30
|
+
const date = new Date(
|
|
31
|
+
parseFloat(dateArray[3]), // year
|
|
32
|
+
parseFloat(dateArray[2]) - 1, // month
|
|
33
|
+
parseFloat(dateArray[1]) // day
|
|
34
|
+
);
|
|
35
|
+
const unixMilli = date.getTime();
|
|
36
|
+
return new ExtendedDate(unixMilli);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* creates an Extended date from a hypedDate like "2018-03-28"
|
|
41
|
+
* @param dateString
|
|
42
|
+
*/
|
|
43
|
+
public static fromHyphedDate(dateString: string) {
|
|
44
|
+
// guards
|
|
45
|
+
// implementation
|
|
46
|
+
const dateMillis = new Date(dateString).getTime();
|
|
47
|
+
return new ExtendedDate(dateMillis);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Same as .fromEuropeanDate(), but accepts additional timeArg and zoneArg
|
|
52
|
+
*/
|
|
53
|
+
public static fromEuropeanDateAndTime(
|
|
54
|
+
europeanDateArg: string,
|
|
55
|
+
timeArg: string = '12:00:00',
|
|
56
|
+
zoneArg: TAvailableZone = 'Europe/Berlin'
|
|
57
|
+
) {
|
|
58
|
+
// guards
|
|
59
|
+
|
|
60
|
+
// implementation
|
|
61
|
+
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDateArg);
|
|
62
|
+
const sliceDate = (dateString: string) => {
|
|
63
|
+
return `0${dateString}`.slice(-2);
|
|
64
|
+
};
|
|
65
|
+
const dateTimeString = `${dateArray[3]}-${sliceDate(dateArray[2])}-${sliceDate(
|
|
66
|
+
dateArray[1]
|
|
67
|
+
)}T${timeArg}`;
|
|
68
|
+
const date = plugins.dayjs(dateTimeString);
|
|
69
|
+
const unixMilli = date.toDate().getTime();
|
|
70
|
+
return new ExtendedDate(unixMilli);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// INSTANCE
|
|
74
|
+
public timezone: TAvailableZone;
|
|
75
|
+
|
|
76
|
+
constructor(unixMilli: number = Date.now()) {
|
|
77
|
+
super(unixMilli);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
//
|
|
81
|
+
public exportToEuropeanDate() {
|
|
82
|
+
const units = this.exportToUnits();
|
|
83
|
+
return `${units.dayString}.${units.monthString}.${units.yearString}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public exportToHyphedSortableDate() {
|
|
87
|
+
const units = this.exportToUnits();
|
|
88
|
+
return `${units.yearString}-${units.monthString}-${units.dayString}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* exports units
|
|
93
|
+
*/
|
|
94
|
+
public exportToUnits(): IDateUnits {
|
|
95
|
+
const monthsArray = [
|
|
96
|
+
'January',
|
|
97
|
+
'February',
|
|
98
|
+
'March',
|
|
99
|
+
'April',
|
|
100
|
+
'May',
|
|
101
|
+
'June',
|
|
102
|
+
'July',
|
|
103
|
+
'August',
|
|
104
|
+
'September',
|
|
105
|
+
'October',
|
|
106
|
+
'November',
|
|
107
|
+
'December',
|
|
108
|
+
];
|
|
109
|
+
const daysArray = [
|
|
110
|
+
'Monday',
|
|
111
|
+
'Tuesday',
|
|
112
|
+
'Wednesday',
|
|
113
|
+
'Thursday',
|
|
114
|
+
'Friday',
|
|
115
|
+
'Saturday',
|
|
116
|
+
'Sunday',
|
|
117
|
+
];
|
|
118
|
+
return {
|
|
119
|
+
year: this.getFullYear(),
|
|
120
|
+
yearString: `${this.getFullYear()}`,
|
|
121
|
+
month: this.getMonth() + 1,
|
|
122
|
+
monthString: ('0' + (this.getMonth() + 1)).slice(-2),
|
|
123
|
+
monthName: monthsArray[this.getMonth()],
|
|
124
|
+
day: this.getDate(),
|
|
125
|
+
dayString: ('0' + this.getDate()).slice(-2),
|
|
126
|
+
dayOfTheWeek: this.getDay(),
|
|
127
|
+
dayOfTheWeekName: daysArray[this.getDay()],
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public format(formatArg: string) {
|
|
132
|
+
return plugins.dayjs(this.getTime()).format(formatArg);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* boolean checks
|
|
137
|
+
*/
|
|
138
|
+
public isToday() {
|
|
139
|
+
return plugins.dayjs(this.getTime()).isToday();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public lessTimePassedToNow(unitArgs: units.IUnitCombinationArg): boolean {
|
|
143
|
+
const maxPassedUnixTime = units.getMilliSecondsFromUnits(unitArgs);
|
|
144
|
+
const actualPassedUnixTime = Date.now() - this.getTime();
|
|
145
|
+
return actualPassedUnixTime < maxPassedUnixTime;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public moreTimePassedToNow(unitArgs: units.IUnitCombinationArg) {
|
|
149
|
+
return !this.lessTimePassedToNow(unitArgs);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* easy high resolution time measurement
|
|
3
|
+
*/
|
|
4
|
+
export class HrtMeasurement {
|
|
5
|
+
public nanoSeconds: number = null;
|
|
6
|
+
public milliSeconds: number = null;
|
|
7
|
+
private _milliStart: number = null;
|
|
8
|
+
private _milliDiff: number = null;
|
|
9
|
+
private _started: boolean = false;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* start the measurement
|
|
13
|
+
*/
|
|
14
|
+
public start() {
|
|
15
|
+
this._started = true;
|
|
16
|
+
this._milliStart = Date.now();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* stop the measurement
|
|
21
|
+
*/
|
|
22
|
+
public stop() {
|
|
23
|
+
if (this._started === false) {
|
|
24
|
+
console.log("Hasn't started yet");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
this._milliDiff = Date.now() - this._milliStart;
|
|
28
|
+
this.nanoSeconds = this._milliDiff * 1000;
|
|
29
|
+
this.milliSeconds = this._milliDiff;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* reset the measurement
|
|
35
|
+
*/
|
|
36
|
+
public reset() {
|
|
37
|
+
this.nanoSeconds = null;
|
|
38
|
+
this.milliSeconds = null;
|
|
39
|
+
this._milliStart = null;
|
|
40
|
+
this._milliDiff = null;
|
|
41
|
+
this._started = false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as plugins from './smarttime.plugins.js';
|
|
2
|
+
|
|
3
|
+
export class Interval {
|
|
4
|
+
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
|
5
|
+
private statusAuthorization: any = null;
|
|
6
|
+
|
|
7
|
+
// timings
|
|
8
|
+
public intervalMilliseconds: number;
|
|
9
|
+
public nextIntervalMillisenconds: number;
|
|
10
|
+
|
|
11
|
+
public intervalJobs: Array<() => any> = [];
|
|
12
|
+
constructor(intervalMillisencondsArg: number) {
|
|
13
|
+
this.intervalMilliseconds = intervalMillisencondsArg;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public start() {
|
|
17
|
+
this.status = 'started';
|
|
18
|
+
const statusAuth = new Date();
|
|
19
|
+
this.statusAuthorization = statusAuth;
|
|
20
|
+
const runInterval = async () => {
|
|
21
|
+
while (this.status === 'started' && this.statusAuthorization === statusAuth) {
|
|
22
|
+
await plugins.smartdelay.delayFor(this.intervalMilliseconds);
|
|
23
|
+
this.executeIntervalJobs();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
runInterval();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public stop() {
|
|
30
|
+
this.status = 'stopped';
|
|
31
|
+
this.statusAuthorization = null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public addIntervalJob(funcArg: () => any) {
|
|
35
|
+
this.intervalJobs.push(funcArg);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private executeIntervalJobs() {
|
|
39
|
+
for (const funcArg of this.intervalJobs) {
|
|
40
|
+
funcArg();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import * as plugins from './smarttime.plugins.js';
|
|
2
|
+
|
|
3
|
+
import { TimeStamp } from './smarttime.classes.timestamp.js';
|
|
4
|
+
|
|
5
|
+
export type TimerState = 'initiated' | 'started' | 'paused' | 'completed';
|
|
6
|
+
|
|
7
|
+
export class Timer {
|
|
8
|
+
/**
|
|
9
|
+
* the original amount of milliseconds for this Timer
|
|
10
|
+
*/
|
|
11
|
+
public timeInMilliseconds: number;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* the state of the timer
|
|
15
|
+
*/
|
|
16
|
+
public state: TimerState = 'initiated';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* completed Promise
|
|
20
|
+
*/
|
|
21
|
+
public completed: Promise<void>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* a reference to when the Timeout started
|
|
25
|
+
*/
|
|
26
|
+
public startedAt: TimeStamp;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* a reference to when a Timer has been potentially paused
|
|
30
|
+
*/
|
|
31
|
+
public pausedAt: TimeStamp;
|
|
32
|
+
|
|
33
|
+
get timeLeft(): number {
|
|
34
|
+
return this.timeInMilliseconds - this.pausedAt.change;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* the current timeout the needs to be canceled when this Timer is stopped
|
|
38
|
+
*/
|
|
39
|
+
private currentTimeout: NodeJS.Timer;
|
|
40
|
+
|
|
41
|
+
// a deferred triggeted when Timer has completed
|
|
42
|
+
private completedDeferred = plugins.smartpromise.defer<void>();
|
|
43
|
+
|
|
44
|
+
constructor(timeInMillisecondsArg: number) {
|
|
45
|
+
this.timeInMilliseconds = timeInMillisecondsArg;
|
|
46
|
+
this.completed = this.completedDeferred.promise;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* starts the timer
|
|
51
|
+
*/
|
|
52
|
+
public start() {
|
|
53
|
+
if (!this.startedAt) {
|
|
54
|
+
this.currentTimeout = setTimeout(() => {
|
|
55
|
+
this.completedDeferred.resolve();
|
|
56
|
+
}, this.timeInMilliseconds);
|
|
57
|
+
this.startedAt = new TimeStamp();
|
|
58
|
+
} else {
|
|
59
|
+
throw new Error('timer has been started before. Please use resume instead');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public pause() {
|
|
64
|
+
if (this.startedAt) {
|
|
65
|
+
clearTimeout(this.currentTimeout);
|
|
66
|
+
this.currentTimeout = null;
|
|
67
|
+
this.pausedAt = TimeStamp.fromTimeStamp(this.startedAt);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public resume() {
|
|
72
|
+
if (this.startedAt) {
|
|
73
|
+
this.currentTimeout = setTimeout(() => {
|
|
74
|
+
this.completedDeferred.resolve();
|
|
75
|
+
}, this.timeLeft);
|
|
76
|
+
} else {
|
|
77
|
+
throw new Error('timer has NOT been started before. Please use .start() instead');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public reset() {
|
|
82
|
+
this.pause();
|
|
83
|
+
this.startedAt = null;
|
|
84
|
+
this.pausedAt = null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import * as plugins from './smarttime.plugins.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TimeStamp
|
|
5
|
+
* smart timestamp
|
|
6
|
+
*/
|
|
7
|
+
export class TimeStamp {
|
|
8
|
+
/**
|
|
9
|
+
* returns new TimeStamp from milliseconds
|
|
10
|
+
*/
|
|
11
|
+
public static fromMilliSeconds(milliSecondsArg: number) {
|
|
12
|
+
return new TimeStamp(milliSecondsArg);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* returns new TimeStamp for now with change set
|
|
17
|
+
* @param timeStampArg
|
|
18
|
+
*/
|
|
19
|
+
public static fromTimeStamp(timeStampArg: TimeStamp) {
|
|
20
|
+
const localTimeStamp = new TimeStamp();
|
|
21
|
+
localTimeStamp.change = localTimeStamp.milliSeconds - timeStampArg.milliSeconds;
|
|
22
|
+
return localTimeStamp;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The standard JavaScript Date
|
|
27
|
+
*/
|
|
28
|
+
public date: Date;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The time as linux time (milliseconds, not seconds though)
|
|
32
|
+
* good for comparison
|
|
33
|
+
*/
|
|
34
|
+
public milliSeconds: number;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The standard epoch time in seconds
|
|
38
|
+
*/
|
|
39
|
+
public epochtime: number;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* if derived from another TimeStamp points out the change in milliseconds
|
|
43
|
+
*/
|
|
44
|
+
public change: number = null;
|
|
45
|
+
|
|
46
|
+
constructor(creatorArg?: number) {
|
|
47
|
+
if (!creatorArg) {
|
|
48
|
+
this.date = new Date();
|
|
49
|
+
} else if (typeof creatorArg === 'number') {
|
|
50
|
+
this.date = new Date(creatorArg);
|
|
51
|
+
}
|
|
52
|
+
this.milliSeconds = this.date.getTime();
|
|
53
|
+
this.epochtime = Math.floor(this.milliSeconds / 1000);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* returns a boolean for wether the timestamp is older than another timestamp
|
|
58
|
+
* @param TimeStampArg
|
|
59
|
+
* @param tresholdTimeArg
|
|
60
|
+
*/
|
|
61
|
+
public isOlderThanOtherTimeStamp(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
|
62
|
+
if (this.milliSeconds < TimeStampArg.milliSeconds - tresholdTimeArg) {
|
|
63
|
+
return true;
|
|
64
|
+
} else {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Is the current instance older than the argument
|
|
71
|
+
* @param TimeStampArg
|
|
72
|
+
*/
|
|
73
|
+
public isOlderThan(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
|
74
|
+
if (this.milliSeconds + tresholdTimeArg < TimeStampArg.milliSeconds) {
|
|
75
|
+
return true;
|
|
76
|
+
} else {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* returns a boolean for wether the timestamp is younger than another timestamp
|
|
83
|
+
* @param TimeStampArg
|
|
84
|
+
* @param tresholdTimeArg
|
|
85
|
+
*/
|
|
86
|
+
public isYoungerThanOtherTimeStamp(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
|
87
|
+
if (this.milliSeconds > TimeStampArg.milliSeconds + tresholdTimeArg) {
|
|
88
|
+
return true;
|
|
89
|
+
} else {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public isYoungerThanMilliSeconds(millisecondArg: number) {
|
|
95
|
+
const nowTimeStamp = new TimeStamp();
|
|
96
|
+
const compareEpochTime = nowTimeStamp.epochtime - millisecondArg;
|
|
97
|
+
const compareTimeStamp = new TimeStamp(compareEpochTime);
|
|
98
|
+
return this.isYoungerThanOtherTimeStamp(compareTimeStamp);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// @pushrocks scope
|
|
2
|
+
import * as lik from '@pushrocks/lik';
|
|
3
|
+
import * as smartdelay from '@pushrocks/smartdelay';
|
|
4
|
+
import * as smartpromise from '@pushrocks/smartpromise';
|
|
5
|
+
|
|
6
|
+
export { lik, smartdelay, smartpromise };
|
|
7
|
+
|
|
8
|
+
// third parties;
|
|
9
|
+
import croner from 'croner';
|
|
10
|
+
import dayjs from 'dayjs';
|
|
11
|
+
import isToday from 'dayjs/plugin/isToday.js';
|
|
12
|
+
import prettyMs from 'pretty-ms';
|
|
13
|
+
|
|
14
|
+
dayjs.extend(isToday);
|
|
15
|
+
|
|
16
|
+
export { croner, dayjs, prettyMs };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import * as plugins from './smarttime.plugins.js';
|
|
2
|
+
|
|
3
|
+
export let units = {
|
|
4
|
+
years: (timesArg = 1): number => {
|
|
5
|
+
return timesArg * 3.154e10;
|
|
6
|
+
},
|
|
7
|
+
months: (timesArg = 1): number => {
|
|
8
|
+
return timesArg * 2.628e9;
|
|
9
|
+
},
|
|
10
|
+
weeks: (timesArg = 1) => {
|
|
11
|
+
return timesArg * 6.048e8;
|
|
12
|
+
},
|
|
13
|
+
days: (timesArg = 1) => {
|
|
14
|
+
return timesArg * 8.64e7;
|
|
15
|
+
},
|
|
16
|
+
hours: (timesArg = 1) => {
|
|
17
|
+
return timesArg * 3.6e6;
|
|
18
|
+
},
|
|
19
|
+
minutes: (timesArg = 1) => {
|
|
20
|
+
return timesArg * 60000;
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export interface IUnitCombinationArg {
|
|
25
|
+
years?: number;
|
|
26
|
+
months?: number;
|
|
27
|
+
weeks?: number;
|
|
28
|
+
days?: number;
|
|
29
|
+
hours?: number;
|
|
30
|
+
minutes?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export let getMilliSecondsFromUnits = (combinationArg: IUnitCombinationArg) => {
|
|
34
|
+
let timeInMilliseconds = 0;
|
|
35
|
+
let addMilliSeconds = (milliSecondsArg: number) => {
|
|
36
|
+
timeInMilliseconds = timeInMilliseconds + milliSecondsArg;
|
|
37
|
+
};
|
|
38
|
+
if (combinationArg.years) {
|
|
39
|
+
addMilliSeconds(units.years(combinationArg.years));
|
|
40
|
+
}
|
|
41
|
+
if (combinationArg.months) {
|
|
42
|
+
addMilliSeconds(units.months(combinationArg.months));
|
|
43
|
+
}
|
|
44
|
+
if (combinationArg.weeks) {
|
|
45
|
+
addMilliSeconds(units.weeks(combinationArg.weeks));
|
|
46
|
+
}
|
|
47
|
+
if (combinationArg.days) {
|
|
48
|
+
addMilliSeconds(units.days(combinationArg.days));
|
|
49
|
+
}
|
|
50
|
+
if (combinationArg.hours) {
|
|
51
|
+
addMilliSeconds(units.hours(combinationArg.hours));
|
|
52
|
+
}
|
|
53
|
+
if (combinationArg.minutes) {
|
|
54
|
+
addMilliSeconds(units.minutes(combinationArg.minutes));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return timeInMilliseconds;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const getMilliSecondsAsHumanReadableString = (milliSecondsArg: number): string => {
|
|
61
|
+
return plugins.prettyMs(milliSecondsArg);
|
|
62
|
+
};
|