diff --git a/__tests__/js-joda.test.ts b/__tests__/js-joda.test.ts new file mode 100644 index 00000000..2bbe458c --- /dev/null +++ b/__tests__/js-joda.test.ts @@ -0,0 +1,20 @@ +import { LocalDate, LocalTime, LocalDateTime } from '@js-joda/core'; +import JsJodaUtils from "../packages/js-joda/src"; + +describe("js-joda", () => { + const jsJodaUtils = new JsJodaUtils(); + + it("Merges a date with a non-time", () => { + const one = LocalDateTime.of(2022, 12, 5, 9, 30); + const two = LocalDate.of(2022, 12, 6); + + expect(jsJodaUtils.mergeDateAndTime(one, two)).toEqual(one); + }); + + it("Merges a non-date with a time", () => { + const one = LocalTime.of(9, 30); + const two = LocalDateTime.of(2022, 12, 5, 9, 30); + + expect(jsJodaUtils.mergeDateAndTime(one, two)).toEqual(two); + }); +}); diff --git a/packages/js-joda/src/js-joda-utils.ts b/packages/js-joda/src/js-joda-utils.ts index 9fb1945f..859e2729 100644 --- a/packages/js-joda/src/js-joda-utils.ts +++ b/packages/js-joda/src/js-joda-utils.ts @@ -436,12 +436,17 @@ export default class JsJodaUtils implements IUtils { } mergeDateAndTime(date: Temporal, time: Temporal): Temporal { - var qtime = time.query(TemporalQueries.localTime()); - if (qtime == null) { - /* istanbul ignore next */ - return date; + const qdate = date.query(TemporalQueries.localDate()); + const qtime = time.query(TemporalQueries.localTime()); + if (qdate && qtime) { + return qdate.atTime(qtime); + } else if (!qdate) { + // A time merged with a non-date gives the original time. + return time; } else { - return LocalDate.from(date).atTime(LocalTime.from(time)); + // A date merged with a non-time gives the original date. We also use + // this as the fallback / error behavior. + return date; } }