1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00

test(std/datetime): port golang dayOfYear tests (#7105)

This commit is contained in:
Tim Reichen 2020-08-27 11:12:49 +02:00 committed by GitHub
parent 81811e80e1
commit 58bcb9880b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 4 deletions

View file

@ -47,7 +47,11 @@ export function format(date: Date, formatString: string): string {
* @return Number of the day in year
*/
export function dayOfYear(date: Date): number {
const yearStart = new Date(date.getFullYear(), 0, 0);
// Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date)
// Using setFullYear as a workaround
const yearStart = new Date(date);
yearStart.setFullYear(date.getFullYear(), 0, 0);
const diff = date.getTime() -
yearStart.getTime() +
(yearStart.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;

View file

@ -107,9 +107,74 @@ Deno.test({
Deno.test({
name: "[std/datetime] dayOfYear",
fn: () => {
assertEquals(datetime.dayOfYear(new Date("2019-01-01T03:24:00")), 1);
assertEquals(datetime.dayOfYear(new Date("2019-03-11T03:24:00")), 70);
assertEquals(datetime.dayOfYear(new Date("2019-12-31T03:24:00")), 365);
// from https://golang.org/src/time/time_test.go
// Test YearDay in several different scenarios
// and corner cases
// Non-leap-year tests
assertEquals(datetime.dayOfYear(new Date("2007-01-01")), 1);
assertEquals(datetime.dayOfYear(new Date("2007-01-15")), 15);
assertEquals(datetime.dayOfYear(new Date("2007-02-01")), 32);
assertEquals(datetime.dayOfYear(new Date("2007-02-15")), 46);
assertEquals(datetime.dayOfYear(new Date("2007-03-01")), 60);
assertEquals(datetime.dayOfYear(new Date("2007-03-15")), 74);
assertEquals(datetime.dayOfYear(new Date("2007-04-01")), 91);
assertEquals(datetime.dayOfYear(new Date("2007-12-31")), 365);
// Leap-year tests
assertEquals(datetime.dayOfYear(new Date("2008-01-01")), 1);
assertEquals(datetime.dayOfYear(new Date("2008-01-15")), 15);
assertEquals(datetime.dayOfYear(new Date("2008-02-01")), 32);
assertEquals(datetime.dayOfYear(new Date("2008-02-15")), 46);
assertEquals(datetime.dayOfYear(new Date("2008-03-01")), 61);
assertEquals(datetime.dayOfYear(new Date("2008-03-15")), 75);
assertEquals(datetime.dayOfYear(new Date("2008-04-01")), 92);
assertEquals(datetime.dayOfYear(new Date("2008-12-31")), 366);
// Looks like leap-year (but isn't) tests
assertEquals(datetime.dayOfYear(new Date("1900-01-01")), 1);
assertEquals(datetime.dayOfYear(new Date("1900-01-15")), 15);
assertEquals(datetime.dayOfYear(new Date("1900-02-01")), 32);
assertEquals(datetime.dayOfYear(new Date("1900-02-15")), 46);
assertEquals(datetime.dayOfYear(new Date("1900-03-01")), 60);
assertEquals(datetime.dayOfYear(new Date("1900-03-15")), 74);
assertEquals(datetime.dayOfYear(new Date("1900-04-01")), 91);
assertEquals(datetime.dayOfYear(new Date("1900-12-31")), 365);
// Year one tests (non-leap)
assertEquals(datetime.dayOfYear(new Date("0001-01-01")), 1);
assertEquals(datetime.dayOfYear(new Date("0001-01-15")), 15);
assertEquals(datetime.dayOfYear(new Date("0001-02-01")), 32);
assertEquals(datetime.dayOfYear(new Date("0001-02-15")), 46);
assertEquals(datetime.dayOfYear(new Date("0001-03-01")), 60);
assertEquals(datetime.dayOfYear(new Date("0001-03-15")), 74);
assertEquals(datetime.dayOfYear(new Date("0001-04-01")), 91);
assertEquals(datetime.dayOfYear(new Date("0001-12-31")), 365);
// Year minus one tests (non-leap)
assertEquals(datetime.dayOfYear(new Date("-000001-01-01")), 1);
assertEquals(datetime.dayOfYear(new Date("-000001-01-15")), 15);
assertEquals(datetime.dayOfYear(new Date("-000001-02-01")), 32);
assertEquals(datetime.dayOfYear(new Date("-000001-02-15")), 46);
assertEquals(datetime.dayOfYear(new Date("-000001-03-01")), 60);
assertEquals(datetime.dayOfYear(new Date("-000001-03-15")), 74);
assertEquals(datetime.dayOfYear(new Date("-000001-04-01")), 91);
assertEquals(datetime.dayOfYear(new Date("-000001-12-31")), 365);
// // 400 BC tests (leap-year)
assertEquals(datetime.dayOfYear(new Date("-00400-01-01")), 1);
assertEquals(datetime.dayOfYear(new Date("-00400-01-15")), 15);
assertEquals(datetime.dayOfYear(new Date("-00400-02-01")), 32);
assertEquals(datetime.dayOfYear(new Date("-00400-02-15")), 46);
assertEquals(datetime.dayOfYear(new Date("-00400-03-01")), 61);
assertEquals(datetime.dayOfYear(new Date("-00400-03-15")), 75);
assertEquals(datetime.dayOfYear(new Date("-00400-04-01")), 92);
assertEquals(datetime.dayOfYear(new Date("-00400-12-31")), 366);
// Special Cases
// Gregorian calendar change (no effect)
assertEquals(datetime.dayOfYear(new Date("1582-10-04T03:24:00")), 277);
assertEquals(datetime.dayOfYear(new Date("1582-10-15T03:24:00")), 288);
},
});