From b17bce1cd247f3a7859c9a00e2047dafea9a173a Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Mon, 25 Jan 2021 20:18:04 +0100 Subject: [PATCH] Add default unitScale --- .../src/fm/services/timespan.service.ts | 159 +++++++++--------- 1 file changed, 84 insertions(+), 75 deletions(-) diff --git a/projects/common/src/fm/services/timespan.service.ts b/projects/common/src/fm/services/timespan.service.ts index 6c7ce41..e728c2c 100644 --- a/projects/common/src/fm/services/timespan.service.ts +++ b/projects/common/src/fm/services/timespan.service.ts @@ -1,75 +1,84 @@ -import { Injectable } from '@angular/core'; -import { DatePipe } from '@angular/common'; -import { Observable , Observer } from 'rxjs'; -import { ITypeaheadItem } from '../models/typeahead.item'; - -@Injectable({ - providedIn: 'root', -}) -export class TimespanService { - constructor(private datePipe: DatePipe) { - } - unitScales: number[] = [1, 1000, 1000 * 60, 1000 * 60 * 60, 1000 * 60 * 60 * 24, 1000 * 60 * 60 * 24 * 7, 1000 * 60 * 60 * 24 * 31, 1000 * 60 * 60 * 24 * 31 * 3, 1000 * 60 * 60 * 24 * 365.25]; - units: string[] = ['millisecond', 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year']; - quarters: string[] = ['KW1', 'KW2', 'KW3', 'KW4']; - - getStartEndCaption(date: Date, otherDate: Date, unitScale: number, suffix: boolean = false, extended: boolean = true): string { - let showSuffix = false; - otherDate = new Date(otherDate.getTime() - 1); // fix year edge case - if (unitScale == 3) { - let format = "HH:00"; - if (extended) { - if (suffix || date.getFullYear() != otherDate.getFullYear()) - format = "d MMM yyyy:HH:00"; - else if (date.getMonth() !== otherDate.getMonth()) - format = "d MMM HH:00"; - } - return this.datePipe.transform(date, format); - - } - if (unitScale == 4) { - let format = "d"; - if (extended) { - if (suffix || date.getFullYear() != otherDate.getFullYear()) - format = "d MMM yyyy"; - else if (date.getMonth() !== otherDate.getMonth()) - format = "d MMM" - } - return this.datePipe.transform(date, format); - - } - if (unitScale == 6) { - let format = "MMM"; - if (extended) { - if (suffix || date.getFullYear() != otherDate.getFullYear()) - format = "MMM yyyy"; - } - return this.datePipe.transform(date, format); - } - if (unitScale == 7) { - let q = Math.trunc(date.getMonth() / 3); - return this.quarters[q]; - } - if (unitScale == 8) { - return this.datePipe.transform(date, "yyyy"); - } - return ""; - } - - getStartCaption(startDate: Date, endDate: Date, unitScale: number, suffix: boolean = false, extended: boolean = true): string { - return this.getStartEndCaption(new Date(startDate.getTime() + (this.unitScales[unitScale] / 2)), endDate, unitScale, suffix, extended); - } - - getEndCaption(startDate: Date,endDate: Date, unitScale: number, suffix: boolean = true): string { - return this.getStartEndCaption(new Date(endDate.getTime() - (this.unitScales[unitScale] / 2)), startDate, unitScale, suffix); - } - - getCaption(startDate: Date, endDate: Date, unitScale: number): string { - let startCaption = this.getStartCaption(startDate, endDate, unitScale); - let endCaption = this.getEndCaption(startDate,endDate, unitScale); - if ((endDate.getTime() - startDate.getTime()) < (1.5 * this.unitScales[unitScale])) - return endCaption; - return `${startCaption}-${endCaption}`; - } - -} +import { Injectable } from '@angular/core'; +import { DatePipe } from '@angular/common'; +import { Observable , Observer } from 'rxjs'; +import { ITypeaheadItem } from '../models/typeahead.item'; + +@Injectable({ + providedIn: 'root', +}) +export class TimespanService { + constructor(private datePipe: DatePipe) { + } + unitScales: number[] = [1, 1000, 1000 * 60, 1000 * 60 * 60, 1000 * 60 * 60 * 24, 1000 * 60 * 60 * 24 * 7, 1000 * 60 * 60 * 24 * 31, 1000 * 60 * 60 * 24 * 31 * 3, 1000 * 60 * 60 * 24 * 365.25]; + units: string[] = ['millisecond', 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year']; + quarters: string[] = ['KW1', 'KW2', 'KW3', 'KW4']; + + getStartEndCaption(date: Date, otherDate: Date, unitScale: number, suffix: boolean = false, extended: boolean = true): string { + let showSuffix = false; + otherDate = new Date(otherDate.getTime() - 1); // fix year edge case + if (unitScale == 3) { + let format = "HH:00"; + if (extended) { + if (suffix || date.getFullYear() != otherDate.getFullYear()) + format = "d MMM yyyy:HH:00"; + else if (date.getMonth() !== otherDate.getMonth()) + format = "d MMM HH:00"; + } + return this.datePipe.transform(date, format); + + } + if (unitScale == 4) { + let format = "d"; + if (extended) { + if (suffix || date.getFullYear() != otherDate.getFullYear()) + format = "d MMM yyyy"; + else if (date.getMonth() !== otherDate.getMonth()) + format = "d MMM" + } + return this.datePipe.transform(date, format); + + } + if (unitScale == 6) { + let format = "MMM"; + if (extended) { + if (suffix || date.getFullYear() != otherDate.getFullYear()) + format = "MMM yyyy"; + } + return this.datePipe.transform(date, format); + } + if (unitScale == 7) { + let q = Math.trunc(date.getMonth() / 3); + return this.quarters[q]; + } + if (unitScale == 8) { + return this.datePipe.transform(date, "yyyy"); + } + return ""; + } + + getStartCaption(startDate: Date, endDate: Date, unitScale: number, suffix: boolean = false, extended: boolean = true): string { + return this.getStartEndCaption(new Date(startDate.getTime() + (this.unitScales[unitScale] / 2)), endDate, unitScale, suffix, extended); + } + + getEndCaption(startDate: Date,endDate: Date, unitScale: number, suffix: boolean = true): string { + return this.getStartEndCaption(new Date(endDate.getTime() - (this.unitScales[unitScale] / 2)), startDate, unitScale, suffix); + } + + getCaption(startDate: Date, endDate: Date, unitScale?: number): string { + let scale = unitScale; + if(unitScale==null) { + scale=4; + if(startDate.getUTCMonth() == 0 && startDate.getUTCDate() == 1 && startDate.getUTCHours() == 0 && startDate.getUTCMinutes() ==0 && startDate.getUTCSeconds() ==0 && + endDate.getUTCMonth() == 0 && endDate.getUTCDate() == 1 && endDate.getUTCHours() == 0 && endDate.getUTCMinutes() ==0 && endDate.getUTCSeconds() ==0) { + scale=8; + } else if (startDate.getUTCHours() > 0 && startDate.getUTCMinutes() >0) { + scale =3 + } + } + let startCaption = this.getStartCaption(startDate, endDate, scale); + let endCaption = this.getEndCaption(startDate,endDate, scale); + if ((endDate.getTime() - startDate.getTime()) < (1.5 * this.unitScales[scale])) + return endCaption; + return `${startCaption}-${endCaption}`; + } +}