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

Create new class for DateRangePicker state internals #619

Merged
Merged
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
20 changes: 20 additions & 0 deletions packages/datetime/src/common/dateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* and https://github.com/palantir/blueprint/blob/master/PATENTS
*/

import { Months } from "./months";

export type DateRange = [Date | undefined, Date | undefined];

export function areEqual(date1: Date, date2: Date) {
Expand Down Expand Up @@ -107,3 +109,21 @@ export function getDateTime(date: Date, time: Date) {
time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
}
}

export function getDatePreviousMonth(date: Date): Date {
const monthIsJanuary = date.getMonth() === Months.JANUARY;
if (monthIsJanuary) {
return new Date(date.getFullYear() - 1, Months.DECEMBER);
} else {
return new Date(date.getFullYear(), date.getMonth() - 1);
}
}

export function getDateNextMonth(date: Date): Date {
const monthIsDecember = date.getMonth() === Months.DECEMBER;
if (monthIsDecember) {
return new Date(date.getFullYear() + 1, Months.JANUARY);
} else {
return new Date(date.getFullYear(), date.getMonth() + 1);
}
}
74 changes: 74 additions & 0 deletions packages/datetime/src/common/monthAndYear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
* Licensed under the BSD-3 License as modified (the “License”); you may obtain a copy
* of the license at https://github.com/palantir/blueprint/blob/master/LICENSE
* and https://github.com/palantir/blueprint/blob/master/PATENTS
*/

import { getDateNextMonth, getDatePreviousMonth } from "./dateUtils";

export class MonthAndYear {
private date: Date;

constructor (month?: number, year?: number) {
if (month !== null && year !== null) {
this.date = new Date(year, month);
} else {
this.date = new Date();
}
}

public clone(): MonthAndYear {
return new MonthAndYear(this.getMonth(), this.getYear());
}

public getFullDate(): Date {
return this.date;
}

public getMonth(): number {
return this.date.getMonth();
}

public getYear(): number {
return this.date.getFullYear();
}

public getPreviousMonth(): MonthAndYear {
const previousMonth = getDatePreviousMonth(this.date);
return new MonthAndYear(previousMonth.getMonth(), previousMonth.getFullYear());
}

public getNextMonth(): MonthAndYear {
const nextMonth = getDateNextMonth(this.date);
return new MonthAndYear(nextMonth.getMonth(), nextMonth.getFullYear());
}

public isBefore(displayMonth: MonthAndYear): boolean {
return compareMonthAndYear(this, displayMonth) < 0;
}

public isAfter(displayMonth: MonthAndYear): boolean {
return compareMonthAndYear(this, displayMonth) > 0;
}

public isSame(displayMonth: MonthAndYear): boolean {
return compareMonthAndYear(this, displayMonth) === 0;
}
}

// returns -ve if left < right
// returns +ve if left > right
// returns 0 if left === right
function compareMonthAndYear(firstMonthAndYear: MonthAndYear, secondMonthAndYear: MonthAndYear): number {
const firstMonth = firstMonthAndYear.getMonth();
const firstYear = firstMonthAndYear.getYear();
const secondMonth = secondMonthAndYear.getMonth();
const secondYear = secondMonthAndYear.getYear();

if (firstYear === secondYear) {
return firstMonth - secondMonth;
} else {
return firstYear - secondYear;
}
}
Loading