1 | import { Meridiem } from "../types.js";
|
2 | import { assignSimilarDate, implySimilarDate } from "../utils/dayjs.js";
|
3 | export function mergeDateTimeResult(dateResult, timeResult) {
|
4 | const result = dateResult.clone();
|
5 | const beginDate = dateResult.start;
|
6 | const beginTime = timeResult.start;
|
7 | result.start = mergeDateTimeComponent(beginDate, beginTime);
|
8 | if (dateResult.end != null || timeResult.end != null) {
|
9 | const endDate = dateResult.end == null ? dateResult.start : dateResult.end;
|
10 | const endTime = timeResult.end == null ? timeResult.start : timeResult.end;
|
11 | const endDateTime = mergeDateTimeComponent(endDate, endTime);
|
12 | if (dateResult.end == null && endDateTime.date().getTime() < result.start.date().getTime()) {
|
13 | const nextDayJs = endDateTime.dayjs().add(1, "day");
|
14 | if (endDateTime.isCertain("day")) {
|
15 | assignSimilarDate(endDateTime, nextDayJs);
|
16 | }
|
17 | else {
|
18 | implySimilarDate(endDateTime, nextDayJs);
|
19 | }
|
20 | }
|
21 | result.end = endDateTime;
|
22 | }
|
23 | return result;
|
24 | }
|
25 | export function mergeDateTimeComponent(dateComponent, timeComponent) {
|
26 | const dateTimeComponent = dateComponent.clone();
|
27 | if (timeComponent.isCertain("hour")) {
|
28 | dateTimeComponent.assign("hour", timeComponent.get("hour"));
|
29 | dateTimeComponent.assign("minute", timeComponent.get("minute"));
|
30 | if (timeComponent.isCertain("second")) {
|
31 | dateTimeComponent.assign("second", timeComponent.get("second"));
|
32 | if (timeComponent.isCertain("millisecond")) {
|
33 | dateTimeComponent.assign("millisecond", timeComponent.get("millisecond"));
|
34 | }
|
35 | else {
|
36 | dateTimeComponent.imply("millisecond", timeComponent.get("millisecond"));
|
37 | }
|
38 | }
|
39 | else {
|
40 | dateTimeComponent.imply("second", timeComponent.get("second"));
|
41 | dateTimeComponent.imply("millisecond", timeComponent.get("millisecond"));
|
42 | }
|
43 | }
|
44 | else {
|
45 | dateTimeComponent.imply("hour", timeComponent.get("hour"));
|
46 | dateTimeComponent.imply("minute", timeComponent.get("minute"));
|
47 | dateTimeComponent.imply("second", timeComponent.get("second"));
|
48 | dateTimeComponent.imply("millisecond", timeComponent.get("millisecond"));
|
49 | }
|
50 | if (timeComponent.isCertain("timezoneOffset")) {
|
51 | dateTimeComponent.assign("timezoneOffset", timeComponent.get("timezoneOffset"));
|
52 | }
|
53 | if (timeComponent.isCertain("meridiem")) {
|
54 | dateTimeComponent.assign("meridiem", timeComponent.get("meridiem"));
|
55 | }
|
56 | else if (timeComponent.get("meridiem") != null && dateTimeComponent.get("meridiem") == null) {
|
57 | dateTimeComponent.imply("meridiem", timeComponent.get("meridiem"));
|
58 | }
|
59 | if (dateTimeComponent.get("meridiem") == Meridiem.PM && dateTimeComponent.get("hour") < 12) {
|
60 | if (timeComponent.isCertain("hour")) {
|
61 | dateTimeComponent.assign("hour", dateTimeComponent.get("hour") + 12);
|
62 | }
|
63 | else {
|
64 | dateTimeComponent.imply("hour", dateTimeComponent.get("hour") + 12);
|
65 | }
|
66 | }
|
67 | dateTimeComponent.addTags(dateComponent.tags());
|
68 | dateTimeComponent.addTags(timeComponent.tags());
|
69 | return dateTimeComponent;
|
70 | }
|
71 |
|
\ | No newline at end of file |