From: Bill Erickson Date: Wed, 17 Mar 2021 21:05:59 +0000 (-0400) Subject: LP1904036 Date util class X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=3ebae4889e6d862acb6862876cb0e06f1001a4fc;p=working%2FEvergreen.git LP1904036 Date util class Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/src/eg2/src/app/share/date-select/date-select.component.ts b/Open-ILS/src/eg2/src/app/share/date-select/date-select.component.ts index b66a26188e..bd6e681753 100644 --- a/Open-ILS/src/eg2/src/app/share/date-select/date-select.component.ts +++ b/Open-ILS/src/eg2/src/app/share/date-select/date-select.component.ts @@ -1,6 +1,7 @@ import {Component, OnInit, Input, Output, ViewChild, EventEmitter, forwardRef} from '@angular/core'; import {NgbDateStruct} from '@ng-bootstrap/ng-bootstrap'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms'; +import {DateUtil} from '@eg/share/util/date'; /** * RE: displaying locale dates in the input field: @@ -46,7 +47,7 @@ export class DateSelectComponent implements OnInit, ControlValueAccessor { if (this.current == null) { return null; } if (!this.isValidDate(this.current)) { return null; } const ymd = `${this.current.year}-${String(this.current.month).padStart(2, '0')}-${String(this.current.day).padStart(2, '0')}`; - const date = this.localDateFromYmd(ymd); + const date = DateUtil.localDateFromYmd(ymd); const iso = date.toISOString(); return iso; } @@ -54,7 +55,7 @@ export class DateSelectComponent implements OnInit, ControlValueAccessor { if (this.current == null) { return null; } if (!this.isValidDate(this.current)) { return null; } const ymd = `${this.current.year}-${String(this.current.month).padStart(2, '0')}-${String(this.current.day).padStart(2, '0')}`; - const date = this.localDateFromYmd(ymd); + const date = DateUtil.localDateFromYmd(ymd); return date; } @@ -72,7 +73,7 @@ export class DateSelectComponent implements OnInit, ControlValueAccessor { ngOnInit() { if (this.initialYmd) { - this.initialDate = this.localDateFromYmd(this.initialYmd); + this.initialDate = DateUtil.localDateFromYmd(this.initialYmd); } else if (this.initialIso) { this.initialDate = new Date(this.initialIso); @@ -99,7 +100,7 @@ export class DateSelectComponent implements OnInit, ControlValueAccessor { onDateSelect(evt) { const ymd = `${evt.year}-${String(evt.month).padStart(2, '0')}-${String(evt.day).padStart(2, '0')}`; - const date = this.localDateFromYmd(ymd); + const date = DateUtil.localDateFromYmd(ymd); const iso = date.toISOString(); this.onChangeAsDate.emit(date); this.onChangeAsYmd.emit(ymd); @@ -107,14 +108,6 @@ export class DateSelectComponent implements OnInit, ControlValueAccessor { this.propagateChange(date); } - // Create a date in the local time zone with selected YMD values. - // TODO: Consider moving this to a date service... - localDateFromYmd(ymd: string): Date { - const parts = ymd.split('-'); - return new Date( - Number(parts[0]), Number(parts[1]) - 1, Number(parts[2])); - } - reset() { this.current = { year: null, diff --git a/Open-ILS/src/eg2/src/app/share/util/date.ts b/Open-ILS/src/eg2/src/app/share/util/date.ts new file mode 100644 index 0000000000..cb6655435c --- /dev/null +++ b/Open-ILS/src/eg2/src/app/share/util/date.ts @@ -0,0 +1,57 @@ + +/* Utility code for dates */ + +export class DateUtil { + + /** + * Converts an interval string to seconds. + * + * intervalToSeconds('1 min 2 seconds')) => 62 + * intervalToSeconds('2 days')) => 172800 (except across time changes) + * intervalToSeconds('02:00:23')) => 7223 + */ + static intervalToSeconds(interval: string): number { + const d = new Date(); + let start = d.getTime(); + const parts = interval.split(' '); + + for(let i = 0; i < parts.length; i += 2) { + + if (!parts[i+1]) { + // interval is a bare hour:min:sec string + const times = parts[i].split(':'); + d.setHours(d.getHours() + Number(times[0])); + d.setMinutes(d.getMinutes() + Number(times[1])); + d.setSeconds(d.getSeconds() + Number(times[2])); + continue; + } + + const count = Number(parts[i]); + const partType = parts[i+1].replace(/s?,?$/, ''); + + if (partType.match(/^s/)) { + d.setSeconds(d.getSeconds() + count); + } else if (partType.match(/^min/)) { + d.setMinutes(d.getMinutes() + count); + } else if (partType.match(/^h/)) { + d.setHours(d.getHours() + count); + } else if (partType.match(/^d/)) { + d.setDate(d.getDate() + count); + } else if (partType.match(/^mon/)) { + d.setMonth(d.getMonth() + count); + } else if (partType.match(/^y/)) { + d.setFullYear(d.getFullYear() + count); + } + } + + return Number((d.getTime() - start) / 1000); + } + + // Create a date in the local time zone with selected YMD values. + static localDateFromYmd(ymd: string): Date { + const parts = ymd.split('-'); + return new Date( + Number(parts[0]), Number(parts[1]) - 1, Number(parts[2])); + } +} +