Skip to content

Commit

Permalink
Merge pull request #614 from flaviendelangle/year-boundaries
Browse files Browse the repository at this point in the history
feat: New methods `startOfYear` and `endOfYear`
  • Loading branch information
flaviendelangle authored May 12, 2022
2 parents c83bcc8 + 21b3ee9 commit af75b41
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export interface IUtils<TDate> {

isWithinRange(value: TDate, range: [TDate, TDate]): boolean;

startOfYear(value: TDate): TDate;
endOfYear(value: TDate): TDate;
startOfMonth(value: TDate): TDate;
endOfMonth(value: TDate): TDate;
startOfWeek(value: TDate): TDate;
Expand Down
12 changes: 12 additions & 0 deletions __tests__/calculations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ describe("DateTime calculations", () => {
);
});

utilsTest("startOfYear", (date, utils, lib) => {
expect(utils.formatByString(utils.startOfYear(date), formats.dateTime[lib])).toBe(
"2018-01-01 00:00"
);
});

utilsTest("endOfYear", (date, utils, lib) => {
expect(utils.formatByString(utils.endOfYear(date), formats.dateTime[lib])).toBe(
"2018-12-31 23:59"
);
});

utilsTest("startOfMonth", (date, utils, lib) => {
expect(utils.formatByString(utils.startOfMonth(date), formats.dateTime[lib])).toBe(
"2018-10-01 00:00"
Expand Down
12 changes: 12 additions & 0 deletions __tests__/date-fns-jalali.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ describe("DateFnsJalali", () => {
);
});

it("DateFnsJalali -- startOfYear", () => {
expect(utils.formatByString(utils.startOfYear(date), dateTimeFormat)).toBe(
"1397/01/01 00:00"
);
});

it("DateFnsJalali -- endOfYear", () => {
expect(utils.formatByString(utils.endOfYear(date), dateTimeFormat)).toBe(
"1397/12/29 23:59"
);
});

it("DateFnsJalali -- startOfMonth", () => {
expect(utils.formatByString(utils.startOfMonth(date), dateTimeFormat)).toBe(
"1397/08/01 00:00"
Expand Down
16 changes: 16 additions & 0 deletions __tests__/hijri.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ describe("Hijri", () => {
expect(hijriiUtils.getWeekdays()).toEqual(["ح", "ن", "ث", "ر", "خ", "ج", "س"]);
});

it("Hijri -- endOfYear", () => {
const date = hijriiUtils.date(TEST_TIMESTAMP);

expect(hijriiUtils.endOfYear(date).toISOString()).toEqual(
"2019-08-30T23:59:59.999Z"
);
});

it("Hijri -- startOfYear", () => {
const date = hijriiUtils.date(TEST_TIMESTAMP);

expect(hijriiUtils.startOfYear(date).toISOString()).toEqual(
"2018-09-11T00:00:00.000Z"
);
});

it("Hijri -- endOfMonth", () => {
const date = hijriiUtils.date(TEST_TIMESTAMP);

Expand Down
16 changes: 16 additions & 0 deletions __tests__/jalaali.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ describe("Jalaali", () => {
expect(jalaaliUtils.getWeekdays()).toEqual(["ش", "ی", "د", "س", "چ", "پ", "ج"]);
});

it("Jalaali -- endOfYear", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);

expect(jalaaliUtils.endOfYear(date).toISOString()).toEqual(
"2019-03-20T23:59:59.999Z"
);
});

it("Jalaali -- startOfYear", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);

expect(jalaaliUtils.startOfYear(date).toISOString()).toEqual(
"2018-03-21T00:00:00.000Z"
);
});

it("Jalaali -- endOfMonth", () => {
const date = jalaaliUtils.date(TEST_TIMESTAMP);

Expand Down
12 changes: 12 additions & 0 deletions __tests__/local-date-calculations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ describe("DateTime calculations", () => {
);
});

localDateutilsTest("startOfYear", (date, utils, lib) => {
expect(utils.formatByString(utils.startOfYear(date), formats.dateTime[lib])).toBe(
"2018-01-01 00:00"
);
});

localDateutilsTest("endOfYear", (date, utils, lib) => {
expect(utils.formatByString(utils.endOfYear(date), formats.dateTime[lib])).toBe(
"2018-12-31 23:59"
);
});

localDateutilsTest("startOfMonth", (date, utils, lib) => {
expect(utils.formatByString(utils.startOfMonth(date), formats.dateTime[lib])).toBe(
"2018-10-01 00:00"
Expand Down
2 changes: 2 additions & 0 deletions packages/core/IUtils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export interface IUtils<TDate extends ExtendableDateType> {

isWithinRange(value: TDate, range: [TDate, TDate]): boolean;

startOfYear(value: TDate): TDate;
endOfYear(value: TDate): TDate;
startOfMonth(value: TDate): TDate;
endOfMonth(value: TDate): TDate;
startOfWeek(value: TDate): TDate;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/dev-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports.createRollupConfig = (typescript) => {
output: {
file: `build/index.esm.js`,
format: "esm",
exports: "auto"
exports: "auto",
},
plugins: [nodeResolve({ extensions }), typescriptPlugin({ typescript })],
},
Expand All @@ -24,7 +24,7 @@ exports.createRollupConfig = (typescript) => {
output: {
file: `build/index.js`,
format: "cjs",
exports: "auto"
exports: "auto",
},
plugins: [nodeResolve({ extensions }), typescriptPlugin({ typescript })],
},
Expand Down
8 changes: 8 additions & 0 deletions packages/date-fns-jalali/src/date-fns-jalali-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ export default class DateFnsJalaliUtils implements IUtils<Date> {
return isSameHour(value, comparing);
};

public startOfYear = (value: Date) => {
return startOfYear(value);
};

public endOfYear = (value: Date) => {
return endOfYear(value);
};

public startOfMonth = (value: Date) => {
return startOfMonth(value);
};
Expand Down
8 changes: 8 additions & 0 deletions packages/date-fns/src/date-fns-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ export default class DateFnsUtils implements IUtils<Date> {
return isSameHour(value, comparing);
};

public startOfYear = (value: Date) => {
return startOfYear(value);
};

public endOfYear = (value: Date) => {
return endOfYear(value);
};

public startOfMonth = (value: Date) => {
return startOfMonth(value);
};
Expand Down
8 changes: 8 additions & 0 deletions packages/dayjs/src/dayjs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ export default class DayjsUtils<TDate extends Dayjs = Dayjs> implements IUtils<T
return ampm === "am" ? "AM" : "PM";
};

public startOfYear = (date: Dayjs) => {
return date.startOf("year") as TDate;
};

public endOfYear = (date: Dayjs) => {
return date.endOf("year") as TDate;
};

public startOfMonth = (date: Dayjs) => {
return date.clone().startOf("month") as TDate;
};
Expand Down
8 changes: 8 additions & 0 deletions packages/hijri/src/hijri-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ export default class MomentUtils extends DefaultMomentUtils {
return date.daysInMonth();
};

public startOfYear = (date: Moment) => {
return date.clone().startOf("iYear");
};

public endOfYear = (date: Moment) => {
return date.clone().endOf("iYear");
};

public startOfMonth = (date: Moment) => {
return date.clone().startOf("iMonth");
};
Expand Down
8 changes: 8 additions & 0 deletions packages/jalaali/src/jalaali-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ export default class MomentUtils extends DefaultMomentUtils {
return date.daysInMonth();
};

public startOfYear = (date: Moment) => {
return date.clone().startOf("jYear");
};

public endOfYear = (date: Moment) => {
return date.clone().endOf("jYear");
};

public startOfMonth = (date: Moment) => {
return date.clone().startOf("jMonth");
};
Expand Down
8 changes: 8 additions & 0 deletions packages/js-joda/src/js-joda-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ export default class JsJodaUtils implements IUtils<Temporal> {
return datedate.isBefore(valuedate);
}

startOfYear(date: Temporal) {
return Year.from(date).atMonth(1).atDay(1).atStartOfDay();
}

endOfYear(date: Temporal) {
return Year.from(date).atMonth(12).atEndOfMonth().atTime(LocalTime.MAX);
}

startOfMonth(date: Temporal) {
return YearMonth.from(date).atDay(1).atStartOfDay();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/js-joda/type/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module "@date-io/type" {
import {Temporal} from "@js-joda/core";
import { Temporal } from "@js-joda/core";

export type DateType = Temporal;
}
8 changes: 8 additions & 0 deletions packages/luxon/src/luxon-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ export default class LuxonUtils implements IUtils<DateTime> {
});
};

public startOfYear = (value: DateTime) => {
return value.startOf("year");
};

public endOfYear = (value: DateTime) => {
return value.endOf("year");
};

public startOfMonth = (value: DateTime) => {
return value.startOf("month");
};
Expand Down
8 changes: 8 additions & 0 deletions packages/moment/src/moment-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ export default class MomentUtils implements IUtils<defaultMoment.Moment> {
return ampm === "am" ? "AM" : "PM"; // fallback for de, ru, ...etc
};

public startOfYear = (date: Moment) => {
return date.clone().startOf("year");
};

public endOfYear = (date: Moment) => {
return date.clone().endOf("year");
};

public startOfMonth = (date: Moment) => {
return date.clone().startOf("month");
};
Expand Down

0 comments on commit af75b41

Please sign in to comment.