Skip to content

Commit

Permalink
Added more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Blackbourn committed Jul 9, 2023
1 parent 02a1f07 commit 8fcb5ca
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 199 deletions.
6 changes: 3 additions & 3 deletions docs/classes/Calendar.html

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions docs/classes/Duration.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/classes/HolidayCalendar.html

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions docs/classes/IANATimezone.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/classes/LocaleInfo.html

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions docs/classes/Timezone.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/classes/WeekendCalendar.html

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions docs/enums/BusinessDayConvention.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/interfaces/DatePartResponse.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/interfaces/DayPlurals.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/interfaces/I18nSettings.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/TimezoneOffset.html

Large diffs are not rendered by default.

232 changes: 116 additions & 116 deletions docs/modules.html

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions docs/pages/guide/calendars.html

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions manual/guide/calendars.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,78 @@ const cal = new HolidayCalendar(
tzLocal
)
```

## Custom Calendars

You can make your own calendars. The following abstract class provides a calendar
which caches the holidays for a year.

```ts
import { WeekendCalendar, Timezone, startOfDay, tzLocal } from "@jetblack/date";

export abstract class YearlyCalendar extends WeekendCalendar {

private holidays: Map<number, Map<number, string>> = new Map()

constructor (name: string, weekends: number[] = [0, 6]) {
super(name, weekends)
}

public override isHoliday(date: Date, tz: Timezone = tzLocal): boolean {
if (this.isWeekend(date, tz)) {
return true
}

const year = tz.year(date)
if (!this.holidays.has(year)) {
this.holidays.set(year, this.fetchHolidays(year, tz))
}

const yearHolidays = this.holidays.get(year)
return yearHolidays.has(startOfDay(date, tz).getTime())
}

protected abstract fetchHolidays(year: number, tz: Timezone): Map<number, string>
}
```

With the above we can create a rules based calendar.

```ts
import { Timezone, easter, addDays } from "@jetblack/date"
import { YearlyCalendar } from "./YearlyCalendar"

export class Target extends YearlyCalendar {
constructor() {
super("TARGET", [0, 6])
}

protected override fetchHolidays(year: number, tz: Timezone): Map<number, string> {
const holidays: Map<number, string> = new Map()

holidays.set(tz.makeDate(year, 0, 1).getTime(), "New Year's Day")

if (year >= 2000) {
const easterSunday = easter(year, tz)
holidays.set(addDays(easterSunday, -2, tz).getTime(), "Good Friday")
holidays.set(addDays(easterSunday, 1, tz).getTime(), "Easter Monday")
}

if (year >= 2000) {
holidays.set(tz.makeDate(year, 4, 1).getTime(), "Labor Day")
}

holidays.set(tz.makeDate(year, 11, 25).getTime(), "Christmas Day")

if (year >= 2000) {
holidays.set(tz.makeDate(year, 11, 26).getTime(), "Christmas Holiday")
}

if (year === 1998 || year === 1999 || year === 2001) {
holidays.set(tz.makeDate(year, 11, 31).getTime(), "New Year's Eve")
}

return holidays
}
}
```

0 comments on commit 8fcb5ca

Please sign in to comment.