From 6d13ba42c61e3a5286d3be6d393abfbdaf15f1fc Mon Sep 17 00:00:00 2001 From: Tim Donohue Date: Thu, 1 Jul 2021 15:12:23 -0500 Subject: [PATCH 1/3] Fix saving of dates to UTC --- src/app/shared/date.util.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/app/shared/date.util.ts b/src/app/shared/date.util.ts index 44afdd10a4..afd7c09bd5 100644 --- a/src/app/shared/date.util.ts +++ b/src/app/shared/date.util.ts @@ -31,9 +31,9 @@ export function dateToISOFormat(date: Date | NgbDateStruct | string): string { const dateObj: Date = (date instanceof Date) ? date : ((typeof date === 'string') ? ngbDateStructToDate(stringToNgbDateStruct(date)) : ngbDateStructToDate(date)); - let year = dateObj.getFullYear().toString(); - let month = (dateObj.getMonth() + 1).toString(); - let day = dateObj.getDate().toString(); + let year = dateObj.getUTCFullYear().toString(); + let month = (dateObj.getUTCMonth() + 1).toString(); + let day = dateObj.getUTCDate().toString(); let hour = dateObj.getHours().toString(); let min = dateObj.getMinutes().toString(); let sec = dateObj.getSeconds().toString(); @@ -86,9 +86,9 @@ export function dateToNgbDateStruct(date?: Date): NgbDateStruct { } return { - year: date.getFullYear(), - month: date.getMonth() + 1, - day: date.getDate() + year: date.getUTCFullYear(), + month: date.getUTCMonth() + 1, + day: date.getUTCDate() }; } @@ -103,9 +103,9 @@ export function dateToNgbDateStruct(date?: Date): NgbDateStruct { export function dateToString(date: Date | NgbDateStruct): string { const dateObj: Date = (date instanceof Date) ? date : ngbDateStructToDate(date); - let year = dateObj.getFullYear().toString(); - let month = (dateObj.getMonth() + 1).toString(); - let day = dateObj.getDate().toString(); + let year = dateObj.getUTCFullYear().toString(); + let month = (dateObj.getUTCMonth() + 1).toString(); + let day = dateObj.getUTCDate().toString(); year = (year.length === 1) ? '0' + year : year; month = (month.length === 1) ? '0' + month : month; From 9ee733ea80ae6b1bc274fd18be49833c4a23a588 Mon Sep 17 00:00:00 2001 From: Tim Donohue Date: Thu, 1 Jul 2021 17:17:53 -0500 Subject: [PATCH 2/3] Stop using localized Dates. Ensure we always use UTC dates, as the backend expects/uses UTC. --- .../browse-by-date-page.component.spec.ts | 2 +- .../browse-by-date-page.component.ts | 2 +- .../date-picker/date-picker.component.ts | 6 +++--- .../search-range-filter.component.ts | 2 +- .../edit/section-upload-file-edit.component.ts | 18 +++++++++--------- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/app/+browse-by/+browse-by-date-page/browse-by-date-page.component.spec.ts b/src/app/+browse-by/+browse-by-date-page/browse-by-date-page.component.spec.ts index a5cc69e430..5ce52d0002 100644 --- a/src/app/+browse-by/+browse-by-date-page/browse-by-date-page.component.spec.ts +++ b/src/app/+browse-by/+browse-by-date-page/browse-by-date-page.component.spec.ts @@ -107,6 +107,6 @@ describe('BrowseByDatePageComponent', () => { }); it('should create a list of startsWith options with the current year first', () => { - expect(comp.startsWithOptions[0]).toEqual(new Date().getFullYear()); + expect(comp.startsWithOptions[0]).toEqual(new Date().getUTCFullYear()); }); }); diff --git a/src/app/+browse-by/+browse-by-date-page/browse-by-date-page.component.ts b/src/app/+browse-by/+browse-by-date-page/browse-by-date-page.component.ts index a9eaa09e2f..f579085b5c 100644 --- a/src/app/+browse-by/+browse-by-date-page/browse-by-date-page.component.ts +++ b/src/app/+browse-by/+browse-by-date-page/browse-by-date-page.component.ts @@ -92,7 +92,7 @@ export class BrowseByDatePageComponent extends BrowseByMetadataPageComponent { } } const options = []; - const currentYear = new Date().getFullYear(); + const currentYear = new Date().getUTCFullYear(); const oneYearBreak = Math.floor((currentYear - environment.browseBy.oneYearLimit) / 5) * 5; const fiveYearBreak = Math.floor((currentYear - environment.browseBy.fiveYearLimit) / 10) * 10; if (lowerLimit <= fiveYearBreak) { diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/date-picker/date-picker.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/date-picker/date-picker.component.ts index 6989535aa3..438f78a6a0 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/date-picker/date-picker.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/date-picker/date-picker.component.ts @@ -57,9 +57,9 @@ export class DsDatePickerComponent extends DynamicFormControlComponent implement ngOnInit() { const now = new Date(); - this.initialYear = now.getFullYear(); - this.initialMonth = now.getMonth() + 1; - this.initialDay = now.getDate(); + this.initialYear = now.getUTCFullYear(); + this.initialMonth = now.getUTCMonth() + 1; + this.initialDay = now.getUTCDate(); if (this.model && this.model.value !== null) { const values = this.model.value.toString().split(DS_DATE_PICKER_SEPARATOR); diff --git a/src/app/shared/search/search-filters/search-filter/search-range-filter/search-range-filter.component.ts b/src/app/shared/search/search-filters/search-filter/search-range-filter/search-range-filter.component.ts index b23a2d8224..a9ad789d4d 100644 --- a/src/app/shared/search/search-filters/search-filter/search-range-filter/search-range-filter.component.ts +++ b/src/app/shared/search/search-filters/search-filter/search-range-filter/search-range-filter.component.ts @@ -56,7 +56,7 @@ export class SearchRangeFilterComponent extends SearchFacetFilterComponent imple /** * Fallback maximum for the range */ - max = new Date().getFullYear(); + max = new Date().getUTCFullYear(); /** * The current range of the filter diff --git a/src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts b/src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts index 3275787984..96725f151e 100644 --- a/src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts +++ b/src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts @@ -244,9 +244,9 @@ export class SubmissionSectionUploadFileEditComponent implements OnChanges { if (metadataModel.type === DYNAMIC_FORM_CONTROL_TYPE_DATEPICKER) { const date = new Date(accessCondition[key]); metadataModel.value = { - year: date.getFullYear(), - month: date.getMonth() + 1, - day: date.getDate() + year: date.getUTCFullYear(), + month: date.getUTCMonth() + 1, + day: date.getUTCDate() }; } else { metadataModel.value = accessCondition[key]; @@ -302,9 +302,9 @@ export class SubmissionSectionUploadFileEditComponent implements OnChanges { const min = new Date(accessCondition.maxStartDate); startDateModel.max = { - year: min.getFullYear(), - month: min.getMonth() + 1, - day: min.getDate() + year: min.getUTCFullYear(), + month: min.getUTCMonth() + 1, + day: min.getUTCDate() }; } if (accessCondition.hasEndDate) { @@ -314,9 +314,9 @@ export class SubmissionSectionUploadFileEditComponent implements OnChanges { const max = new Date(accessCondition.maxEndDate); endDateModel.max = { - year: max.getFullYear(), - month: max.getMonth() + 1, - day: max.getDate() + year: max.getUTCFullYear(), + month: max.getUTCMonth() + 1, + day: max.getUTCDate() }; } } From 008e0cb66cd3c43235ff3dd3b0efe9a5357c0b6e Mon Sep 17 00:00:00 2001 From: Tim Donohue Date: Mon, 12 Jul 2021 11:47:53 -0500 Subject: [PATCH 3/3] Another small fix from @atarix83 --- src/app/shared/date.util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/shared/date.util.ts b/src/app/shared/date.util.ts index afd7c09bd5..3a0c735717 100644 --- a/src/app/shared/date.util.ts +++ b/src/app/shared/date.util.ts @@ -57,7 +57,7 @@ export function dateToISOFormat(date: Date | NgbDateStruct | string): string { * the Date object */ export function ngbDateStructToDate(date: NgbDateStruct): Date { - return new Date(date.year, (date.month - 1), date.day); + return new Date(Date.UTC(date.year, (date.month - 1), date.day)); } /**