smarttime/ts/smarttime.classes.timestamp.ts

101 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

2022-11-21 08:14:32 +00:00
import * as plugins from './smarttime.plugins.js';
2017-08-16 12:29:12 +00:00
/**
* TimeStamp
* smart timestamp
*/
export class TimeStamp {
2018-03-11 15:44:32 +00:00
/**
* returns new TimeStamp from milliseconds
*/
2021-10-06 09:58:36 +00:00
public static fromMilliSeconds(milliSecondsArg: number) {
2018-03-11 15:44:32 +00:00
return new TimeStamp(milliSecondsArg);
}
/**
* returns new TimeStamp for now with change set
* @param timeStampArg
*/
public static fromTimeStamp(timeStampArg: TimeStamp) {
const localTimeStamp = new TimeStamp();
localTimeStamp.change = localTimeStamp.milliSeconds - timeStampArg.milliSeconds;
return localTimeStamp;
}
2017-08-16 12:29:12 +00:00
/**
* The standard JavaScript Date
*/
2018-03-11 15:44:32 +00:00
public date: Date;
2017-08-16 12:29:12 +00:00
/**
* The time as linux time (milliseconds, not seconds though)
2017-08-16 12:29:12 +00:00
* good for comparison
*/
2018-03-11 15:44:32 +00:00
public milliSeconds: number;
/**
* The standard epoch time in seconds
*/
2018-03-11 15:44:32 +00:00
public epochtime: number;
/**
* if derived from another TimeStamp points out the change in milliseconds
*/
2018-03-11 15:44:32 +00:00
public change: number = null;
2018-03-11 15:44:32 +00:00
constructor(creatorArg?: number) {
2017-08-16 12:29:12 +00:00
if (!creatorArg) {
2018-03-11 15:44:32 +00:00
this.date = new Date();
} else if (typeof creatorArg === 'number') {
2018-03-11 15:44:32 +00:00
this.date = new Date(creatorArg);
2017-08-16 12:29:12 +00:00
}
2018-03-11 15:44:32 +00:00
this.milliSeconds = this.date.getTime();
this.epochtime = Math.floor(this.milliSeconds / 1000);
2017-08-16 12:29:12 +00:00
}
/**
* returns a boolean for wether the timestamp is older than another timestamp
* @param TimeStampArg
* @param tresholdTimeArg
*/
public isOlderThanOtherTimeStamp(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
if (this.milliSeconds < TimeStampArg.milliSeconds - tresholdTimeArg) {
return true;
} else {
return false;
}
}
2017-08-16 12:29:12 +00:00
/**
* Is the current instance older than the argument
* @param TimeStampArg
*/
2018-03-11 15:44:32 +00:00
public isOlderThan(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
if (this.milliSeconds + tresholdTimeArg < TimeStampArg.milliSeconds) {
return true;
2017-08-16 12:29:12 +00:00
} else {
2018-03-11 15:44:32 +00:00
return false;
2017-08-16 12:29:12 +00:00
}
}
2019-04-10 09:34:30 +00:00
/**
* returns a boolean for wether the timestamp is younger than another timestamp
2019-04-10 09:34:30 +00:00
* @param TimeStampArg
* @param tresholdTimeArg
*/
public isYoungerThanOtherTimeStamp(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
2018-03-11 15:44:32 +00:00
if (this.milliSeconds > TimeStampArg.milliSeconds + tresholdTimeArg) {
return true;
2017-08-16 12:29:12 +00:00
} else {
2018-03-11 15:44:32 +00:00
return false;
2017-08-16 12:29:12 +00:00
}
}
public isYoungerThanMilliSeconds(millisecondArg: number) {
2018-06-10 12:00:18 +00:00
const nowTimeStamp = new TimeStamp();
const compareEpochTime = nowTimeStamp.epochtime - millisecondArg;
const compareTimeStamp = new TimeStamp(compareEpochTime);
return this.isYoungerThanOtherTimeStamp(compareTimeStamp);
}
2017-08-16 12:29:12 +00:00
}