Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sc93486] [Angular Calendar] Migrate date-fns to dayjs #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ng add angular-calendar
First install through npm:

```bash
npm install --save angular-calendar date-fns
npm install --save angular-calendar dayjs
```

Next include the CSS file in the global (not component scoped) styles of your app:
Expand Down
2 changes: 1 addition & 1 deletion build-tool-examples/angular-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@nguniversal/module-map-ngfactory-loader": "v8.2.6",
"angular-calendar": "^0.28.5",
"core-js": "^3.4.7",
"date-fns": "^2.8.1",
"dayjs": "1.11.6",
"express": "^4.15.2",
"rxjs": "^6.5.3",
"tslib": "^1.10.0",
Expand Down
1 change: 0 additions & 1 deletion build-tool-examples/angular-cli/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CalendarModule, DateAdapter } from 'angular-calendar';
import { adapterFactory } from 'angular-calendar/date-adapters/date-fns';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { AppComponent } from './app.component';
Expand Down
2 changes: 1 addition & 1 deletion build-tool-examples/rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@angular/platform-browser-dynamic": "^8.2.14",
"angular-calendar": "^0.28.2",
"core-js": "^2.6.10",
"date-fns": "^2.8.1",
"dayjs": "1.11.6",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.5.3",
"zone.js": "^0.9.1"
Expand Down
2 changes: 1 addition & 1 deletion build-tool-examples/systemjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"angular-calendar": "^0.28.2",
"angular-in-memory-web-api": "^0.9.0",
"core-js": "^2.6.10",
"date-fns": "^2.8.1",
"dayjs": "1.11.6",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.5.3",
"systemjs": "^0.19.39",
Expand Down
33 changes: 13 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build:clean": "del-cli dist",
"build:package": "copyfiles package.json projects/angular-calendar && ng-packagr -p projects/angular-calendar/ng-package.json && npm run build:styles",
"build:schematics": "tsc projects/angular-calendar/schematics/ng-add/index.ts",
"build:copyfiles": "copyfiles CHANGELOG.md README.md LICENSE dist/angular-calendar & cp projects/angular-calendar/util/date-adapter-package.json dist/angular-calendar/date-adapters/date-fns/package.json & cp projects/angular-calendar/util/date-adapter-package.json dist/angular-calendar/date-adapters/moment/package.json & copyfiles -u 3 projects/angular-calendar/src/**/*.scss dist/angular-calendar/scss & npx copyfiles -u 2 \"projects/angular-calendar/schematics/**/*.{json,js}\" dist/angular-calendar",
"build:copyfiles": "copyfiles CHANGELOG.md README.md LICENSE dist/angular-calendar & cp projects/angular-calendar/util/date-adapter-package.json dist/angular-calendar/date-adapters/moment/package.json & copyfiles -u 3 projects/angular-calendar/src/**/*.scss dist/angular-calendar/scss & npx copyfiles -u 2 \"projects/angular-calendar/schematics/**/*.{json,js}\" dist/angular-calendar",
"build": "run-s build:copy-package-json build:lib build:date-adapters build:styles build:copyfiles",
"test:single": "cross-env TZ=UTC ng test angular-calendar --watch=false --code-coverage",
"test:watch": "cross-env TZ=UTC ng test angular-calendar",
Expand Down Expand Up @@ -111,7 +111,7 @@
"copyfiles": "^2.4.1",
"core-js": "^3.25.2",
"css-loader": "^3.5.3",
"date-fns": "^2.29.3",
"dayjs": "1.11.6",
"flatpickr": "^4.6.3",
"highlightjs-loader": "^0.2.3",
"husky": "^4.2.5",
Expand Down
38 changes: 0 additions & 38 deletions projects/angular-calendar/src/date-adapters/date-fns/index.ts

This file was deleted.

136 changes: 136 additions & 0 deletions projects/angular-calendar/src/date-adapters/dayjs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { DateAdapter } from '../date-adapter';
import * as dayjs from 'dayjs';
import isoWeek from 'dayjs/plugin/isoWeek';
import minMax from 'dayjs/plugin/minMax';

