From f228e9f064f27301e59533aac48fd6c2e533890d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Aug 2022 10:26:20 +0200 Subject: [PATCH] More precise date for jobs --- .../src/app/+admin/system/jobs/job.service.ts | 4 +-- .../+admin/system/jobs/jobs.component.html | 2 +- .../app/core/rest/rest-extractor.service.ts | 23 ++++++++++----- client/src/app/helpers/utils/date.ts | 29 +++++++++++++++++-- client/tsconfig.json | 2 +- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/client/src/app/+admin/system/jobs/job.service.ts b/client/src/app/+admin/system/jobs/job.service.ts index 6c4a07469..ef8ddd3b4 100644 --- a/client/src/app/+admin/system/jobs/job.service.ts +++ b/client/src/app/+admin/system/jobs/job.service.ts @@ -34,9 +34,7 @@ export class JobService { return this.authHttp.get>(JobService.BASE_JOB_URL + `/${jobState || ''}`, { params }) .pipe( - map(res => { - return this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'processedOn', 'finishedOn' ]) - }), + map(res => this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'processedOn', 'finishedOn' ], 'precise')), map(res => this.restExtractor.applyToResultListData(res, this.prettyPrintData)), map(res => this.restExtractor.applyToResultListData(res, this.buildUniqId)), catchError(err => this.restExtractor.handleError(err)) diff --git a/client/src/app/+admin/system/jobs/jobs.component.html b/client/src/app/+admin/system/jobs/jobs.component.html index b53fafeba..a5266bde5 100644 --- a/client/src/app/+admin/system/jobs/jobs.component.html +++ b/client/src/app/+admin/system/jobs/jobs.component.html @@ -69,7 +69,7 @@ {{ getProgress(job) }} - {{ job.createdAt | date: 'short' }} + {{ job.createdAt }} diff --git a/client/src/app/core/rest/rest-extractor.service.ts b/client/src/app/core/rest/rest-extractor.service.ts index 8a2974563..7eec2eca6 100644 --- a/client/src/app/core/rest/rest-extractor.service.ts +++ b/client/src/app/core/rest/rest-extractor.service.ts @@ -1,14 +1,17 @@ import { throwError as observableThrowError } from 'rxjs' -import { Injectable } from '@angular/core' +import { Inject, Injectable, LOCALE_ID } from '@angular/core' import { Router } from '@angular/router' -import { dateToHuman } from '@app/helpers' -import { HttpStatusCode, ResultList } from '@shared/models' +import { DateFormat, dateToHuman } from '@app/helpers' import { logger } from '@root-helpers/logger' +import { HttpStatusCode, ResultList } from '@shared/models' @Injectable() export class RestExtractor { - constructor (private router: Router) { } + constructor ( + @Inject(LOCALE_ID) private localeId: string, + private router: Router + ) { } applyToResultListData ( result: ResultList, @@ -23,13 +26,17 @@ export class RestExtractor { } } - convertResultListDateToHuman (result: ResultList, fieldsToConvert: string[] = [ 'createdAt' ]): ResultList { - return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert ]) + convertResultListDateToHuman ( + result: ResultList, + fieldsToConvert: string[] = [ 'createdAt' ], + format?: DateFormat + ): ResultList { + return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert, format ]) } - convertDateToHuman (target: any, fieldsToConvert: string[]) { + convertDateToHuman (target: any, fieldsToConvert: string[], format?: DateFormat) { fieldsToConvert.forEach(field => { - target[field] = dateToHuman(target[field]) + target[field] = dateToHuman(this.localeId, new Date(target[field]), format) }) return target diff --git a/client/src/app/helpers/utils/date.ts b/client/src/app/helpers/utils/date.ts index 012b959ea..75363cc81 100644 --- a/client/src/app/helpers/utils/date.ts +++ b/client/src/app/helpers/utils/date.ts @@ -1,8 +1,29 @@ import { DatePipe } from '@angular/common' -const datePipe = new DatePipe('en') -function dateToHuman (date: string) { - return datePipe.transform(date, 'medium') +let datePipe: DatePipe +let intl: Intl.DateTimeFormat + +type DateFormat = 'medium' | 'precise' + +function dateToHuman (localeId: string, date: Date, format: 'medium' | 'precise' = 'medium') { + if (!datePipe) { + datePipe = new DatePipe(localeId) + } + + if (!intl) { + intl = new Intl.DateTimeFormat(localeId, { + hour: 'numeric', + minute: 'numeric', + second: 'numeric', + year: '2-digit', + month: 'numeric', + day: 'numeric', + fractionalSecondDigits: 3 + }) + } + + if (format === 'medium') return datePipe.transform(date, format) + if (format === 'precise') return intl.format(date) } function durationToString (duration: number) { @@ -20,6 +41,8 @@ function durationToString (duration: number) { } export { + DateFormat, + durationToString, dateToHuman } diff --git a/client/tsconfig.json b/client/tsconfig.json index 7a0584d5c..2345ca289 100644 --- a/client/tsconfig.json +++ b/client/tsconfig.json @@ -20,7 +20,7 @@ "node_modules/@types" ], "lib": [ - "ES2020.Intl", + "ES2021.Intl", "es2018", "es2017", "es2016", -- 2.41.0