Skip to content

Commit

Permalink
Merge pull request #2866 from rsun19/fix-end-date
Browse files Browse the repository at this point in the history
fixed end date issue and issues scheduling pipelines in Firefox
  • Loading branch information
openshift-merge-bot[bot] authored Jun 11, 2024
2 parents ed9f7e4 + c65b901 commit 41922cd
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import EndDateBeforeStartDateError from '~/concepts/pipelines/content/createRun/
import CatchUp from '~/concepts/pipelines/content/createRun/contentSections/CatchUp';
import MaxConcurrencyField from '~/concepts/pipelines/content/createRun/contentSections/MaxConcurrencyField';
import TriggerTypeField from '~/concepts/pipelines/content/createRun/contentSections/TriggerTypeField';
import { convertToDate } from '~/utilities/time';

type RunTypeSectionScheduledProps = {
data: RunTypeScheduledData;
Expand Down Expand Up @@ -39,7 +40,7 @@ const RunTypeSectionScheduled: React.FC<RunTypeSectionScheduledProps> = ({ data,
onChange={(end) => onChange({ ...data, end })}
adjustNow={(now) => {
if (data.start) {
const start = new Date(`${data.start.date} ${data.start.time}`);
const start = convertToDate(data.start);
start.setDate(start.getDate() + 7);
return start;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
getInputDefinitionParams,
isFilledRunFormData,
} from '~/concepts/pipelines/content/createRun/utils';
import { convertPeriodicTimeToSeconds } from '~/utilities/time';
import { convertPeriodicTimeToSeconds, convertToDate } from '~/utilities/time';

const createRun = async (
formData: SafeRunFormData,
Expand All @@ -47,12 +47,11 @@ const createRun = async (
return createPipelineRun({}, data);
};

const convertDateDataToKFDateTime = (dateData?: RunDateTime): DateTimeKF | null => {
export const convertDateDataToKFDateTime = (dateData?: RunDateTime): DateTimeKF | null => {
if (!dateData) {
return null;
}

const date = new Date(`${dateData.date} ${dateData.time}`);
const date = convertToDate(dateData);
return date.toISOString();
};

Expand Down
11 changes: 4 additions & 7 deletions frontend/src/concepts/pipelines/content/createRun/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
ScheduledType,
} from '~/concepts/pipelines/content/createRun/types';
import { ParametersKF, PipelineVersionKFv2 } from '~/concepts/pipelines/kfTypes';

import { getCorePipelineSpec } from '~/concepts/pipelines/getCorePipelineSpec';
import { convertToDate } from '~/utilities/time';

const runTypeSafeData = (runType: RunFormData['runType']): boolean =>
runType.type !== RunTypeOption.SCHEDULED ||
Expand All @@ -18,19 +18,16 @@ export const isStartBeforeEnd = (start?: RunDateTime, end?: RunDateTime): boolea
if (!start || !end) {
return true;
}

const startDate = new Date(`${start.date} ${start.time}`);
const endDate = new Date(`${end.date} ${end.time}`);

const startDate = convertToDate(start);
const endDate = convertToDate(end);
return endDate.getTime() - startDate.getTime() > 0;
};

const isValidDate = (value?: RunDateTime): boolean => {
if (!value) {
return true;
}

const date = new Date(`${value.date} ${value.time}`);
const date = convertToDate(value);
return date.toString() !== 'Invalid Date';
};

Expand Down
50 changes: 50 additions & 0 deletions frontend/src/utilities/__tests__/time.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RunDateTime } from '~/concepts/pipelines/content/createRun/types';
import {
convertPeriodicTimeToSeconds,
convertSecondsToPeriodicTime,
Expand All @@ -7,6 +8,8 @@ import {
ensureTimeFormat,
printSeconds,
relativeTime,
convertToTwentyFourHourTime,
convertToDate,
} from '~/utilities/time';

describe('relativeDuration', () => {
Expand Down Expand Up @@ -51,6 +54,53 @@ describe('convertDateToTimeString', () => {
});
});

describe('convertToDate', () => {
it('should convert to local date', () => {
const value: RunDateTime = {
date: '2024-01-04',
time: '11:55 PM',
};
expect(convertToDate(value)).toStrictEqual(new Date('2024-01-04T23:55:00.000'));
});

it('should convert to local date using convertToTwentyFourHourTime function', () => {
const value: RunDateTime = {
date: '2024-01-04',
time: '12:30 PM',
};
expect(convertToDate(value)).toStrictEqual(
new Date(`${value.date}T${convertToTwentyFourHourTime(value.time)}:00.000`),
);
});
});

describe('convertToTwentyFourHourTime', () => {
it('should convert 12 hour time to 24 hour time', () => {
const time = '1:30 AM';
expect(convertToTwentyFourHourTime(time)).toBe('01:30');
});

it('should convert past 12', () => {
const time = '2:55 PM';
expect(convertToTwentyFourHourTime(time)).toBe('14:55');
});

it('should convert from 10 AM <= x < 12PM to 24 hour time', () => {
const time = '11:24 AM';
expect(convertToTwentyFourHourTime(time)).toBe('11:24');
});

it('should convert from 12:30 PM to 12:30', () => {
const time = '12:30 PM';
expect(convertToTwentyFourHourTime(time)).toBe('12:30');
});

it('should convert from 12:30 AM to 00:30', () => {
const time = '12:30 AM';
expect(convertToTwentyFourHourTime(time)).toBe('00:30');
});
});

describe('ensureTimeFormat', () => {
it('should return time value', () => {
expect(ensureTimeFormat('3:30 PM')).toBe('3:30 PM');
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/utilities/time.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
PeriodicOptions,
RunDateTime,
periodicOptionAsSeconds,
} from '~/concepts/pipelines/content/createRun/types';

Expand Down Expand Up @@ -42,6 +43,21 @@ export const convertDateToTimeString = (date?: Date): string | null => {
return `${hoursIn12}:${leadZero(date.getMinutes())} ${hours >= 12 ? 'PM' : 'AM'}`;
};

export const convertToDate = ({ date, time }: RunDateTime): Date =>
new Date(`${date}T${convertToTwentyFourHourTime(time)}:00.000`);

/* Return HH:MM from HH:MM *M */
export const convertToTwentyFourHourTime = (time: string): string => {
const timeArray = time.trim().split(' ');
const hourMinutesArray = timeArray[0].trim().split(':');
const hour = Number(hourMinutesArray[0]);
const minutes = hourMinutesArray[1];
if (timeArray[1] === 'AM') {
return `${hour === 12 ? 0 : hour}:${minutes}`.padStart(5, '0');
}
return `${hour === 12 ? hour : hour + 12}:${minutes}`;
};

/** The TimeField component can sometimes cause '6:00PM' instead of '6:00 PM' if the user edits directly */
export const ensureTimeFormat = (time: string): string | null => {
if (/\s[AP]M/.test(time)) {
Expand Down

0 comments on commit 41922cd

Please sign in to comment.