dayjs.extend(minMax);
dayjs.extend(isoWeek);

export function adapterFactory(): DateAdapter {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is I think the perfect file for heavy unit tests

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

return {
endOfDay(date: Date | number): Date {
return dayjs(date).endOf('day').toDate();
},
endOfMonth(date: Date | number): Date {
return dayjs(date).endOf('month').toDate();
},
endOfWeek(date: Date | number, options?: { weekStartsOn?: number }): Date {
return dayjs(date).endOf('week').toDate();
},
addDays(date: Date | number, amount: number): Date {
return dayjs(date).add(amount, 'day').toDate();
},
addHours(date: Date | number, amount: number): Date {
return dayjs(date).add(amount, 'hour').toDate();
},
addMinutes(date: Date | number, amount: number): Date {
return dayjs(date).add(amount, 'minute').toDate();
},
addMonths(date: Date | number, amount: number): Date {
return dayjs(date).add(amount, 'month').toDate();
},
addSeconds(date: Date | number, amount: number): Date {
return dayjs(date).add(amount, 'second').toDate();
},
addWeeks(date: Date | number, amount: number): Date {
return dayjs(date).add(amount, 'week').toDate();
},
differenceInDays(
dateLeft: Date | number,
dateRight: Date | number
): number {
return dayjs(dateLeft).diff(dayjs(dateRight, 'day'));
},
differenceInSeconds(
dateLeft: Date | number,
dateRight: Date | number
): number {
return dayjs(dateLeft).diff(dayjs(dateRight, 'second'));
},
differenceInMinutes(
dateLeft: Date | number,
dateRight: Date | number
): number {
return dayjs(dateLeft).diff(dayjs(dateRight, 'minute'));
},
getDate(date: Date | number): number {
return dayjs(date).date();
},
getDay(date: Date | number): number {
return dayjs(date).day();
},
getISOWeek(date: Date | number): number {
return dayjs(date).isoWeek();
},
getMonth(date: Date | number): number {
return dayjs(date).month();
},
getYear(date: Date | number): number {
return dayjs(date).year();
},
isSameDay(dateLeft: Date | number, dateRight: Date | number): boolean {
return dayjs(dateLeft).isSame(dayjs(dateRight), 'day');
},
isSameMonth(dateLeft: Date | number, dateRight: Date | number): boolean {
return dayjs(dateLeft).isSame(dayjs(dateRight), 'month');
},
isSameSecond(dateLeft: Date | number, dateRight: Date | number): boolean {
return dayjs(dateLeft).isSame(dayjs(dateRight), 'second');
},
setDate(date: Date | number, dayOfMonth: number): Date {
return dayjs(date).set('date', dayOfMonth).toDate();
},
setMonth(date: Date | number, month: number): Date {
return dayjs(date).set('month', month).toDate();
},
setYear(date: Date | number, year: number): Date {
return dayjs(date).set('year', year).toDate();
},
subDays(date: Date | number, amount: number): Date {
return dayjs(date).subtract(amount, 'day').toDate();
},
subMonths(date: Date | number, amount: number): Date {
return dayjs(date).subtract(amount, 'month').toDate();
},
subWeeks(date: Date | number, amount: number): Date {
return dayjs(date).subtract(amount, 'week').toDate();
},
getHours(date: Date | number): number {
return dayjs(date).hour();
},
getMinutes(date: Date | number): number {
return dayjs(date).minute();
},
max(dates: (Date | number)[]): Date {
const dayjsDates = dates.map((date) => dayjs(date));
return dayjs.max(...dayjsDates).toDate();
},
setHours(date: Date | number, hours: number): Date {
return dayjs(date).set('hour', hours).toDate();
},
setMinutes(date: Date | number, minutes: number): Date {
return dayjs(date).set('minute', minutes).toDate();
},
startOfDay(date: Date | number): Date {
return dayjs(date).startOf('day').toDate();
},
startOfMinute(date: Date | number): Date {
return dayjs(date).startOf('minute').toDate();
},
startOfMonth(date: Date | number): Date {
return dayjs(date).startOf('month').toDate();
},
startOfWeek(
date: Date | number,
options?: { weekStartsOn?: number }
): Date {
return dayjs(date).startOf('week').toDate();
},
reverseTz(date: Date | number): Date {
return new Date(date);
},
withTimezone(timezone: string): DateAdapter {
return this;
},
};
}
2 changes: 1 addition & 1 deletion projects/angular-calendar/src/date-adapters/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { DateAdapter } from './date-adapter';
export * from './date-fns';
export * from './dayjs';
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as moment from 'moment';
import * as MomentTz from 'moment-timezone';
import isAfterFns from 'date-fns/esm/isAfter';
import isBeforeFns from 'date-fns/esm/isBefore';
import { Injectable } from '@angular/core';
import { TimezoneInfo } from './timezone-info.model';
import dayjs from 'dayjs';

