-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Rob Blackbourn <rob.blackbourn@gmail.com>
- Loading branch information
1 parent
58342c7
commit 6756e92
Showing
14 changed files
with
234 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { tzLocal } from './LocalTimezone' | ||
import { Timezone } from './Timezone' | ||
import { daysInMonth } from './daysInMonth' | ||
import { quarterOfYear } from './quarterOfYear' | ||
|
||
/** | ||
* Find the last moment of the quarter. | ||
* | ||
* @category Anchors | ||
* | ||
* @param date A date | ||
* @param tz An optional timezone. Defaults to the local timezone. | ||
* @returns The last moment of the quarter. | ||
*/ | ||
export function endOfQuarter(date: Date, tz: Timezone = tzLocal): Date { | ||
const quarter = quarterOfYear(date, tz) | ||
const monthIndex = 3 * (quarter - 1) + 2 | ||
const year = tz.year(date) | ||
const day = daysInMonth(year, monthIndex) | ||
return tz.makeDate(year, monthIndex, day, 23, 59, 59, 999) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { isDateEqual } from './isDateEqual' | ||
import { lastDayOfQuarter } from './lastDayOfQuarter' | ||
import { tzLocal } from './LocalTimezone' | ||
import { Timezone } from './Timezone' | ||
|
||
/** | ||
* Check if the date is the last day of the quarter. | ||
* | ||
* @category Calendars | ||
* | ||
* @param date The date to check. | ||
* @param tz An optional timezone. Defaults to the local timezone. | ||
* @returns True if the date is the last day of the quarter. | ||
*/ | ||
export function isLastDayOfQuarter( | ||
date: Date, | ||
tz: Timezone = tzLocal | ||
): boolean { | ||
const end = lastDayOfQuarter(date, tz) | ||
return isDateEqual(date, end) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { tzLocal } from './LocalTimezone' | ||
import { Timezone } from './Timezone' | ||
import { daysInMonth } from './daysInMonth' | ||
import { quarterOfYear } from './quarterOfYear' | ||
|
||
/** | ||
* Find the last day of the quarter. | ||
* | ||
* @category Anchors | ||
* | ||
* @param date The start date. | ||
* @param tz An optional timezone. Defaults to the local timezone. | ||
* @returns A date which is the last day of the quarter. | ||
*/ | ||
export function lastDayOfQuarter(date: Date, tz: Timezone = tzLocal): Date { | ||
const quarter = quarterOfYear(date, tz) | ||
const monthIndex = 3 * (quarter - 1) + 2 | ||
const year = tz.year(date) | ||
const day = daysInMonth(year, monthIndex) | ||
return tz.makeDate(year, monthIndex, day) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { tzLocal } from './LocalTimezone' | ||
import { Timezone } from './Timezone' | ||
import { quarterOfYear } from './quarterOfYear' | ||
|
||
/** | ||
* Find the start of the quarter for a given date. | ||
* | ||
* @category Anchors | ||
* | ||
* @param date The date. | ||
* @param tz An optional timezone. Defaults to the local timezone. | ||
* @returns The start of the quarter. | ||
*/ | ||
export function startOfQuarter(date: Date, tz: Timezone = tzLocal): Date { | ||
const quarter = quarterOfYear(date, tz) | ||
const monthIndex = 3 * (quarter - 1) | ||
return tz.makeDate(tz.year(date), monthIndex, 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
IANATimezone, | ||
dataToTimezoneOffset, | ||
endOfQuarter, | ||
tzLocal, | ||
tzUtc | ||
} from '../src' | ||
import chicagoTzData from '@jetblack/tzdata/dist/latest/America/Chicago.json' | ||
import tokyoTzData from '@jetblack/tzdata/dist/latest/Asia/Tokyo.json' | ||
|
||
describe('endOfQuarter', () => { | ||
const tzChicago = new IANATimezone( | ||
'America/Chicago', | ||
chicagoTzData.map(dataToTimezoneOffset) | ||
) | ||
const tzTokyo = new IANATimezone( | ||
'Asia/Tokyo', | ||
tokyoTzData.map(dataToTimezoneOffset) | ||
) | ||
|
||
for (const tz of [tzUtc, tzLocal, tzChicago, tzTokyo]) { | ||
describe(tz.name, () => { | ||
it('should find the last moment of the quarter', () => { | ||
const date = tz.makeDate(2000, 0, 1) | ||
const actual = endOfQuarter(date, tz) | ||
const expected = tz.makeDate(2000, 2, 31, 23, 59, 59, 999) | ||
expect(tz.toISOString(actual)).toBe(tz.toISOString(expected)) | ||
}) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
IANATimezone, | ||
dataToTimezoneOffset, | ||
isLastDayOfQuarter, | ||
tzLocal, | ||
tzUtc | ||
} from '../src' | ||
import chicagoTzData from '@jetblack/tzdata/dist/latest/America/Chicago.json' | ||
import tokyoTzData from '@jetblack/tzdata/dist/latest/Asia/Tokyo.json' | ||
|
||
describe('isLastDayOfQuarter', () => { | ||
const tzChicago = new IANATimezone( | ||
'America/Chicago', | ||
chicagoTzData.map(dataToTimezoneOffset) | ||
) | ||
const tzTokyo = new IANATimezone( | ||
'Asia/Tokyo', | ||
tokyoTzData.map(dataToTimezoneOffset) | ||
) | ||
|
||
for (const tz of [tzUtc, tzLocal, tzChicago, tzTokyo]) { | ||
describe(tz.name, () => { | ||
it('should know 30 January is not the end of the quarter', () => { | ||
const date = tz.makeDate(2008, 0, 30) | ||
expect(isLastDayOfQuarter(date, tz)).toBeFalsy() | ||
}) | ||
|
||
it('should know 31 March is the end of the month', () => { | ||
const date = tz.makeDate(2008, 2, 31) | ||
expect(isLastDayOfQuarter(date, tz)).toBeTruthy() | ||
}) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { | ||
IANATimezone, | ||
dataToTimezoneOffset, | ||
lastDayOfQuarter, | ||
tzLocal, | ||
tzUtc | ||
} from '../src' | ||
import chicagoTzData from '@jetblack/tzdata/dist/latest/America/Chicago.json' | ||
import tokyoTzData from '@jetblack/tzdata/dist/latest/Asia/Tokyo.json' | ||
|
||
describe('lastDayOfQuarter', () => { | ||
const tzChicago = new IANATimezone( | ||
'America/Chicago', | ||
chicagoTzData.map(dataToTimezoneOffset) | ||
) | ||
const tzTokyo = new IANATimezone( | ||
'Asia/Tokyo', | ||
tokyoTzData.map(dataToTimezoneOffset) | ||
) | ||
|
||
for (const tz of [tzUtc, tzLocal, tzChicago, tzTokyo]) { | ||
describe(tz.name, () => { | ||
it('should find the last day of the quarter', () => { | ||
const actual = lastDayOfQuarter(tz.makeDate(2000, 0, 1), tz) | ||
const expected = tz.makeDate(2000, 2, 31) | ||
expect(tz.toISOString(actual)).toBe(tz.toISOString(expected)) | ||
}) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
IANATimezone, | ||
dataToTimezoneOffset, | ||
startOfQuarter, | ||
tzLocal, | ||
tzUtc | ||
} from '../src' | ||
import chicagoTzData from '@jetblack/tzdata/dist/latest/America/Chicago.json' | ||
import tokyoTzData from '@jetblack/tzdata/dist/latest/Asia/Tokyo.json' | ||
|
||
describe('startOfQuarter', () => { | ||
const tzChicago = new IANATimezone( | ||
'America/Chicago', | ||
chicagoTzData.map(dataToTimezoneOffset) | ||
) | ||
const tzTokyo = new IANATimezone( | ||
'Asia/Tokyo', | ||
tokyoTzData.map(dataToTimezoneOffset) | ||
) | ||
|
||
for (const tz of [tzUtc, tzLocal, tzChicago, tzTokyo]) { | ||
describe(tz.name, () => { | ||
it('should return the start of the quarters', () => { | ||
const date = tz.makeDate(2014, 8, 2, 2, 11, 55, 664) | ||
const actual = startOfQuarter(date, tz) | ||
const expected = tz.makeDate(2014, 6, 1) | ||
expect(tz.toISOString(actual)).toBe(tz.toISOString(expected)) | ||
}) | ||
|
||
it('should not change date', () => { | ||
const date = tz.makeDate(2014, 6, 1) | ||
const actual = startOfQuarter(date, tz) | ||
const expected = tz.makeDate(2014, 6, 1) | ||
expect(tz.toISOString(actual)).toBe(tz.toISOString(expected)) | ||
}) | ||
}) | ||
} | ||
}) |