update to latest standards
This commit is contained in:
11
ts/index.ts
11
ts/index.ts
@ -1,7 +1,8 @@
|
||||
import * as plugins from './smarttime.plugins'
|
||||
import * as plugins from './smarttime.plugins';
|
||||
|
||||
export * from './smarttime.classes.hrtmeasurement'
|
||||
export * from './smarttime.classes.timestamp'
|
||||
export * from './smarttime.units'
|
||||
export * from './smarttime.classes.hrtmeasurement';
|
||||
export * from './smarttime.classes.timer';
|
||||
export * from './smarttime.classes.timestamp';
|
||||
export * from './smarttime.units';
|
||||
|
||||
export { moment } from './smarttime.plugins'
|
||||
export { moment } from './smarttime.plugins';
|
||||
|
@ -1,45 +1,45 @@
|
||||
import * as process from 'process'
|
||||
import * as process from 'process';
|
||||
|
||||
/**
|
||||
* easy high resolution time measurement
|
||||
*/
|
||||
export class HrtMeasurement {
|
||||
nanoSeconds: number = null
|
||||
milliSeconds: number = null
|
||||
private _hrTimeStart = null
|
||||
private _hrTimeStopDiff = null
|
||||
private _started: boolean = false
|
||||
nanoSeconds: number = null;
|
||||
milliSeconds: number = null;
|
||||
private _hrTimeStart = null;
|
||||
private _hrTimeStopDiff = null;
|
||||
private _started: boolean = false;
|
||||
|
||||
/**
|
||||
* start the measurement
|
||||
*/
|
||||
start () {
|
||||
this._started = true
|
||||
this._hrTimeStart = process.hrtime()
|
||||
start() {
|
||||
this._started = true;
|
||||
this._hrTimeStart = process.hrtime();
|
||||
}
|
||||
|
||||
/**
|
||||
* stop the measurement
|
||||
*/
|
||||
stop () {
|
||||
stop() {
|
||||
if (this._started === false) {
|
||||
console.log('Hasn\'t started yet')
|
||||
return
|
||||
console.log("Hasn't started yet");
|
||||
return;
|
||||
}
|
||||
this._hrTimeStopDiff = process.hrtime(this._hrTimeStart)
|
||||
this.nanoSeconds = (this._hrTimeStopDiff[0] * 1e9) + this._hrTimeStopDiff[1]
|
||||
this.milliSeconds = this.nanoSeconds / 1000000
|
||||
return this
|
||||
this._hrTimeStopDiff = process.hrtime(this._hrTimeStart);
|
||||
this.nanoSeconds = this._hrTimeStopDiff[0] * 1e9 + this._hrTimeStopDiff[1];
|
||||
this.milliSeconds = this.nanoSeconds / 1000000;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* reset the measurement
|
||||
*/
|
||||
reset () {
|
||||
this.nanoSeconds = null
|
||||
this.milliSeconds = null
|
||||
this._hrTimeStart = null
|
||||
this._hrTimeStopDiff = null
|
||||
this._started = false
|
||||
reset() {
|
||||
this.nanoSeconds = null;
|
||||
this.milliSeconds = null;
|
||||
this._hrTimeStart = null;
|
||||
this._hrTimeStopDiff = null;
|
||||
this._started = false;
|
||||
}
|
||||
}
|
||||
|
75
ts/smarttime.classes.timer.ts
Normal file
75
ts/smarttime.classes.timer.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import * as plugins from './smarttime.plugins';
|
||||
|
||||
import { TimeStamp } from './smarttime.classes.timestamp';
|
||||
|
||||
export type TimerState = 'initiated' | 'started' | 'paused' | 'completed';
|
||||
|
||||
export class Timer {
|
||||
/**
|
||||
* the original amount of milliseconds for this Timer
|
||||
*/
|
||||
public timeInMilliseconds: number;
|
||||
|
||||
/**
|
||||
* the state of the timer
|
||||
*/
|
||||
public state: TimerState = 'initiated';
|
||||
|
||||
/**
|
||||
* completed Promise
|
||||
*/
|
||||
public completed: Promise<void>;
|
||||
|
||||
/**
|
||||
* a reference to when the Timeout started
|
||||
*/
|
||||
public startedAt: TimeStamp;
|
||||
|
||||
/**
|
||||
* a reference to when a Timer has been potentially paused
|
||||
*/
|
||||
public pausedAt: TimeStamp;
|
||||
|
||||
get timeLeft(): number {
|
||||
return this.timeInMilliseconds - this.pausedAt.change;
|
||||
}
|
||||
/**
|
||||
* the current timeout the needs to be canceled when this Timer is stopped
|
||||
*/
|
||||
private currentTimeout: NodeJS.Timer;
|
||||
|
||||
// a deferred triggeted when Timer has completed
|
||||
private completedDeferred = plugins.smartq.defer<void>();
|
||||
|
||||
constructor(timeInMillisecondsArg: number) {
|
||||
this.timeInMilliseconds = timeInMillisecondsArg;
|
||||
this.completed = this.completedDeferred.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* starts the timer
|
||||
*/
|
||||
public start() {
|
||||
if (!this.startedAt) {
|
||||
this.currentTimeout = setTimeout(() => {
|
||||
this.completedDeferred.resolve();
|
||||
}, this.timeInMilliseconds);
|
||||
this.startedAt = new TimeStamp();
|
||||
} else {
|
||||
throw new Error('timer has been started before. Please use resume instead');
|
||||
}
|
||||
}
|
||||
|
||||
public pause() {
|
||||
clearTimeout(this.currentTimeout);
|
||||
this.pausedAt = TimeStamp.fromTimeStamp(this.startedAt);
|
||||
}
|
||||
|
||||
public resume() {
|
||||
if (this.startedAt) {
|
||||
this.currentTimeout = setTimeout(() => {
|
||||
this.completedDeferred.resolve();
|
||||
}, this.timeLeft);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,75 +1,75 @@
|
||||
import * as plugins from './smarttime.plugins'
|
||||
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)
|
||||
* 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) {
|
||||
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 new TimeStamp from milliseconds
|
||||
*/
|
||||
static fromMilliSeconds (milliSecondsArg) {
|
||||
return new TimeStamp(milliSecondsArg)
|
||||
public 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
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the current instance older than the argument
|
||||
* @param TimeStampArg
|
||||
*/
|
||||
isOlderThan (TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
||||
if ((this.milliSeconds + tresholdTimeArg) < TimeStampArg.milliSeconds) {
|
||||
return true
|
||||
public isOlderThan(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
||||
if (this.milliSeconds + tresholdTimeArg < TimeStampArg.milliSeconds) {
|
||||
return true;
|
||||
} else {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
isYoungerThan (TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
||||
if (this.milliSeconds > (TimeStampArg.milliSeconds + tresholdTimeArg)) {
|
||||
return true
|
||||
public isYoungerThan(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
||||
if (this.milliSeconds > TimeStampArg.milliSeconds + tresholdTimeArg) {
|
||||
return true;
|
||||
} else {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,4 @@
|
||||
import 'typings-global'
|
||||
import * as moment from 'moment';
|
||||
import * as smartq from 'smartq';
|
||||
|
||||
import * as moment from 'moment'
|
||||
|
||||
export {
|
||||
moment
|
||||
}
|
||||
export { moment, smartq };
|
||||
|
@ -1,45 +1,56 @@
|
||||
export let units = {
|
||||
years: (timesArg = 1): number => {
|
||||
return timesArg * 3.154e+10
|
||||
return timesArg * 3.154e10;
|
||||
},
|
||||
months: (timesArg = 1): number => {
|
||||
return timesArg * 2.628e+9
|
||||
return timesArg * 2.628e9;
|
||||
},
|
||||
weeks: (timesArg = 1) => {
|
||||
return timesArg * 6.048e+8
|
||||
return timesArg * 6.048e8;
|
||||
},
|
||||
days: (timesArg = 1) => {
|
||||
return timesArg * 8.64e+7
|
||||
return timesArg * 8.64e7;
|
||||
},
|
||||
hours: (timesArg = 1) => {
|
||||
return timesArg * 3.6e+6
|
||||
return timesArg * 3.6e6;
|
||||
},
|
||||
minutes: (timesArg = 1) => {
|
||||
return timesArg * 60000
|
||||
return timesArg * 60000;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export interface IUnitCombinationArg {
|
||||
years?: number
|
||||
months?: number
|
||||
weeks?: number
|
||||
days?: number
|
||||
hours?: number
|
||||
minutes?: number
|
||||
years?: number;
|
||||
months?: number;
|
||||
weeks?: number;
|
||||
days?: number;
|
||||
hours?: number;
|
||||
minutes?: number;
|
||||
}
|
||||
|
||||
export let getMilliSecondsFromUnits = (combinationArg: IUnitCombinationArg) => {
|
||||
let timeInMilliseconds = 0
|
||||
let addMilliSeconds = (milliSecondsArg) => {
|
||||
timeInMilliseconds = timeInMilliseconds + milliSecondsArg
|
||||
let timeInMilliseconds = 0;
|
||||
let addMilliSeconds = milliSecondsArg => {
|
||||
timeInMilliseconds = timeInMilliseconds + milliSecondsArg;
|
||||
};
|
||||
if (combinationArg.years) {
|
||||
addMilliSeconds(units.years(combinationArg.years));
|
||||
}
|
||||
if (combinationArg.months) {
|
||||
addMilliSeconds(units.months(combinationArg.months));
|
||||
}
|
||||
if (combinationArg.weeks) {
|
||||
addMilliSeconds(units.weeks(combinationArg.weeks));
|
||||
}
|
||||
if (combinationArg.days) {
|
||||
addMilliSeconds(units.days(combinationArg.days));
|
||||
}
|
||||
if (combinationArg.hours) {
|
||||
addMilliSeconds(units.hours(combinationArg.hours));
|
||||
}
|
||||
if (combinationArg.minutes) {
|
||||
addMilliSeconds(units.minutes(combinationArg.minutes));
|
||||
}
|
||||
if (combinationArg.years) { addMilliSeconds(units.years(combinationArg.years)) }
|
||||
if (combinationArg.months) { addMilliSeconds(units.months(combinationArg.months)) }
|
||||
if (combinationArg.weeks) { addMilliSeconds(units.weeks(combinationArg.weeks)) }
|
||||
if (combinationArg.days) { addMilliSeconds(units.days(combinationArg.days)) }
|
||||
if (combinationArg.hours) { addMilliSeconds(units.hours(combinationArg.hours)) }
|
||||
if (combinationArg.minutes) { addMilliSeconds(units.minutes(combinationArg.minutes)) }
|
||||
|
||||
return timeInMilliseconds
|
||||
}
|
||||
|
||||
return timeInMilliseconds;
|
||||
};
|
||||
|
Reference in New Issue
Block a user