From fe0c1ee84ac59a7dc965d388e56b5bb734700366 Mon Sep 17 00:00:00 2001 From: Jane Sandberg <sandbej@linnbenton.edu> Date: Thu, 11 Jul 2019 12:48:22 -0700 Subject: [PATCH] LP1834662: Fix incorrect locale fallback Resolves a deprecation warning and validation errors in the datetime-select component Signed-off-by: Jane Sandberg <sandbej@linnbenton.edu> Signed-off-by: Bill Erickson <berickxx@gmail.com> --- Open-ILS/src/eg2/src/app/core/format.service.ts | 22 ++++++++++++++-------- Open-ILS/src/eg2/src/app/core/format.spec.ts | 8 ++++---- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/core/format.service.ts b/Open-ILS/src/eg2/src/app/core/format.service.ts index b6fba2dea4..043ea920f3 100644 --- a/Open-ILS/src/eg2/src/app/core/format.service.ts +++ b/Open-ILS/src/eg2/src/app/core/format.service.ts @@ -159,17 +159,24 @@ export class FormatService { idlFormatDatetime(datetime: string, timezone: string): string { return this.momentizeDateTimeString(datetime, timezone).toISOString(); } /** + * Create a Moment from an ISO string + */ + momentizeIsoString(isoString: string, timezone: string): Moment { + return (isoString.length) ? Moment(isoString, timezone) : Moment(); + } + + /** * Turn a date string into a Moment using the date format org setting. */ - momentizeDateString(date: string, timezone: string, strict = false): Moment { - return this.momentize(date, this.makeFormatParseable(this.dateFormat), timezone, strict); + momentizeDateString(date: string, timezone: string, strict?, locale?): Moment { + return this.momentize(date, this.makeFormatParseable(this.dateFormat, locale), timezone, strict); } /** * Turn a datetime string into a Moment using the datetime format org setting. */ - momentizeDateTimeString(date: string, timezone: string, strict = false): Moment { - return this.momentize(date, this.makeFormatParseable(this.dateTimeFormat), timezone, strict); + momentizeDateTimeString(date: string, timezone: string, strict?, locale?): Moment { + return this.momentize(date, this.makeFormatParseable(this.dateTimeFormat, locale), timezone, strict); } /** @@ -184,9 +191,7 @@ export class FormatService { } return Moment.tz(date, format, false, timezone); } - // TODO: The following fallback returns the date at midnight UTC, - // rather than midnight in the local TZ - return Moment.tz(date, timezone); + return Moment(new Date(date), timezone); } } @@ -199,7 +204,7 @@ export class FormatService { */ private makeFormatParseable(original: string, locale?: string): string { if (!original) { return ''; } - if (!locale) { locale = locale; } + if (!locale) { locale = this.locale.currentLocaleCode(); } switch (original) { case 'short': { const template = getLocaleDateTimeFormat(locale, FormatWidth.Short); @@ -299,3 +304,4 @@ export class FormatValuePipe implements PipeTransform { return this.formatter.transform({value: value, datatype: datatype}); } } + diff --git a/Open-ILS/src/eg2/src/app/core/format.spec.ts b/Open-ILS/src/eg2/src/app/core/format.spec.ts index 272ab41b3f..cd5feaa107 100644 --- a/Open-ILS/src/eg2/src/app/core/format.spec.ts +++ b/Open-ILS/src/eg2/src/app/core/format.spec.ts @@ -97,15 +97,15 @@ describe('FormatService', () => { }); it('should transform M/d/yy, h:mm a Angular format string to a valid MomentJS one', () => { - const momentVersion = service['makeFormatParseable']('M/d/yy, h:mm a'); + const momentVersion = service['makeFormatParseable']('M/d/yy, h:mm a', 'en-US'); expect(momentVersion).toBe('M/D/YY, h:mm a'); }); it('should transform MMM d, y, h:mm:ss a Angular format string to a valid MomentJS one', () => { - const momentVersion = service['makeFormatParseable']('MMM d, y, h:mm:ss a'); + const momentVersion = service['makeFormatParseable']('MMM d, y, h:mm:ss a', 'ar-JO'); expect(momentVersion).toBe('MMM D, Y, h:mm:ss a'); }); it('should transform MMMM d, y, h:mm:ss a z Angular format strings to a valid MomentJS one', () => { - const momentVersion = service['makeFormatParseable']('MMMM d, y, h:mm:ss a z'); + const momentVersion = service['makeFormatParseable']('MMMM d, y, h:mm:ss a z', 'fr-CA'); expect(momentVersion).toBe('MMMM D, Y, h:mm:ss a [GMT]Z'); }); it('should transform full Angular format strings to a valid MomentJS one using Angular locale en-US', () => { @@ -133,7 +133,7 @@ describe('FormatService', () => { }); it('can create a valid Momentjs object given a valid datetime string and a dateTimeFormat from org settings', () => { service['dateTimeFormat'] = 'M/D/YY, h:mm a'; - const moment = service.momentizeDateTimeString('7/3/12, 6:06 PM', 'Africa/Addis_Ababa', false); + const moment = service.momentizeDateTimeString('7/3/12, 6:06 PM', 'Africa/Addis_Ababa', false, 'fr-CA'); expect(moment.isValid()).toBe(true); }); -- 2.11.0