2018-03-11 15:44:32 +00:00
|
|
|
import * as plugins from './smarttime.plugins';
|
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
|
|
|
|
*/
|
|
|
|
public static fromMilliSeconds(milliSecondsArg) {
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2017-08-16 13:34:49 +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;
|
2017-08-16 13:34:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The standard epoch time in seconds
|
|
|
|
*/
|
2018-03-11 15:44:32 +00:00
|
|
|
public epochtime: number;
|
2017-08-16 13:34:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* if derived from another TimeStamp points out the change in milliseconds
|
|
|
|
*/
|
2018-03-11 15:44:32 +00:00
|
|
|
public change: number = null;
|
2017-08-16 13:34:49 +00:00
|
|
|
|
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();
|
2017-08-16 13:34:49 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-11 17:20:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-03-11 17:20:33 +00:00
|
|
|
|
|
|
|
public isYoungerThanMilliSeconds(millisecondArg: number) {
|
2018-06-10 12:00:18 +00:00
|
|
|
const nowTimeStamp = new TimeStamp();
|
2018-03-11 17:20:33 +00:00
|
|
|
const compareEpochTime = nowTimeStamp.epochtime - millisecondArg;
|
|
|
|
const compareTimeStamp = new TimeStamp(compareEpochTime);
|
|
|
|
return this.isYoungerThanOtherTimeStamp(compareTimeStamp);
|
|
|
|
}
|
2017-08-16 12:29:12 +00:00
|
|
|
}
|