libs/core/src/lib/time/time.service.ts
Methods |
|
constructor(localStorage: LocalStorage)
|
||||||
Defined in libs/core/src/lib/time/time.service.ts:9
|
||||||
Parameters :
|
Public centerTimespan |
centerTimespan(timespan: Timespan, date: Date)
|
Defined in libs/core/src/lib/time/time.service.ts:15
|
Returns :
Timespan
|
Public centerTimespanWithDuration | |||||||||
centerTimespanWithDuration(timespan: Timespan, d: moment.Duration)
|
|||||||||
Defined in libs/core/src/lib/time/time.service.ts:22
|
|||||||||
Parameters :
Returns :
Timespan
|
Public containsIn | |||||||||
containsIn(timeInterval: TimeInterval, timestamp: number)
|
|||||||||
Defined in libs/core/src/lib/time/time.service.ts:65
|
|||||||||
Parameters :
Returns :
boolean
|
Public createTimespanOfInterval | ||||||
createTimespanOfInterval(timeInterval: TimeInterval)
|
||||||
Defined in libs/core/src/lib/time/time.service.ts:70
|
||||||
Parameters :
Returns :
Timespan
|
Public generateTimespan | |||||||||
generateTimespan(defaultTimeseriesTimeduration: MomentInputObject, align: "start" | "center" | "end")
|
|||||||||
Defined in libs/core/src/lib/time/time.service.ts:113
|
|||||||||
Parameters :
Returns :
Timespan
|
Public getBufferedTimespan |
getBufferedTimespan(timespan: Timespan, factor: number, maxBufferInMs?: number)
|
Defined in libs/core/src/lib/time/time.service.ts:83
|
Returns :
Timespan
|
Public getCenterOfTimespan | ||||||
getCenterOfTimespan(timespan: Timespan)
|
||||||
Defined in libs/core/src/lib/time/time.service.ts:28
|
||||||
Parameters :
Returns :
number
|
Private getDuration | ||||||
getDuration(timespan: Timespan)
|
||||||
Defined in libs/core/src/lib/time/time.service.ts:128
|
||||||
Parameters :
Returns :
moment.Duration
|
Public initTimespan |
initTimespan()
|
Defined in libs/core/src/lib/time/time.service.ts:106
|
Returns :
Timespan
|
Public loadTimespan | ||||||
loadTimespan(param: string)
|
||||||
Defined in libs/core/src/lib/time/time.service.ts:98
|
||||||
Parameters :
Returns :
Timespan
|
Public overlaps | ||||||||||||
overlaps(timeInterval: TimeInterval, from: number, to: number)
|
||||||||||||
Defined in libs/core/src/lib/time/time.service.ts:57
|
||||||||||||
Parameters :
Returns :
boolean
|
Public saveTimespan |
saveTimespan(param: string, timespan: Timespan)
|
Defined in libs/core/src/lib/time/time.service.ts:94
|
Returns :
void
|
Public stepBack | ||||||
stepBack(timespan: Timespan)
|
||||||
Defined in libs/core/src/lib/time/time.service.ts:32
|
||||||
Parameters :
Returns :
Timespan
|
Public stepForward | ||||||
stepForward(timespan: Timespan)
|
||||||
Defined in libs/core/src/lib/time/time.service.ts:39
|
||||||
Parameters :
Returns :
Timespan
|
Public stepForwardCustom |
stepForwardCustom(timespan: Timespan, interval: number)
|
Defined in libs/core/src/lib/time/time.service.ts:51
|
Increase timespan by custom interval
Returns :
Timespan
|
import { Injectable } from '@angular/core';
import { plainToClass } from 'class-transformer';
import moment, { duration, MomentInputObject } from 'moment';
import { LocalStorage } from '../local-storage/local-storage.service';
import { BufferedTime, TimeInterval, Timespan } from '../model/internal/timeInterval';
@Injectable()
export class Time {
constructor(
protected localStorage: LocalStorage
) { }
public centerTimespan(timespan: Timespan, date: Date): Timespan {
const halfduration = this.getDuration(timespan).asMilliseconds() / 2;
const from = moment(date).subtract(halfduration).unix() * 1000;
const to = moment(date).add(halfduration).unix() * 1000;
return new Timespan(from, to);
}
public centerTimespanWithDuration(timespan: Timespan, d: moment.Duration): Timespan {
const half = d.asMilliseconds() / 2;
const center = this.getCenterOfTimespan(timespan);
return new Timespan(center - half, center + half);
}
public getCenterOfTimespan(timespan: Timespan): number {
return timespan.from + (timespan.to - timespan.from) / 2;
}
public stepBack(timespan: Timespan): Timespan {
const d = this.getDuration(timespan);
const from = moment(timespan.from).subtract(d).unix() * 1000;
const to = moment(timespan.to).subtract(d).unix() * 1000;
return new Timespan(from, to);
}
public stepForward(timespan: Timespan): Timespan {
const d = this.getDuration(timespan);
const from = moment(timespan.from).add(d).unix() * 1000;
const to = moment(timespan.to).add(d).unix() * 1000;
return new Timespan(from, to);
}
/**
* Increase timespan by custom interval
* @param timespan
* @param interval
*/
public stepForwardCustom(timespan: Timespan, interval: number): Timespan {
const from = moment(timespan.from).add(interval).unix() * 1000;
const to = moment(timespan.to).add(interval).unix() * 1000;
return new Timespan(from, to);
}
public overlaps(timeInterval: TimeInterval, from: number, to: number): boolean {
const timespan = this.createTimespanOfInterval(timeInterval);
if (timespan.from <= to && timespan.to >= from) {
return true;
}
return false;
}
public containsIn(timeInterval: TimeInterval, timestamp: number) {
const timespan = this.createTimespanOfInterval(timeInterval);
return timespan.from <= timestamp && timestamp <= timespan.to;
}
public createTimespanOfInterval(timeInterval: TimeInterval): Timespan {
if (timeInterval instanceof Timespan) {
return timeInterval;
} else if (timeInterval instanceof BufferedTime) {
const d = moment.duration(timeInterval.bufferInterval / 2);
const from = moment(timeInterval.timestamp).subtract(d).unix() * 1000;
const to = moment(timeInterval.timestamp).add(d).unix() * 1000;
return new Timespan(from, to);
} else {
console.error('Wrong time interval!');
}
}
public getBufferedTimespan(timespan: Timespan, factor: number, maxBufferInMs?: number): Timespan {
const durationMillis = this.getDuration(timespan).asMilliseconds();
let buffer = durationMillis * factor;
if (maxBufferInMs && buffer > maxBufferInMs) {
buffer = maxBufferInMs;
}
const from = timespan.from - buffer;
const to = timespan.to + buffer;
return new Timespan(from, to);
}
public saveTimespan(param: string, timespan: Timespan) {
this.localStorage.save(param, timespan);
}
public loadTimespan(param: string): Timespan {
const json = this.localStorage.load<object>(param);
if (json) {
return plainToClass<Timespan, object>(Timespan, json);
}
return null;
}
public initTimespan(): Timespan {
const now = new Date();
const start = moment(now).startOf('day').unix() * 1000;
const end = moment(now).endOf('day').unix() * 1000;
return new Timespan(start, end);
}
public generateTimespan(defaultTimeseriesTimeduration: MomentInputObject, align: 'start' | 'center' | 'end'): Timespan {
const now = new Date();
const d = duration(defaultTimeseriesTimeduration);
switch (align) {
case 'start':
return new Timespan(now.getTime(), now.getTime() + d.asMilliseconds());
case 'end':
return new Timespan(now.getTime() - d.asMilliseconds(), now.getTime());
case 'center':
default:
const half = d.asMilliseconds() / 2;
return new Timespan(now.getTime() - half, now.getTime() + half);
}
}
private getDuration(timespan: Timespan): moment.Duration {
const from = moment(timespan.from);
const to = moment(timespan.to);
return moment.duration(to.diff(from));
}
}