@Injectable({
providedIn: 'root',
Expand Down Expand Up @@ -89,19 +88,19 @@ export class TimezoneManager {
let offset: number = null;
let offsetInDST: number = null;

const startOfYear = MomentTz.tz({ year: year }, timezone);
const startOfYear = MomentTz.tz({ year }, timezone);
const endOfYear = startOfYear.clone().endOf('year');
const monthCount = endOfYear.diff(startOfYear, 'months') + 1;

// Iterate through all the months to find the ones containing the DST switch
let previousOffset = startOfYear.clone().subtract(1, 's').utcOffset();
for (let month = 0; month < monthCount; month++) {
const startOfMonth = MomentTz.tz({ year: year, month: month }, timezone);
const startOfMonth = MomentTz.tz({ year, month }, timezone);
const endOfMonth = startOfMonth.clone().endOf('month');
let offsetDiff =
startOfMonth.utcOffset() - endOfMonth.utcOffset() ||
previousOffset - startOfMonth.utcOffset();
if (offsetDiff != 0) {
if (offsetDiff !== 0) {
// DST switch occurs during this month
const dayCount = endOfMonth.clone().diff(startOfMonth, 'days') + 1;

Expand All @@ -113,13 +112,13 @@ export class TimezoneManager {
offsetDiff =
startOfDay.utcOffset() - endOfDay.utcOffset() ||
previousOffset - startOfDay.utcOffset();
if (offsetDiff != 0) {
if (offsetDiff !== 0) {
// a DST switch occurs during this hour
const hourCount = endOfDay.diff(startOfDay, 'hours') + 1;

// Iterate through all the hours to find the DST switch
for (let hour = 0; hour < hourCount; hour++) {
let startOfHour = startOfDay.clone().hour(hour);
const startOfHour = startOfDay.clone().hour(hour);
offsetDiff = previousOffset - startOfHour.utcOffset();

if (offsetDiff < 0) {
Expand Down Expand Up @@ -158,17 +157,20 @@ export class TimezoneManager {
startOfDST: startOfDST ? startOfDST.toDate() : null,
endOfDST: endOfDST ? endOfDST.toDate() : null,
offset: offset || startOfYear.utcOffset(),
offsetInDST: offsetInDST,
offsetInDST,
};
}

private isInDST(date: Date, startOfDST: Date, endOfDST: Date): boolean {
if (startOfDST && endOfDST) {
return isAfterFns(date, startOfDST) && isBeforeFns(date, endOfDST);
return (
dayjs(date).isAfter(dayjs(startOfDST)) &&
dayjs(date).isBefore(dayjs(endOfDST))
);
} else if (startOfDST && !endOfDST) {
return isAfterFns(date, startOfDST);
return dayjs(date).isAfter(dayjs(startOfDST));
} else if (!startOfDST && endOfDST) {
return isBeforeFns(date, endOfDST);
return dayjs(date).isBefore(dayjs(endOfDST));
}
return false;
}
Expand Down
1 change: 0 additions & 1 deletion tsconfig-date-adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"target": "es5"
},
"files": [
"projects/angular-calendar/src/date-adapters/date-fns/index.ts",
"projects/angular-calendar/src/date-adapters/moment/index.ts"
]
}
Loading