diff --git a/src/components/ObservationList.vue b/src/components/ObservationList.vue index d7a558e..b6b3d38 100644 --- a/src/components/ObservationList.vue +++ b/src/components/ObservationList.vue @@ -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(); @@ -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; diff --git a/src/utils/dateUtils.ts b/src/utils/dateUtils.ts index c316b5b..da5653a 100644 --- a/src/utils/dateUtils.ts +++ b/src/utils/dateUtils.ts @@ -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; +} diff --git a/tests/common/dateUtils.spec.ts b/tests/common/dateUtils.spec.ts index 3f59675..fad7e39 100644 --- a/tests/common/dateUtils.spec.ts +++ b/tests/common/dateUtils.spec.ts @@ -13,6 +13,7 @@ import { dateToDateTimeString, ZTimeStringToOffsetTimeString, ZTimeToOffsetTime, + timeToHourMinuteString, } from '../../src/utils/dateUtils'; describe('dateToDateString', () => { @@ -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'); + }); +});