Yaavi 1 year ago
parent
commit
ff8d8c49d6

+ 1 - 1
README.en.md

@@ -6,7 +6,7 @@
 
 > Translated by ChatGPT-4, PRs are welcome.
 
-This project provides a set of functions for managing and querying Chinese holidays, adjusted workdays (in lieu days), regular workdays, and the 24 solar terms. By using these functions, users can easily check the status of a specified date, get holidays or workdays within a date range, and find specific workdays. Additionally, the project supports querying the dates of the 24 solar terms, helping users understand the timing of traditional Chinese solar terms.
+This project provides a series of functions for querying Chinese holidays, compensatory holidays, working days, and 24 solar terms, as well as for the conversion between the lunar and solar calendars. By using these functions, users can easily check the status of a specified date, get holidays or workdays within a date range, and find specific workdays. Additionally, the project supports querying the dates of the 24 solar terms, helping users understand the timing of traditional Chinese solar terms.
 
 Description:
 1. Holidays: Supports from 2004 to 2024, including the extended Spring Festival in 2020.

+ 1 - 1
README.md

@@ -4,7 +4,7 @@
 ![GitHub License](https://img.shields.io/github/license/vsme/chinese-days)
 [![README](https://img.shields.io/badge/README-English-brightgreen.svg)](https://github.com/vsme/chinese-days/blob/main/README.en.md)
 
-本项目提供了一系列用于管理和查询中国节假日、调休日、工作日及二十四节气的函数。通过使用这些函数,用户可以方便地检查指定日期的状态,获取日期范围内的节假日或工作日,并查找特定的工作日。此外,项目还支持查询二十四节气的日期,帮助用户了解中国传统节气的时间安排。
+本项目提供了一系列用于查询中国节假日、调休日、工作日、24节气查询,农历阳历互转的函数。通过使用这些函数,用户可以方便地检查指定日期的状态,获取日期范围内的节假日或工作日,并查找特定的工作日。此外,项目还支持查询二十四节气的日期,帮助用户了解中国传统节气的时间安排。
 
 说明:
 1. 节假日:支持 2004年 至 2024年,包括 2020年 的春节延长。

+ 80 - 1
index.html

@@ -6,6 +6,85 @@
     <title>Test</title>
   </head>
   <body>
-    <script type="module" src="./src/solar_lunar/index.ts"></script>
+    <script type="module">
+      import { isWorkday, isHoliday, isInLieu, getDayDetail, getHolidaysInRange, getWorkdaysInRange, findWorkday, getSolarTerms, getLunarDate } from './dist/index.es.js'
+
+      console.log(isWorkday('2020-01-01'));
+      console.log(isHoliday('2020-01-01'));
+
+      // 检查 2024-05-02 返回 `true` 则表示是一个调休日。
+      console.log(isInLieu('2024-05-02')); // true
+
+      // 检查 2024-05-01 返回 `false` 则表示不是一个调休日。
+      console.log(isInLieu('2024-05-01')); // false
+
+      // 正常工作日 周五
+      console.log(getDayDetail('2024-02-02')); // { "date": "2024-02-02", "work":true,"name":"Friday"}
+      // 节假日 周末
+      console.log(getDayDetail('2024-02-03')); // { "date": "2024-02-03", "work":false,"name":"Saturday"}
+      // 调休需要上班
+      console.log(getDayDetail('2024-02-04')); // { "date": "2024-02-04", "work":true,"name":"Spring Festival,春节,3"}
+      // 节假日 春节
+      console.log(getDayDetail('2024-02-17')); // { "date": "2024-02-17", "work":false,"name":"Spring Festival,春节,3"}
+
+      const start = '2024-04-26';
+      const end = '2024-05-06';
+
+      // 获取从 2024-05-01 到 2024-05-10 的所有节假日,包括周末
+      const holidaysIncludingWeekends = getHolidaysInRange(start, end, true);
+      console.log('Holidays including weekends:', holidaysIncludingWeekends.map(d => getDayDetail(d)));
+
+      // 获取从 2024-05-01 到 2024-05-10 的节假日,不包括周末
+      const holidaysExcludingWeekends = getHolidaysInRange(start, end, false);
+      console.log('Holidays excluding weekends:', holidaysExcludingWeekends.map(d => getDayDetail(d)));
+
+
+      // 获取从 2024-05-01 到 2024-05-10 的所有工作日,包括周末
+      const workdaysIncludingWeekends = getWorkdaysInRange(start, end, true);
+      console.log('Workdays including weekends:', workdaysIncludingWeekends);
+
+      // 获取从 2024-05-01 到 2024-05-10 的工作日,不包括周末
+      const workdaysExcludingWeekends = getWorkdaysInRange(start, end, false);
+      console.log('Workdays excluding weekends:', workdaysExcludingWeekends);
+
+      const currentWorkday = findWorkday(0);
+      console.log(currentWorkday);
+
+      // 查找从今天开始未来的第一个工作日
+      const nextWorkday = findWorkday(1);
+      console.log(nextWorkday);
+
+      // 查找从今天开始之前的前一个工作日
+      const previousWorkday = findWorkday(-1);
+      console.log(previousWorkday);
+
+      // 可以传第二个参数 查找具体日期的上下工作日
+      // 查找从 2024-05-18 开始,未来的第二个工作日
+      const secondNextWorkday = findWorkday(2, '2024-05-18');
+      console.log(secondNextWorkday);
+
+
+      /** 获取范围内 节气日期数组 */
+      const solarTerms = getSolarTerms("2024-05-01", "2024-05-20");
+      solarTerms.forEach(({ date, term, name }) => {
+        console.log(`${name}: ${date}, ${term}`);
+      });
+      // 立夏: 2024-05-05, the_beginning_of_summer
+      // 小满: 2024-05-20, lesser_fullness_of_grain
+
+      // 没有节气 返回 []
+      console.log(getSolarTerms("2024-05-21", "2024-05-25"))
+      // return []
+
+      /* 不传 end 参数, 获取某天 节气 */
+      console.log(getSolarTerms("2024-05-20"));
+      // return: [{date: '2024-05-20', term: 'lesser_fullness_of_grain', name: '小满'}]
+
+      // 2097-8-7
+      console.log(getLunarDate('2097-08-07'))
+
+      // 2057-9-28
+      console.log(getLunarDate('2057-09-28'))
+    </script>
   </body>
 </html>

+ 2 - 10
package-lock.json

@@ -1,16 +1,13 @@
 {
   "name": "chinese-days",
-  "version": "0.2.0",
+  "version": "1.0.0",
   "lockfileVersion": 3,
   "requires": true,
   "packages": {
     "": {
       "name": "chinese-days",
-      "version": "0.2.0",
+      "version": "1.0.0",
       "license": "MIT",
-      "dependencies": {
-        "dayjs": "^1.11.11"
-      },
       "devDependencies": {
         "@types/jest": "^29.5.12",
         "jest": "^29.7.0",
@@ -3222,11 +3219,6 @@
         "node": ">= 8"
       }
     },
-    "node_modules/dayjs": {
-      "version": "1.11.11",
-      "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.11.tgz",
-      "integrity": "sha512-okzr3f11N6WuqYtZSvm+F776mB41wRZMhKP+hc34YdW+KmtYYK9iqvHSwo2k9FEH3fhGXvOPV6yz2IcSrfRUDg=="
-    },
     "node_modules/de-indent": {
       "version": "1.0.2",
       "resolved": "https://registry.npmjs.org/de-indent/-/de-indent-1.0.2.tgz",

+ 1 - 4
package.json

@@ -1,6 +1,6 @@
 {
   "name": "chinese-days",
-  "version": "0.2.0",
+  "version": "1.0.0",
   "description": "中国节假日、调休日、工作日、24节气查询,农历阳历互转,支持 TS、CommonJS、UMD 模块化使用。",
   "main": "dist/index.min.js",
   "module": "dist/index.es.js",
@@ -39,9 +39,6 @@
   "bugs": {
     "url": "https://github.com/vsme/chinese-days/issues"
   },
-  "dependencies": {
-    "dayjs": "^1.11.11"
-  },
   "devDependencies": {
     "@types/jest": "^29.5.12",
     "jest": "^29.7.0",

+ 27 - 11
scripts/init.ts

@@ -1,6 +1,7 @@
 import fs from "fs";
 import generate from "./generate";
 import { Holiday } from '../src/holidays/arrangement';
+import dayjs from "../src/utils/dayjs";
 
 const data = generate();
 
@@ -13,7 +14,7 @@ const mapToEnum = (obj: Record<string, string>): Record<string, any> => {
   const result: Record<string, any> = {};
   for (const [key, value] of Object.entries(obj)) {
     if (holidayMap[value]) {
-      result[key] = `Holiday.${holidayMap[value]}`;
+      result[key] = `${holidayMap[value]}`;
     } else {
       result[key] = `${value}`;
     }
@@ -26,20 +27,35 @@ const holidays = mapToEnum(data.holidays);
 const workdays = mapToEnum(data.workdays);
 const inLieuDays = mapToEnum(data.inLieuDays);
 
-const formatObjectString = (obj: Record<string, any>): string => {
-  const entries = Object.entries(obj).map(([key, value]) => `  "${key}": ${value}`);
-  return `{\n${entries.join(",\n")}\n}`;
-};
+const startDate = dayjs('2000-01-01');
+
+const getDaysSince2000 = (date: string): number => {
+  const givenDate = dayjs(date);
+  const diffDays = givenDate.diff(startDate, 'day');
+  return diffDays;
+}
+
+const compress = (dates: Record<string, Holiday>) => {
+  const compressedDates: Record<string, number[]> = {};
+  for (const [date, holidayInfo] of Object.entries(dates)) {
+    const [holidayName, ,] = holidayInfo.split(',');
+    const daysSince2000 = getDaysSince2000(date);
+    if (!compressedDates[holidayName]) {
+      compressedDates[holidayName] = [];
+    }
+    compressedDates[holidayName].push(daysSince2000);
+  }
+  return compressedDates;
+}
 
-const tsContent = `import { Holiday } from "./arrangement";
+const tsContent = `/** 自动生成的文件,请勿手动修改,数字含义是与 2000 年 1 月 1 日 相差天数 */
 
-export const holidays: Record<string, Holiday> = ${formatObjectString(holidays)};
-export const workdays: Record<string, Holiday> = ${formatObjectString(workdays)};
-export const inLieuDays: Record<string, Holiday> = ${formatObjectString(inLieuDays)};
-`;
+export const holidays: Record<string, number[]> = ${JSON.stringify(compress(holidays))};
+export const workdays: Record<string, number[]> = ${JSON.stringify(compress(workdays))};
+export const inLieuDays: Record<string, number[]> = ${JSON.stringify(compress(inLieuDays))};`
 
 // 保存到 constants.ts 文件
 fs.writeFile('./src/holidays/constants.ts', tsContent, (err) => {
   if (err) throw err;
   console.log('The file has been saved!');
-});
+});

+ 1 - 1
src/holidays/arrangement.ts

@@ -1,4 +1,4 @@
-import dayjs from "dayjs";
+import dayjs from "../utils/dayjs";
 
 export enum Holiday {
   NY = "New Year's Day,元旦,1",

File diff suppressed because it is too large
+ 1 - 844
src/holidays/constants.ts


+ 26 - 12
src/holidays/index.ts

@@ -1,9 +1,24 @@
-import dayjs, { Dayjs } from 'dayjs';
+import dayjs, { Dayjs, type ConfigType } from "../utils/dayjs";
 import { wrapDate, getDates } from '../utils'
-import { holidays, workdays, inLieuDays } from './constants';
-import Arrangement, { Holiday } from './arrangement';
+import { holidays as compressedHolidays, workdays as compressedWorkdays, inLieuDays as compressedInLieuDays } from './constants';
+import { Holiday } from './arrangement';
 
-const _validateDate = (...dates: dayjs.ConfigType[]): Dayjs | Dayjs[] => {
+const getOriginal = (dates: Record<string, number[]>) => {
+  const dateMap: Map<string, Holiday> = new Map();
+  Object.keys(dates).forEach((key) => {
+    const days = dates[key];
+    days.forEach(n => {
+      dateMap.set(dayjs('2000-01-01').add(n, 'day').format('YYYY-MM-DD'), Holiday[key as keyof typeof Holiday]);
+    })
+  });
+  return Object.fromEntries(dateMap);
+}
+
+const holidays = getOriginal(compressedHolidays);
+const workdays = getOriginal(compressedWorkdays);
+const inLieuDays = getOriginal(compressedInLieuDays);
+
+const _validateDate = (...dates: ConfigType[]): Dayjs | Dayjs[] => {
   if (dates.length !== 1) {
     return dates.map(date => _validateDate(date)) as Dayjs[];
   }
@@ -21,12 +36,12 @@ const _validateDate = (...dates: dayjs.ConfigType[]): Dayjs | Dayjs[] => {
 }
 
 /** 是否节假日 */
-const isHoliday = (date: dayjs.ConfigType): boolean => {
+const isHoliday = (date: ConfigType): boolean => {
   return !isWorkday(date);
 }
 
 /** 是否工作日 */
-const isWorkday = (date: dayjs.ConfigType): boolean => {
+const isWorkday = (date: ConfigType): boolean => {
   const validDate = _validateDate(date) as Dayjs;
   const weekday = validDate.day();
   const formattedDate = validDate.format('YYYY-MM-DD');
@@ -35,13 +50,13 @@ const isWorkday = (date: dayjs.ConfigType): boolean => {
 }
 
 /** 是否调休日 - 是节假日,但后续有需要补班 */
-const isInLieu = (date: dayjs.ConfigType): boolean => {
+const isInLieu = (date: ConfigType): boolean => {
   date = _validateDate(date) as Dayjs;
   return !!inLieuDays[date.format('YYYY-MM-DD')];
 }
 
 /** 获取工作日详情 */
-const getDayDetail = (date: dayjs.ConfigType): { work: boolean, name: string, date: string } => {
+const getDayDetail = (date: ConfigType): { work: boolean, name: string, date: string } => {
   date = _validateDate(date) as Dayjs;
   const formattedDate = date.format('YYYY-MM-DD')
   if (workdays[formattedDate]) {
@@ -67,7 +82,7 @@ const getDayDetail = (date: dayjs.ConfigType): { work: boolean, name: string, da
 }
 
 /** 获取节假日 */
-const getHolidaysInRange = (start: dayjs.ConfigType, end: dayjs.ConfigType, includeWeekends: boolean = true): string[] => {
+const getHolidaysInRange = (start: ConfigType, end: ConfigType, includeWeekends: boolean = true): string[] => {
   start = _validateDate(start) as Dayjs;
   end = _validateDate(end) as Dayjs;
   if (includeWeekends) {
@@ -77,7 +92,7 @@ const getHolidaysInRange = (start: dayjs.ConfigType, end: dayjs.ConfigType, incl
 }
 
 /** 获取工作日 */
-const getWorkdaysInRange = (start: dayjs.ConfigType, end: dayjs.ConfigType, includeWeekends: boolean = true): string[] => {
+const getWorkdaysInRange = (start: ConfigType, end: ConfigType, includeWeekends: boolean = true): string[] => {
   start = _validateDate(start) as Dayjs;
   end = _validateDate(end) as Dayjs;
   if (includeWeekends) {
@@ -87,7 +102,7 @@ const getWorkdaysInRange = (start: dayjs.ConfigType, end: dayjs.ConfigType, incl
 }
 
 /* 查找从 date 开始 第 n 个工作日, 0 为当天 */
-const findWorkday = (deltaDays: number = 0, date: dayjs.ConfigType = dayjs()): string => {
+const findWorkday = (deltaDays: number = 0, date: ConfigType = dayjs()): string => {
   date = wrapDate(date);
 
   if (deltaDays === 0) {
@@ -112,7 +127,6 @@ const findWorkday = (deltaDays: number = 0, date: dayjs.ConfigType = dayjs()): s
 
 export {
   Holiday,
-  Arrangement,
   isHoliday,
   isWorkday,
   isInLieu,

+ 4 - 4
src/solar_lunar/index.ts

@@ -1,4 +1,4 @@
-import dayjs from "dayjs";
+import dayjs, { type ConfigType } from "../utils/dayjs";
 import { type LunarDateDetail, LUNAR_INFO, CHINESE_NUMBER, NUMBER_MONTH, NUMBER_1, NUMBER_2, ZODIACS } from './constants'
 
 /**
@@ -116,7 +116,7 @@ const getYearLeapMonth = (year: number) => {
  * @param date 指定日期
  * @returns 农历信息
  */
-export const getLunarDate = (date: dayjs.ConfigType): LunarDateDetail => {
+export const getLunarDate = (date: ConfigType): LunarDateDetail => {
   const lunarDate: number[] = new Array(7).fill(0);
   let temp = 0;
   let leap = 0;
@@ -199,7 +199,7 @@ export const getLunarDate = (date: dayjs.ConfigType): LunarDateDetail => {
  * @param endDate 结束日期
  * @returns 范围内所有日期的农历信息
  */
-export const getLunarDatesInRange = (startDate: dayjs.ConfigType, endDate: dayjs.ConfigType): LunarDateDetail[] => {
+export const getLunarDatesInRange = (startDate: ConfigType, endDate: ConfigType): LunarDateDetail[] => {
   const start = dayjs(startDate);
   const end = dayjs(endDate);
   const lunarDates: LunarDateDetail[] = [];
@@ -217,7 +217,7 @@ export const getLunarDatesInRange = (startDate: dayjs.ConfigType, endDate: dayjs
  * @param isLeapMonth 是否闰月
  * @returns 阳历日期
  */
-export const getSolarDateFromLunar = (lunarDate: dayjs.ConfigType): {
+export const getSolarDateFromLunar = (lunarDate: ConfigType): {
   date: string;
   leapMonthDate?: string;
 } => {

+ 3 - 3
src/solar_terms/index.ts

@@ -1,4 +1,4 @@
-import dayjs from "dayjs";
+import dayjs, { Dayjs, type ConfigType } from "../utils/dayjs";
 import { wrapDate } from '../utils'
 import {
   SOLAR_TERMS_C_NUMS,
@@ -53,8 +53,8 @@ export interface SolarTerm {
  * @returns Array of solar terms => 节气数组
  */
 const getSolarTerms = (
-  start: dayjs.ConfigType,
-  end?: dayjs.ConfigType
+  start: ConfigType,
+  end?: ConfigType
 ): SolarTerm[] => {
   const result: SolarTerm[] = [];
   let current = wrapDate(start);

+ 184 - 0
src/utils/dayjs.ts

@@ -0,0 +1,184 @@
+/* 自己实现一个简易的 dayjs,供项目中使用 */
+export type ConfigType = string | number | Date | Dayjs | null | undefined;
+
+export class Dayjs {
+  private _date: Date;
+  private static daysOfWeek = [
+    "Sunday",
+    "Monday",
+    "Tuesday",
+    "Wednesday",
+    "Thursday",
+    "Friday",
+    "Saturday",
+  ];
+
+  constructor(date?: ConfigType) {
+    if (date instanceof Dayjs) {
+      this._date = new Date(date.toDate());
+    } else if (date instanceof Date) {
+      this._date = new Date(date);
+    } else if (typeof date === "string" || typeof date === "number") {
+      this._date = new Date(date);
+    } else {
+      this._date = new Date();
+    }
+  }
+
+  toDate(): Date {
+    return this._date;
+  }
+
+  isValid(): boolean {
+    return !isNaN(this._date.getTime());
+  }
+
+  diff(
+    date: string | number | Date | Dayjs | null | undefined,
+    unit: "day" | "month" | "year" = "day"
+  ): number {
+    const targetDate = new Dayjs(date).toDate();
+    const diffTime = this._date.getTime() - targetDate.getTime();
+    switch (unit) {
+      case "year":
+        return this._date.getFullYear() - targetDate.getFullYear();
+      case "month":
+        return (
+          (this._date.getFullYear() - targetDate.getFullYear()) * 12 +
+          (this._date.getMonth() - targetDate.getMonth())
+        );
+      case "day":
+      default:
+        return Math.floor(diffTime / (1000 * 60 * 60 * 24));
+    }
+  }
+
+  startOf(unit: "year" | "month" | "day"): Dayjs {
+    const newDate = new Date(this._date);
+    switch (unit) {
+      case "year":
+        newDate.setMonth(0);
+        newDate.setDate(1);
+        newDate.setHours(0, 0, 0, 0);
+        break;
+      case "month":
+        newDate.setDate(1);
+        newDate.setHours(0, 0, 0, 0);
+        break;
+      case "day":
+        newDate.setHours(0, 0, 0, 0);
+        break;
+    }
+    return new Dayjs(newDate);
+  }
+
+  add(value: number, unit: "year" | "month" | "day"): Dayjs {
+    const newDate = new Date(this._date);
+    switch (unit) {
+      case "year":
+        newDate.setFullYear(newDate.getFullYear() + value);
+        break;
+      case "month":
+        newDate.setMonth(newDate.getMonth() + value);
+        break;
+      case "day":
+        newDate.setDate(newDate.getDate() + value);
+        break;
+    }
+    return new Dayjs(newDate);
+  }
+
+  format(formatStr: string): string {
+    const map: { [key: string]: number | string } = {
+      YYYY: this._date.getFullYear(),
+      MM: (this._date.getMonth() + 1).toString().padStart(2, "0"),
+      DD: this._date.getDate().toString().padStart(2, "0"),
+      HH: this._date.getHours().toString().padStart(2, "0"),
+      mm: this._date.getMinutes().toString().padStart(2, "0"),
+      ss: this._date.getSeconds().toString().padStart(2, "0"),
+      dddd: Dayjs.daysOfWeek[this._date.getDay()],
+    };
+
+    return formatStr.replace(/YYYY|MM|DD|HH|mm|ss|dddd/g, (matched) => {
+      return map[matched].toString();
+    });
+  }
+
+  year(): number;
+  year(year: number): Dayjs;
+  year(year?: number): number | Dayjs {
+    if (year === undefined) return this._date.getFullYear();
+    const newDate = new Date(this._date);
+    newDate.setFullYear(year);
+    return new Dayjs(newDate);
+  }
+
+  month(): number;
+  month(month: number): Dayjs;
+  month(month?: number): number | Dayjs {
+    if (month === undefined) return this._date.getMonth();
+    const newDate = new Date(this._date);
+    newDate.setMonth(month);
+    return new Dayjs(newDate);
+  }
+
+  date(): number;
+  date(day: number): Dayjs;
+  date(day?: number): number | Dayjs {
+    if (day === undefined) return this._date.getDate();
+    const newDate = new Date(this._date);
+    newDate.setDate(day);
+    return new Dayjs(newDate);
+  }
+
+  day(): number;
+  day(day: number): Dayjs;
+  day(day?: number): number | Dayjs {
+    if (day === undefined) {
+      return this._date.getDay();
+    } else {
+      const currentDay = this._date.getDay();
+      const diff = day - currentDay;
+      const newDate = new Date(this._date);
+      newDate.setDate(this._date.getDate() + diff);
+      return new Dayjs(newDate);
+    }
+  }
+
+  isBefore(date: string | number | Date | Dayjs | null | undefined): boolean {
+    const targetDate = new Dayjs(date).toDate();
+    return this._date.getTime() < targetDate.getTime();
+  }
+
+  isAfter(date: string | number | Date | Dayjs | null | undefined): boolean {
+    const targetDate = new Dayjs(date).toDate();
+    return this._date.getTime() > targetDate.getTime();
+  }
+
+  isSame(
+    date: string | number | Date | Dayjs | null | undefined,
+    unit: "year" | "month" | "day" = "day"
+  ): boolean {
+    const targetDate = new Dayjs(date).toDate();
+    switch (unit) {
+      case "year":
+        return this._date.getFullYear() === targetDate.getFullYear();
+      case "month":
+        return (
+          this._date.getFullYear() === targetDate.getFullYear() &&
+          this._date.getMonth() === targetDate.getMonth()
+        );
+      case "day":
+      default:
+        return (
+          this._date.getFullYear() === targetDate.getFullYear() &&
+          this._date.getMonth() === targetDate.getMonth() &&
+          this._date.getDate() === targetDate.getDate()
+        );
+    }
+  }
+}
+
+const simpleDayjs = (date?: ConfigType): Dayjs => new Dayjs(date);
+
+export default simpleDayjs;

+ 3 - 3
src/utils/index.ts

@@ -1,12 +1,12 @@
-import dayjs, { type Dayjs } from "dayjs";
+import dayjs, { type Dayjs, type ConfigType } from "../utils/dayjs";
 
 // wrapDate to the start of the day
-export const wrapDate = (date: dayjs.ConfigType): dayjs.Dayjs => {
+export const wrapDate = (date: ConfigType): Dayjs => {
   return dayjs(date).startOf("day");
 };
 
 
-export const getDates = (start: dayjs.ConfigType, end: dayjs.ConfigType): Dayjs[] => {
+export const getDates = (start: ConfigType, end: ConfigType): Dayjs[] => {
   start = wrapDate(start);
   end = wrapDate(end);
   const deltaDays = end.diff(start, 'day');

+ 2 - 2
test/holidays/arrangement.test.ts

@@ -1,5 +1,5 @@
-import dayjs from 'dayjs';
-import { Arrangement, Holiday } from '../../src';
+import dayjs from '../../src/utils/dayjs';
+import Arrangement, { Holiday} from '../../src/holidays/arrangement';
 
 describe('Arrangement class', () => {
   let arrangement: Arrangement;

+ 1 - 3
test/holidays/index.test.ts

@@ -1,5 +1,3 @@
-import dayjs from 'dayjs';
-import { holidays, workdays, inLieuDays } from '../../src/holidays/constants';
 import Arrangement, { Holiday } from '../../src/holidays/arrangement';
 import {
   isHoliday,
@@ -43,7 +41,7 @@ describe('Holiday Functions', () => {
     expect(detail).toEqual({
       date: '2024-05-01',
       work: false,
-      name: holidays['2024-05-01'],
+      name: Holiday.L,
     });
   });
 

+ 0 - 0
test/index.ts


+ 1 - 1
test/solar_terms/index.test.ts

@@ -1,4 +1,4 @@
-import dayjs from "dayjs";
+import dayjs from "../../src/utils/dayjs";
 import {
   getSolarTermDate,
   getSolarTerms,

+ 93 - 0
test/utils/dayjs.test.ts

@@ -0,0 +1,93 @@
+import simpleDayjs from "../../src/utils/dayjs";
+// import simpleDayjs from "dayjs";
+
+describe("SimpleDayjs", () => {
+  it("should return true for a valid date", () => {
+    const date = simpleDayjs("2024-02-10");
+    expect(date.isValid()).toBe(true);
+  });
+
+  it("should return false for an invalid date", () => {
+    const date = simpleDayjs("invalid-date");
+    expect(date.isValid()).toBe(false);
+  });
+
+  it("should calculate the difference in days", () => {
+    const date1 = simpleDayjs("2024-02-10");
+    const date2 = simpleDayjs("2000-01-01");
+    expect(date1.diff(date2, "day")).toBe(8806);
+  });
+
+  it("should format the start of the year", () => {
+    const date = simpleDayjs("2024-02-10");
+    expect(date.startOf("year").format("YYYY-MM-DD")).toBe("2024-01-01");
+  });
+
+  it("should add one month to the date", () => {
+    const date = simpleDayjs("2024-02-10");
+    expect(date.add(1, "month").format("YYYY-MM-DD")).toBe("2024-03-10");
+  });
+
+  it("should get and set the year", () => {
+    const date = simpleDayjs("2024-02-10");
+    expect(date.year()).toBe(2024);
+    expect(date.year(2025).format("YYYY-MM-DD")).toBe("2025-02-10");
+  });
+
+  it("should get and set the month", () => {
+    const date = simpleDayjs("2024-02-10");
+    expect(date.month()).toBe(1); // 注意月份是从0开始的
+    expect(date.month(5).format("YYYY-MM-DD")).toBe("2024-06-10");
+  });
+
+  it("should get and set the date", () => {
+    const date = simpleDayjs("2024-02-10");
+    expect(date.date()).toBe(10);
+    expect(date.date(15).format("YYYY-MM-DD")).toBe("2024-02-15");
+  });
+
+  it("should return the day of the week", () => {
+    const date = simpleDayjs("2024-02-10");
+    expect(date.day()).toBe(6); // 6 表示星期六
+  });
+
+  it("should set the day of the week and return the new date", () => {
+    const date = simpleDayjs("2024-02-10");
+    expect(date.day(1).format("YYYY-MM-DD")).toBe("2024-02-05"); // 将日期调整到下一个星期一
+  });
+
+  it("should format the date with day of the week", () => {
+    const date = simpleDayjs("2024-02-10");
+    expect(date.format("dddd, YYYY-MM-DD")).toBe("Saturday, 2024-02-10");
+  });
+
+  it("should check if a date is before another date", () => {
+    const date1 = simpleDayjs("2024-02-10");
+    const date2 = simpleDayjs("2023-01-01");
+    expect(date1.isBefore(date2)).toBe(false);
+  });
+
+  it("should check if a date is after another date", () => {
+    const date1 = simpleDayjs("2024-02-10");
+    const date2 = simpleDayjs("2023-01-01");
+    expect(date1.isAfter(date2)).toBe(true);
+  });
+
+  it("should check if two dates are the same (day)", () => {
+    const date1 = simpleDayjs("2024-02-10");
+    const date2 = simpleDayjs("2024-02-10");
+    expect(date1.isSame(date2, "day")).toBe(true);
+  });
+
+  it("should check if two dates are the same (month)", () => {
+    const date1 = simpleDayjs("2024-02-10");
+    const date2 = simpleDayjs("2024-02-01");
+    expect(date1.isSame(date2, "month")).toBe(true);
+  });
+
+  it("should check if two dates are the same (year)", () => {
+    const date1 = simpleDayjs("2024-02-10");
+    const date2 = simpleDayjs("2024-01-01");
+    expect(date1.isSame(date2, "year")).toBe(true);
+  });
+});

Some files were not shown because too many files changed in this diff