1 | import { ParsingComponents } from "../results.js";
|
2 | import dayjs from "dayjs";
|
3 | import { assignSimilarDate, assignSimilarTime, implySimilarTime, implyTheNextDay, } from "../utils/dayjs.js";
|
4 | import { Meridiem } from "../types.js";
|
5 | export function now(reference) {
|
6 | const targetDate = dayjs(reference.instant);
|
7 | const component = new ParsingComponents(reference, {});
|
8 | assignSimilarDate(component, targetDate);
|
9 | assignSimilarTime(component, targetDate);
|
10 | if (reference.timezoneOffset !== null) {
|
11 | component.assign("timezoneOffset", targetDate.utcOffset());
|
12 | }
|
13 | component.addTag("casualReference/now");
|
14 | return component;
|
15 | }
|
16 | export function today(reference) {
|
17 | const targetDate = dayjs(reference.instant);
|
18 | const component = new ParsingComponents(reference, {});
|
19 | assignSimilarDate(component, targetDate);
|
20 | implySimilarTime(component, targetDate);
|
21 | component.addTag("casualReference/today");
|
22 | return component;
|
23 | }
|
24 | export function yesterday(reference) {
|
25 | return theDayBefore(reference, 1).addTag("casualReference/yesterday");
|
26 | }
|
27 | export function theDayBefore(reference, numDay) {
|
28 | return theDayAfter(reference, -numDay);
|
29 | }
|
30 | export function tomorrow(reference) {
|
31 | return theDayAfter(reference, 1).addTag("casualReference/tomorrow");
|
32 | }
|
33 | export function theDayAfter(reference, nDays) {
|
34 | let targetDate = dayjs(reference.instant);
|
35 | const component = new ParsingComponents(reference, {});
|
36 | targetDate = targetDate.add(nDays, "day");
|
37 | assignSimilarDate(component, targetDate);
|
38 | implySimilarTime(component, targetDate);
|
39 | return component;
|
40 | }
|
41 | export function tonight(reference, implyHour = 22) {
|
42 | const targetDate = dayjs(reference.instant);
|
43 | const component = new ParsingComponents(reference, {});
|
44 | assignSimilarDate(component, targetDate);
|
45 | component.imply("hour", implyHour);
|
46 | component.imply("meridiem", Meridiem.PM);
|
47 | component.addTag("casualReference/tonight");
|
48 | return component;
|
49 | }
|
50 | export function lastNight(reference, implyHour = 0) {
|
51 | let targetDate = dayjs(reference.instant);
|
52 | const component = new ParsingComponents(reference, {});
|
53 | if (targetDate.hour() < 6) {
|
54 | targetDate = targetDate.add(-1, "day");
|
55 | }
|
56 | assignSimilarDate(component, targetDate);
|
57 | component.imply("hour", implyHour);
|
58 | return component;
|
59 | }
|
60 | export function evening(reference, implyHour = 20) {
|
61 | const component = new ParsingComponents(reference, {});
|
62 | component.imply("meridiem", Meridiem.PM);
|
63 | component.imply("hour", implyHour);
|
64 | component.addTag("casualReference/evening");
|
65 | return component;
|
66 | }
|
67 | export function yesterdayEvening(reference, implyHour = 20) {
|
68 | let targetDate = dayjs(reference.instant);
|
69 | const component = new ParsingComponents(reference, {});
|
70 | targetDate = targetDate.add(-1, "day");
|
71 | assignSimilarDate(component, targetDate);
|
72 | component.imply("hour", implyHour);
|
73 | component.imply("meridiem", Meridiem.PM);
|
74 | component.addTag("casualReference/yesterday");
|
75 | component.addTag("casualReference/evening");
|
76 | return component;
|
77 | }
|
78 | export function midnight(reference) {
|
79 | const component = new ParsingComponents(reference, {});
|
80 | const targetDate = dayjs(reference.instant);
|
81 | if (targetDate.hour() > 2) {
|
82 | implyTheNextDay(component, targetDate);
|
83 | }
|
84 | component.assign("hour", 0);
|
85 | component.imply("minute", 0);
|
86 | component.imply("second", 0);
|
87 | component.imply("millisecond", 0);
|
88 | component.addTag("casualReference/midnight");
|
89 | return component;
|
90 | }
|
91 | export function morning(reference, implyHour = 6) {
|
92 | const component = new ParsingComponents(reference, {});
|
93 | component.imply("meridiem", Meridiem.AM);
|
94 | component.imply("hour", implyHour);
|
95 | component.imply("minute", 0);
|
96 | component.imply("second", 0);
|
97 | component.imply("millisecond", 0);
|
98 | component.addTag("casualReference/morning");
|
99 | return component;
|
100 | }
|
101 | export function afternoon(reference, implyHour = 15) {
|
102 | const component = new ParsingComponents(reference, {});
|
103 | component.imply("meridiem", Meridiem.PM);
|
104 | component.imply("hour", implyHour);
|
105 | component.imply("minute", 0);
|
106 | component.imply("second", 0);
|
107 | component.imply("millisecond", 0);
|
108 | component.addTag("casualReference/afternoon");
|
109 | return component;
|
110 | }
|
111 | export function noon(reference) {
|
112 | const component = new ParsingComponents(reference, {});
|
113 | component.imply("meridiem", Meridiem.AM);
|
114 | component.imply("hour", 12);
|
115 | component.imply("minute", 0);
|
116 | component.imply("second", 0);
|
117 | component.imply("millisecond", 0);
|
118 | component.addTag("casualReference/noon");
|
119 | return component;
|
120 | }
|
121 |
|
\ | No newline at end of file |