Skip to content

Commit

Permalink
Fix UTC date on Schedule Backfill (#2082)
Browse files Browse the repository at this point in the history
* Add getUTCString util and tests

* Use getUTCString util on schedule backfill
  • Loading branch information
laurakwhit authored May 3, 2024
1 parent e4fc4ec commit 8d2e63d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
42 changes: 14 additions & 28 deletions src/lib/pages/schedule-view.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import { refresh, workflowCount } from '$lib/stores/workflows';
import type { OverlapPolicy } from '$lib/types/schedule';
import { decodeURIForSvelte } from '$lib/utilities/encode-uri';
import { formatDate } from '$lib/utilities/format-date';
import { formatDate, getUTCString } from '$lib/utilities/format-date';
import {
routeForScheduleEdit,
routeForSchedules,
Expand Down Expand Up @@ -207,32 +207,6 @@
$: backfillConfirmationModalOpen && updateDefaultBackfillTimes();
const applyTimeSelection = (): {
startTime: string;
endTime: string;
} => {
const startTimeUTC = Date.UTC(
startDate.getFullYear(),
startDate.getMonth(),
startDate.getDay(),
Number(startHour),
Number(startMinute),
Number(startSecond),
);
const startTime = new Date(startTimeUTC).toISOString();
const endTimeUTC = Date.UTC(
endDate.getFullYear(),
endDate.getMonth(),
endDate.getDay(),
Number(endHour),
Number(endMinute),
Number(endSecond),
);
const endTime = new Date(endTimeUTC).toISOString();
return { startTime, endTime };
};
const closeBackfillModal = () => {
backfillConfirmationModalOpen = false;
viewMoreBackfillOptions = false;
Expand All @@ -241,7 +215,19 @@
const handleBackfill = async () => {
scheduleUpdating = true;
const { startTime, endTime } = applyTimeSelection();
const startTime = getUTCString({
date: startDate,
hour: startHour,
minute: startMinute,
second: startSecond,
});
const endTime = getUTCString({
date: endDate,
hour: endHour,
minute: endMinute,
second: endSecond,
});
await backfillRequest({
namespace,
Expand Down
31 changes: 30 additions & 1 deletion src/lib/utilities/format-date.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import {
formatDate,
formatUTCOffset,
getLocalTime,
getSelectedTimezone,
getUTCString,
isValidDate,
} from './format-date';

Expand Down Expand Up @@ -145,3 +146,31 @@ describe('getSelectedTimezone', () => {
expect(getSelectedTimezone('UTC')).toBe('UTC');
});
});

describe('getUTCString', () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2020-01-01').getTime());
});

afterEach(() => {
vi.useRealTimers();
});

it('should default to 00:00 for the current date', () => {
expect(getUTCString()).toBe('2020-01-01T00:00:00.000Z');
});

it('should return a string with hours, minutes, and seconds set', () => {
expect(getUTCString({ hour: 1, minute: 1, second: 1 })).toBe(
'2020-01-01T01:01:01.000Z',
);
expect(getUTCString({ hour: 1, minute: 61, second: 1 })).toBe(
'2020-01-01T02:01:01.000Z',
);
expect(getUTCString({ hour: 1, minute: 1, second: 61 })).toBe(
'2020-01-01T01:02:01.000Z',
);
expect(getUTCString({ hour: 25 })).toBe('2020-01-02T01:00:00.000Z');
});
});
22 changes: 22 additions & 0 deletions src/lib/utilities/format-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,25 @@ export function getSelectedTimezone(timeFormat: TimeFormat): string {

return timeFormat;
}

export function getUTCString({
date = new Date(),
hour = 0,
minute = 0,
second = 0,
}: {
date?: Date;
hour?: string | number;
minute?: string | number;
second?: string | number;
} = {}): string {
const utcTime = Date.UTC(
date.getFullYear(),
date.getMonth(),
date.getDate(),
Number(hour),
Number(minute),
Number(second),
);
return new Date(utcTime).toISOString();
}

0 comments on commit 8d2e63d

Please sign in to comment.