smarttime/ts/smarttime.classes.timestamp.ts

76 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-08-16 12:29:12 +00:00
import * as plugins from './smarttime.plugins'
/**
* TimeStamp
* smart timestamp
*/
export class TimeStamp {
/**
* The standard JavaScript Date
*/
date: Date
/**
* The time as linux time (milliseconds, not seconds though)
2017-08-16 12:29:12 +00:00
* good for comparison
*/
milliSeconds: number
/**
* The standard epoch time in seconds
*/
epochtime: number
/**
* if derived from another TimeStamp points out the change in milliseconds
*/
change: number = null
constructor (creatorArg?: number) {
2017-08-16 12:29:12 +00:00
if (!creatorArg) {
this.date = new Date()
} else if (typeof creatorArg === 'number') {
this.date = new Date(creatorArg)
2017-08-16 12:29:12 +00:00
}
this.milliSeconds = this.date.getTime()
this.epochtime = Math.floor(this.milliSeconds / 1000)
}
/**
* returns new TimeStamp from milliseconds
*/
static fromMilliSeconds (milliSecondsArg) {
return new TimeStamp(milliSecondsArg)
}
/**
* returns new TimeStamp for now with change set
* @param timeStampArg
*/
static fromTimeStamp (timeStampArg: TimeStamp) {
let localTimeStamp = new TimeStamp()
localTimeStamp.change = localTimeStamp.milliSeconds - timeStampArg.milliSeconds
return localTimeStamp
2017-08-16 12:29:12 +00:00
}
/**
* Is the current instance older than the argument
* @param TimeStampArg
*/
2017-08-19 15:59:54 +00:00
isOlderThan (TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
if ((this.milliSeconds + tresholdTimeArg) < TimeStampArg.milliSeconds) {
2017-08-16 12:29:12 +00:00
return true
} else {
return false
}
}
2017-08-19 15:59:54 +00:00
isYoungerThan (TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
if (this.milliSeconds > (TimeStampArg.milliSeconds + tresholdTimeArg)) {
2017-08-16 12:29:12 +00:00
return true
} else {
return false
}
}
}