101 lines
2.6 KiB
TypeScript
101 lines
2.6 KiB
TypeScript
import * as plugins from './smarttime.plugins.js';
|
|
|
|
/**
|
|
* TimeStamp
|
|
* smart timestamp
|
|
*/
|
|
export class TimeStamp {
|
|
/**
|
|
* returns new TimeStamp from milliseconds
|
|
*/
|
|
public static fromMilliSeconds(milliSecondsArg: number) {
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* The standard JavaScript Date
|
|
*/
|
|
public date: Date;
|
|
|
|
/**
|
|
* The time as linux time (milliseconds, not seconds though)
|
|
* good for comparison
|
|
*/
|
|
public milliSeconds: number;
|
|
|
|
/**
|
|
* The standard epoch time in seconds
|
|
*/
|
|
public epochtime: number;
|
|
|
|
/**
|
|
* if derived from another TimeStamp points out the change in milliseconds
|
|
*/
|
|
public change: number = null;
|
|
|
|
constructor(creatorArg?: number) {
|
|
if (!creatorArg) {
|
|
this.date = new Date();
|
|
} else if (typeof creatorArg === 'number') {
|
|
this.date = new Date(creatorArg);
|
|
}
|
|
this.milliSeconds = this.date.getTime();
|
|
this.epochtime = Math.floor(this.milliSeconds / 1000);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Is the current instance older than the argument
|
|
* @param TimeStampArg
|
|
*/
|
|
public isOlderThan(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
|
if (this.milliSeconds + tresholdTimeArg < TimeStampArg.milliSeconds) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* returns a boolean for wether the timestamp is younger than another timestamp
|
|
* @param TimeStampArg
|
|
* @param tresholdTimeArg
|
|
*/
|
|
public isYoungerThanOtherTimeStamp(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
|
if (this.milliSeconds > TimeStampArg.milliSeconds + tresholdTimeArg) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public isYoungerThanMilliSeconds(millisecondArg: number) {
|
|
const nowTimeStamp = new TimeStamp();
|
|
const compareEpochTime = nowTimeStamp.epochtime - millisecondArg;
|
|
const compareTimeStamp = new TimeStamp(compareEpochTime);
|
|
return this.isYoungerThanOtherTimeStamp(compareTimeStamp);
|
|
}
|
|
}
|