-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Add getUTCMonday, fix tests in U.S. timezone (#879)
- Loading branch information
1 parent
353b162
commit 6a3bcf1
Showing
7 changed files
with
97 additions
and
5 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
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,66 @@ | ||
import { getMonday, getUTCMonday } from '../../src/utils/dateUtils'; | ||
|
||
const TEST_CASES = [ | ||
{ | ||
title: 'monday', | ||
date: [2023, 9, 4], | ||
expected: [2023, 9, 4] | ||
}, | ||
{ | ||
title: 'monday, cross year', | ||
date: [2014, 12, 1], | ||
expected: [2014, 12, 1] | ||
}, | ||
{ | ||
title: 'monday, february/march normal year', | ||
date: [2015, 2, 23], | ||
expected: [2015, 2, 23] | ||
}, | ||
{ | ||
title: 'monday, february/march step year', | ||
date: [2016, 2, 22], | ||
expected: [2016, 2, 22] | ||
}, | ||
{ | ||
title: 'sunday, cross year', | ||
date: [2023, 1, 1], | ||
expected: [2022, 12, 26] | ||
}, | ||
{ | ||
title: 'saturday, february/march step year', | ||
date: [2024, 3, 2], | ||
expected: [2024, 2, 26] | ||
} | ||
]; | ||
|
||
/** Returns midnight (local time) for given year/month/date. */ | ||
function createDate([year, month, date]) { | ||
return new Date(year, month - 1, date); | ||
} | ||
|
||
/** Returns midnight (UTC) for given year/month/date. */ | ||
function createUTCDate([year, month, date]) { | ||
return new Date(Date.UTC(year, month - 1, date)); | ||
} | ||
|
||
describe('getMonday', () => { | ||
for (const { date, expected, title } of TEST_CASES) { | ||
const expectedString = createDate(expected).toLocaleString(); | ||
it(`${date} ===> ${expectedString} - ${title}`, () => { | ||
const local = getMonday(createDate(date)); | ||
const localString = new Date(local).toLocaleString(); | ||
expect(localString).toBe(expectedString); | ||
}); | ||
} | ||
}); | ||
|
||
describe('getUTCMonday', () => { | ||
for (const { date, expected, title } of TEST_CASES) { | ||
const expectedString = createUTCDate(expected).toISOString(); | ||
it(`${date} ===> ${expectedString} - ${title}`, () => { | ||
const utc = getUTCMonday(createUTCDate(date)); | ||
const utcString = new Date(utc).toISOString(); | ||
expect(utcString).toBe(expectedString); | ||
}); | ||
} | ||
}); |
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 |
---|---|---|
@@ -1,7 +1,24 @@ | ||
/** | ||
* Returns midnight (local time) on the Monday preceeding a given date, in | ||
* milliseconds since the UNIX epoch. | ||
*/ | ||
export function getMonday(date) { | ||
const dateCp = new Date(date); | ||
const day = dateCp.getDay(); | ||
const diff = dateCp.getDate() - day + (day ? 1 : -6); // adjust when day is sunday | ||
dateCp.setDate(diff); | ||
dateCp.setHours(0, 0, 0, 0); | ||
return dateCp.getTime(); | ||
} | ||
|
||
/** | ||
* Returns midnight (UTC) on the Monday preceeding a given date, in | ||
* milliseconds since the UNIX epoch. | ||
*/ | ||
export function getUTCMonday(date) { | ||
const dateCp = new Date(date); | ||
const day = dateCp.getUTCDay(); | ||
const diff = dateCp.getUTCDate() - day + (day ? 1 : -6); // adjust when day is sunday | ||
dateCp.setUTCDate(diff); | ||
return Date.UTC(dateCp.getUTCFullYear(), dateCp.getUTCMonth(), dateCp.getUTCDate()); | ||
} |
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