Skip to content

Commit

Permalink
#470: Do not display seconds in relative schedules in observations ta…
Browse files Browse the repository at this point in the history
…ble.

* Add new dateUtils function to cut off seconds in a time string.
* Add Unit test for new implemented function.
  • Loading branch information
benitsch committed Jun 28, 2024
1 parent d3b5fb0 commit 685307d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/components/ObservationList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Licensed under the Elastic License 2.0. */
import { ScheduleType } from '../models/Scheduler';
import Button from 'primevue/button';
import Menu from 'primevue/menu';
import { timeToHourMinuteString } from '../utils/dateUtils';
const loader = useLoader();
const { observationsApi } = useObservationsApi();
Expand Down Expand Up @@ -310,13 +311,13 @@ Licensed under the Elastic License 2.0. */
schedule.dtstart.offset?.unit
? `${t(
`scheduler.preview.unit.${schedule.dtstart.offset.unit}`,
)} ${schedule.dtstart.offset.value}, ${schedule.dtstart.time}`
)} ${schedule.dtstart.offset.value}, ${timeToHourMinuteString(schedule.dtstart.time)}`
: undefined;
case 'dtend':
return schedule.dtend.offset?.value && schedule.dtend.offset?.unit
? `${t(`scheduler.preview.unit.${schedule.dtend.offset.unit}`)} ${
schedule.dtend.offset.value
}, ${schedule.dtend.time} `
}, ${timeToHourMinuteString(schedule.dtend.time)} `
: undefined;
default:
return undefined;
Expand Down
15 changes: 15 additions & 0 deletions src/utils/dateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,18 @@ export function ZTimeStringToOffsetTimeString(
}
return undefined;
}

/**
* Converts a time string in the format HH:mm:ss into a character string in the format HH:mm.
* @param time - A time string in the format HH:mm:ss (e.g. "10:30:00") or `undefined`.
* @returns A character string in the format HH:mm (e.g. "10:30") or `undefined` if the input is `undefined`.
*/
export function timeToHourMinuteString(
time: string | undefined,
): string | undefined {
if (time) {
return time.substring(0, 5);
}

return time;
}
38 changes: 38 additions & 0 deletions tests/common/dateUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
dateToDateTimeString,
ZTimeStringToOffsetTimeString,
ZTimeToOffsetTime,
timeToHourMinuteString,
} from '../../src/utils/dateUtils';

describe('dateToDateString', () => {
Expand Down Expand Up @@ -133,3 +134,40 @@ describe('ZTimeStringToOffsetTimeString', () => {
expect(result).toBe(expectedTime);
});
});

describe('timeToHourMinuteString', () => {
it('should return HH:mm when given a time string in HH:mm:ss format', () => {
const time = '10:30:00';
const result = timeToHourMinuteString(time);
expect(result).toBe('10:30');
});

it('should return undefined when given undefined', () => {
const result = timeToHourMinuteString(undefined);
expect(result).toBeUndefined();
});

it('should handle edge cases like short strings correctly', () => {
const time = '10:30';
const result = timeToHourMinuteString(time);
expect(result).toBe('10:30');
});

it('should handle empty string correctly', () => {
const time = '';
const result = timeToHourMinuteString(time);
expect(result).toBe('');
});

it('should handle null string correctly', () => {
const time = null as unknown as string;
const result = timeToHourMinuteString(time);
expect(result).toBeNull();
});

it('should handle time strings with more than 8 characters correctly', () => {
const time = '10:30:00 AM';
const result = timeToHourMinuteString(time);
expect(result).toBe('10:30');
});
});

0 comments on commit 685307d

Please sign in to comment.