Skip to content

Commit e25fa10

Browse files
Wanasit TanakitrungruangWanasit Tanakitrungruang
authored andcommitted
Refactor: Remove dayjs from date/time calculation
1 parent 0561886 commit e25fa10

File tree

6 files changed

+50
-68
lines changed

6 files changed

+50
-68
lines changed

src/calculation/years.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dayjs from "dayjs";
1+
import { addDuration } from "./duration";
22

33
/**
44
* Find the most likely year, from a raw number. For example:
@@ -19,20 +19,15 @@ export function findMostLikelyADYear(yearNumber: number): number {
1919
}
2020

2121
export function findYearClosestToRef(refDate: Date, day: number, month: number): number {
22-
//Find the most appropriated year
23-
const refMoment = dayjs(refDate);
24-
let dateMoment = refMoment;
25-
dateMoment = dateMoment.month(month - 1);
26-
dateMoment = dateMoment.date(day);
27-
dateMoment = dateMoment.year(refMoment.year());
28-
29-
const nextYear = dateMoment.add(1, "y");
30-
const lastYear = dateMoment.add(-1, "y");
31-
if (Math.abs(nextYear.diff(refMoment)) < Math.abs(dateMoment.diff(refMoment))) {
32-
dateMoment = nextYear;
33-
} else if (Math.abs(lastYear.diff(refMoment)) < Math.abs(dateMoment.diff(refMoment))) {
34-
dateMoment = lastYear;
22+
let date = new Date(refDate);
23+
date.setMonth(month - 1);
24+
date.setDate(day);
25+
const nextYear = addDuration(date, { "year": 1 });
26+
const lastYear = addDuration(date, { "year": -1 });
27+
if (Math.abs(nextYear.getTime() - refDate.getTime()) < Math.abs(date.getTime() - refDate.getTime())) {
28+
date = nextYear;
29+
} else if (Math.abs(lastYear.getTime() - refDate.getTime()) < Math.abs(date.getTime() - refDate.getTime())) {
30+
date = lastYear;
3531
}
36-
37-
return dateMoment.year();
32+
return date.getFullYear();
3833
}

src/common/refiners/AbstractMergeDateRangeRefiner.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import { ParsingResult } from "../../results";
66
import { MergingRefiner } from "../abstractRefiners";
7+
import { addDuration } from "../../calculation/duration";
78

89
export default abstract class AbstractMergeDateRangeRefiner extends MergingRefiner {
910
abstract patternBetween(): RegExp;
@@ -26,31 +27,30 @@ export default abstract class AbstractMergeDateRangeRefiner extends MergingRefin
2627
}
2728
});
2829
}
30+
if (fromResult.start.date() > toResult.start.date()) {
31+
let fromDate = fromResult.start.date();
32+
let toDate = toResult.start.date();
2933

30-
if (fromResult.start.date().getTime() > toResult.start.date().getTime()) {
31-
let fromMoment = fromResult.start.dayjs();
32-
let toMoment = toResult.start.dayjs();
33-
if (toResult.start.isOnlyWeekdayComponent() && toMoment.add(7, "days").isAfter(fromMoment)) {
34-
toMoment = toMoment.add(7, "days");
35-
toResult.start.imply("day", toMoment.date());
36-
toResult.start.imply("month", toMoment.month() + 1);
37-
toResult.start.imply("year", toMoment.year());
38-
} else if (fromResult.start.isOnlyWeekdayComponent() && fromMoment.add(-7, "days").isBefore(toMoment)) {
39-
fromMoment = fromMoment.add(-7, "days");
40-
fromResult.start.imply("day", fromMoment.date());
41-
fromResult.start.imply("month", fromMoment.month() + 1);
42-
fromResult.start.imply("year", fromMoment.year());
43-
} else if (toResult.start.isDateWithUnknownYear() && toMoment.add(1, "years").isAfter(fromMoment)) {
44-
toMoment = toMoment.add(1, "years");
45-
toResult.start.imply("year", toMoment.year());
46-
} else if (fromResult.start.isDateWithUnknownYear() && fromMoment.add(-1, "years").isBefore(toMoment)) {
47-
fromMoment = fromMoment.add(-1, "years");
48-
fromResult.start.imply("year", fromMoment.year());
34+
if (toResult.start.isOnlyWeekdayComponent() && addDuration(toDate, { day: 7 }) > fromDate) {
35+
toDate = addDuration(toDate, { day: 7 });
36+
toResult.start.imply("day", toDate.getDate());
37+
toResult.start.imply("month", toDate.getMonth() + 1);
38+
toResult.start.imply("year", toDate.getFullYear());
39+
} else if (fromResult.start.isOnlyWeekdayComponent() && addDuration(fromDate, { day: -7 }) < toDate) {
40+
fromDate = addDuration(fromDate, { day: -7 });
41+
fromResult.start.imply("day", fromDate.getDate());
42+
fromResult.start.imply("month", fromDate.getMonth() + 1);
43+
fromResult.start.imply("year", fromDate.getFullYear());
44+
} else if (toResult.start.isDateWithUnknownYear() && addDuration(toDate, { year: 1 }) > fromDate) {
45+
toDate = addDuration(toDate, { year: 1 });
46+
toResult.start.imply("year", toDate.getFullYear());
47+
} else if (fromResult.start.isDateWithUnknownYear() && addDuration(fromDate, { year: -1 }) < toDate) {
48+
fromDate = addDuration(fromDate, { year: -1 });
49+
fromResult.start.imply("year", fromDate.getFullYear());
4950
} else {
5051
[toResult, fromResult] = [fromResult, toResult];
5152
}
5253
}
53-
5454
const result = fromResult.clone();
5555
result.start = fromResult.start;
5656
result.end = toResult.start;
@@ -60,7 +60,6 @@ export default abstract class AbstractMergeDateRangeRefiner extends MergingRefin
6060
} else {
6161
result.text = toResult.text + textBetween + fromResult.text;
6262
}
63-
6463
return result;
6564
}
6665
}

src/common/refiners/ForwardDateRefiner.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
import { ParsingContext, Refiner } from "../../chrono";
88
import { ParsingResult } from "../../results";
9-
import dayjs from "dayjs";
109
import * as dates from "../../utils/dates";
10+
import { implySimilarDate } from "../../utils/dates";
11+
import { addDuration } from "../../calculation/duration";
1112

1213
export default class ForwardDateRefiner implements Refiner {
1314
refine(context: ParsingContext, results: ParsingResult[]): ParsingResult[] {
@@ -16,7 +17,7 @@ export default class ForwardDateRefiner implements Refiner {
1617
}
1718

1819
results.forEach((result) => {
19-
let refMoment = dayjs(context.reference.getDateWithAdjustedTimezone());
20+
let refDate = context.reference.getDateWithAdjustedTimezone();
2021

2122
if (result.start.isOnlyTime() && context.reference.instant > result.start.date()) {
2223
const refDate = context.reference.getDateWithAdjustedTimezone();
@@ -38,41 +39,35 @@ export default class ForwardDateRefiner implements Refiner {
3839
}
3940
}
4041

41-
if (result.start.isOnlyWeekdayComponent() && refMoment.isAfter(result.start.dayjs())) {
42-
if (refMoment.day() >= result.start.get("weekday")) {
43-
refMoment = refMoment.day(result.start.get("weekday") + 7);
44-
} else {
45-
refMoment = refMoment.day(<number>result.start.get("weekday"));
42+
if (result.start.isOnlyWeekdayComponent() && refDate > result.start.date()) {
43+
let daysToAdd = result.start.get("weekday") - refDate.getDay();
44+
if (daysToAdd <= 0) {
45+
daysToAdd += 7;
4646
}
47-
48-
result.start.imply("day", refMoment.date());
49-
result.start.imply("month", refMoment.month() + 1);
50-
result.start.imply("year", refMoment.year());
47+
refDate = addDuration(refDate, { day: daysToAdd });
48+
implySimilarDate(result.start, refDate);
5149
context.debug(() => {
5250
console.log(`${this.constructor.name} adjusted ${result} weekday (${result.start})`);
5351
});
5452

5553
if (result.end && result.end.isOnlyWeekdayComponent()) {
5654
// Adjust date to the coming week
57-
if (refMoment.day() > result.end.get("weekday")) {
58-
refMoment = refMoment.day(result.end.get("weekday") + 7);
59-
} else {
60-
refMoment = refMoment.day(<number>result.end.get("weekday"));
55+
let daysToAdd = result.end.get("weekday") - refDate.getDay();
56+
if (daysToAdd <= 0) {
57+
daysToAdd += 7;
6158
}
62-
63-
result.end.imply("day", refMoment.date());
64-
result.end.imply("month", refMoment.month() + 1);
65-
result.end.imply("year", refMoment.year());
59+
refDate = addDuration(refDate, { day: daysToAdd });
60+
implySimilarDate(result.end, refDate);
6661
context.debug(() => {
6762
console.log(`${this.constructor.name} adjusted ${result} weekday (${result.end})`);
6863
});
6964
}
7065
}
7166

7267
// In case where we know the month, but not which year (e.g. "in December", "25th December"),
73-
// try move to another year
74-
if (result.start.isDateWithUnknownYear() && refMoment.isAfter(result.start.dayjs())) {
75-
for (let i = 0; i < 3 && refMoment.isAfter(result.start.dayjs()); i++) {
68+
// try move to another year (up-to 3 times)
69+
if (result.start.isDateWithUnknownYear() && refDate > result.start.date()) {
70+
for (let i = 0; i < 3 && refDate > result.start.date(); i++) {
7671
result.start.imply("year", result.start.get("year") + 1);
7772
context.debug(() => {
7873
console.log(`${this.constructor.name} adjusted ${result} year (${result.start})`);

src/locales/ja/parsers/JPStandardParser.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { Parser, ParsingContext } from "../../../chrono";
22
import { toHankaku } from "../constants";
33
import { findYearClosestToRef } from "../../../calculation/years";
44

5-
import dayjs from "dayjs";
6-
75
const PATTERN =
86
/(?:(?:([])|((||)?([0-9-]{1,4}|)))\s*)?([0-9-]{1,2})\s*([0-9-]{1,2})/i;
97
const SPECIAL_YEAR_GROUP = 1;
@@ -27,8 +25,7 @@ export default class JPStandardParser implements Parser {
2725
});
2826

2927
if (match[SPECIAL_YEAR_GROUP] && match[SPECIAL_YEAR_GROUP].match("同|今|本")) {
30-
const moment = dayjs(context.refDate);
31-
components.assign("year", moment.year());
28+
components.assign("year", context.reference.getDateWithAdjustedTimezone().getFullYear());
3229
}
3330

3431
if (match[TYPICAL_YEAR_GROUP]) {

src/timezone.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import dayjs from "dayjs";
21
import { TimezoneAbbrMap, Weekday, Month } from "./types";
32

43
export const TIMEZONE_ABBR_MAP: TimezoneAbbrMap = {
@@ -310,10 +309,7 @@ export function toTimezoneOffset(
310309
}
311310

312311
// Return DST offset if the refDate is during daylight savings
313-
if (
314-
dayjs(date).isAfter(matchedTimezone.dstStart(date.getFullYear())) &&
315-
!dayjs(date).isAfter(matchedTimezone.dstEnd(date.getFullYear()))
316-
) {
312+
if (date > matchedTimezone.dstStart(date.getFullYear()) && !(date > matchedTimezone.dstEnd(date.getFullYear()))) {
317313
return matchedTimezone.timezoneOffsetDuringDst;
318314
}
319315

src/utils/dayjs.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)