Skip to content

Commit

Permalink
AAE-23640 Fix dates
Browse files Browse the repository at this point in the history
  • Loading branch information
pmartinezga authored and BSekula committed Jul 24, 2024
1 parent 888b5fc commit 252c339
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
33 changes: 33 additions & 0 deletions lib/core/src/lib/common/utils/date-fns-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,37 @@ describe('DateFnsUtils', () => {
expect(forceLocalDateJapan.getMonth()).toBe(0);
expect(forceLocalDateJapan.getFullYear()).toBe(2020);
});

it('should detect if a formatted string contains a timezone', () => {
let result = DateFnsUtils.stringDateContainsTimeZone('2021-06-09T14:10');
expect(result).toEqual(false);

result = DateFnsUtils.stringDateContainsTimeZone('2021-06-09T14:10:00');
expect(result).toEqual(false);

result = DateFnsUtils.stringDateContainsTimeZone('2021-06-09T14:10:00Z');
expect(result).toEqual(true);

result = DateFnsUtils.stringDateContainsTimeZone('2021-06-09T14:10:00+00:00');
expect(result).toEqual(true);

result = DateFnsUtils.stringDateContainsTimeZone('2021-06-09T14:10:00-00:00');
expect(result).toEqual(true);
});

it('should get the date from number', () => {
const spyUtcToLocal = spyOn(DateFnsUtils, 'utcToLocal').and.callThrough();

const date = DateFnsUtils.getDate(1623232200000);
expect(date.toISOString()).toBe('2021-06-09T09:50:00.000Z');
expect(spyUtcToLocal).not.toHaveBeenCalled();
});

it('should get transformed date when string date does not contain the timezone', () => {
const spyUtcToLocal = spyOn(DateFnsUtils, 'utcToLocal').and.callThrough();

DateFnsUtils.getDate('2021-06-09T14:10:00');

expect(spyUtcToLocal).toHaveBeenCalled();
});
});
14 changes: 14 additions & 0 deletions lib/core/src/lib/common/utils/date-fns-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,18 @@ export class DateFnsUtils {
const utcDate = `${date.getFullYear()}-${panDate(date.getMonth() + 1)}-${panDate(date.getDate())}T00:00:00.000Z`;
return new Date(utcDate);
}

static stringDateContainsTimeZone(value: string): boolean {
return /(Z|([+|-]\d\d:?\d\d))$/.test(value);
}

static getDate(value: string | number | Date): Date {
let date = new Date(value);

if (typeof value === 'string' && !DateFnsUtils.stringDateContainsTimeZone(value)) {
date = this.utcToLocal(date);
}

return date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class DateTimeFieldValidator implements FormFieldValidator {
}

static isValidDateTime(input: string): boolean {
const date = new Date(input);
const date = DateFnsUtils.getDate(input);
return isDateValid(date);
}

Expand Down Expand Up @@ -275,12 +275,12 @@ export class MinDateTimeFieldValidator implements FormFieldValidator {

private checkDateTime(field: FormFieldModel): boolean {
let isValid = true;
const fieldValueDate = new Date(field.value);
const min = new Date(field.minValue);
const fieldValueDate = DateFnsUtils.getDate(field.value);
const min = DateFnsUtils.getDate(field.minValue);

if (isBefore(fieldValueDate, min)) {
field.validationSummary.message = `FORM.FIELD.VALIDATOR.NOT_LESS_THAN`;
field.validationSummary.attributes.set('minValue', DateFnsUtils.formatDate(DateFnsUtils.utcToLocal(min), field.dateDisplayFormat));
field.validationSummary.attributes.set('minValue', DateFnsUtils.formatDate(min, field.dateDisplayFormat));
isValid = false;
}
return isValid;
Expand Down Expand Up @@ -317,12 +317,12 @@ export class MaxDateTimeFieldValidator implements FormFieldValidator {

private checkDateTime(field: FormFieldModel): boolean {
let isValid = true;
const fieldValueDate = new Date(field.value);
const max = new Date(field.maxValue);
const fieldValueDate = DateFnsUtils.getDate(field.value);
const max = DateFnsUtils.getDate(field.maxValue);

if (isAfter(fieldValueDate, max)) {
field.validationSummary.message = `FORM.FIELD.VALIDATOR.NOT_GREATER_THAN`;
field.validationSummary.attributes.set('maxValue', DateFnsUtils.formatDate(DateFnsUtils.utcToLocal(max), field.dateDisplayFormat));
field.validationSummary.attributes.set('maxValue', DateFnsUtils.formatDate(max, field.dateDisplayFormat));
isValid = false;
}
return isValid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ export class FormFieldModel extends FormWidgetModel {
this.value = new Date();
}

const dateTimeValue = this.value !== null ? new Date(this.value) : null;
const dateTimeValue = this.value !== null ? DateFnsUtils.getDate(this.value) : null;

if (isValidDate(dateTimeValue)) {
this.form.values[this.id] = dateTimeValue.toISOString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit {

if (this.field) {
if (this.field.minValue) {
this.minDate = DateFnsUtils.utcToLocal(new Date(this.field.minValue));
this.minDate = DateFnsUtils.getDate(this.field.minValue);
}

if (this.field.maxValue) {
this.maxDate = DateFnsUtils.utcToLocal(new Date(this.field.maxValue));
this.maxDate = DateFnsUtils.getDate(this.field.maxValue);
}

if (this.field.value) {
this.value = new Date(this.field.value);
this.value = DateFnsUtils.getDate(this.field.value);
}
}
}
Expand All @@ -85,11 +85,12 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit {
const newValue = this.dateTimeAdapter.parse(input.value, this.field.dateDisplayFormat);

if (isValid(newValue)) {
this.field.value = DateFnsUtils.localToUtc(newValue).toISOString();
this.field.value = newValue.toISOString();
} else {
this.field.value = input.value;
}

this.value = DateFnsUtils.getDate(this.field.value);
this.onFieldChanged(this.field);
}

Expand All @@ -98,7 +99,7 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit {
const input = event.targetElement as HTMLInputElement;

if (newValue && isValid(newValue)) {
this.field.value = DateFnsUtils.localToUtc(newValue).toISOString();
this.field.value = newValue.toISOString();
} else {
this.field.value = input.value;
}
Expand Down

0 comments on commit 252c339

Please sign in to comment.