fix(datetime/day_of_year): respect time zone of date (#3443)

This commit is contained in:
familyboat 2023-06-14 20:26:29 +08:00 committed by GitHub
parent 8bd34c3a45
commit d1879dd9e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 278 additions and 69 deletions

View File

@ -4,7 +4,7 @@
import { DAY } from "./constants.ts";
/**
* Returns the number of the day in the year.
* Returns the number of the day in the year in the local time zone.
*
* @example
* ```ts
@ -13,7 +13,7 @@ import { DAY } from "./constants.ts";
* dayOfYear(new Date("2019-03-11T03:24:00")); // output: 70
* ```
*
* @return Number of the day in year
* @return Number of the day in the year in the local time zone
*/
export function dayOfYear(date: Date): number {
// 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)
@ -21,6 +21,31 @@ export function dayOfYear(date: Date): number {
const yearStart = new Date(date);
yearStart.setFullYear(date.getFullYear(), 0, 0);
const diff = date.getTime() -
yearStart.getTime();
return Math.floor(diff / DAY);
}
/**
* Returns the number of the day in the year in UTC time.
*
* @example
* ```ts
* import { dayOfYearUtc } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
*
* dayOfYearUtc(new Date("2019-03-11T03:24:00.000Z")) // output 70
* ```
*
* @return Number of the day in the year in UTC time
*/
export function dayOfYearUtc(date: Date): number {
// 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 setUTCFullYear as a workaround
const yearStart = new Date(date);
yearStart.setUTCFullYear(date.getUTCFullYear(), 0, 0);
const diff = date.getTime() -
yearStart.getTime();

View File

@ -1,6 +1,190 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import { dayOfYear } from "./day_of_year.ts";
import { dayOfYear, dayOfYearUtc } from "./day_of_year.ts";
Deno.test({
name: "[std/datetime] dayOfYearUtc",
fn: () => {
// from https://golang.org/src/time/time_test.go
// Test YearDay in several different scenarios
// and corner cases
// Non-leap-year tests
assertEquals(dayOfYearUtc(new Date("2007-01-01T00:00:00.000Z")), 1);
assertEquals(dayOfYearUtc(new Date("2007-01-15T00:00:00.000Z")), 15);
assertEquals(dayOfYearUtc(new Date("2007-02-01T00:00:00.000Z")), 32);
assertEquals(dayOfYearUtc(new Date("2007-02-15T00:00:00.000Z")), 46);
assertEquals(dayOfYearUtc(new Date("2007-03-01T00:00:00.000Z")), 60);
assertEquals(dayOfYearUtc(new Date("2007-03-15T00:00:00.000Z")), 74);
assertEquals(dayOfYearUtc(new Date("2007-04-01T00:00:00.000Z")), 91);
assertEquals(dayOfYearUtc(new Date("2007-12-31T00:00:00.000Z")), 365);
assertEquals(dayOfYearUtc(new Date("2007-01-01T00:00:00.000Z")), 1);
assertEquals(
dayOfYearUtc(new Date("2007-02-01T00:00:00.000Z")),
31 + 1,
);
assertEquals(
dayOfYearUtc(new Date("2007-03-01T00:00:00.000Z")),
31 + 28 + 1,
);
assertEquals(
dayOfYearUtc(new Date("2007-03-24T00:00:00.000Z")),
31 + 28 + 24,
);
assertEquals(
dayOfYearUtc(new Date("2007-03-25T00:00:00.000Z")),
31 + 28 + 25,
);
assertEquals(
dayOfYearUtc(new Date("2007-04-01T00:00:00.000Z")),
31 + 28 + 31 + 1,
);
assertEquals(
dayOfYearUtc(new Date("2007-05-01T00:00:00.000Z")),
31 + 28 + 31 + 30 + 1,
);
assertEquals(
dayOfYearUtc(new Date("2007-06-01T00:00:00.000Z")),
31 + 28 + 31 + 30 + 31 + 1,
);
assertEquals(
dayOfYearUtc(new Date("2007-07-01T00:00:00.000Z")),
31 + 28 + 31 + 30 + 31 + 30 + 1,
);
assertEquals(
dayOfYearUtc(new Date("2007-08-01T00:00:00.000Z")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 1,
);
assertEquals(
dayOfYearUtc(new Date("2007-09-01T00:00:00.000Z")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 1,
);
assertEquals(
dayOfYearUtc(new Date("2007-10-01T00:00:00.000Z")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 1,
);
assertEquals(
dayOfYearUtc(new Date("2007-10-27T00:00:00.000Z")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 27,
);
assertEquals(
dayOfYearUtc(new Date("2007-10-28T00:00:00.000Z")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 28,
);
assertEquals(
dayOfYearUtc(new Date("2007-11-01T00:00:00.000Z")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 1,
);
assertEquals(
dayOfYearUtc(new Date("2007-12-01T00:00:00.000Z")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 1,
);
// Leap-year tests
assertEquals(dayOfYearUtc(new Date("2008-01-01T00:00:00.000Z")), 1);
assertEquals(dayOfYearUtc(new Date("2008-01-15T00:00:00.000Z")), 15);
assertEquals(dayOfYearUtc(new Date("2008-02-01T00:00:00.000Z")), 32);
assertEquals(dayOfYearUtc(new Date("2008-02-15T00:00:00.000Z")), 46);
assertEquals(dayOfYearUtc(new Date("2008-03-01T00:00:00.000Z")), 61);
assertEquals(dayOfYearUtc(new Date("2008-03-15T00:00:00.000Z")), 75);
assertEquals(dayOfYearUtc(new Date("2008-04-01T00:00:00.000Z")), 92);
assertEquals(dayOfYearUtc(new Date("2008-12-31T00:00:00.000Z")), 366);
// Looks like leap-year (but isn't) tests
assertEquals(dayOfYearUtc(new Date("1900-01-01T00:00:00.000Z")), 1);
assertEquals(dayOfYearUtc(new Date("1900-01-15T00:00:00.000Z")), 15);
assertEquals(dayOfYearUtc(new Date("1900-02-01T00:00:00.000Z")), 32);
assertEquals(dayOfYearUtc(new Date("1900-02-15T00:00:00.000Z")), 46);
assertEquals(dayOfYearUtc(new Date("1900-03-01T00:00:00.000Z")), 60);
assertEquals(dayOfYearUtc(new Date("1900-03-15T00:00:00.000Z")), 74);
assertEquals(dayOfYearUtc(new Date("1900-04-01T00:00:00.000Z")), 91);
assertEquals(dayOfYearUtc(new Date("1900-12-31T00:00:00.000Z")), 365);
// Year one tests (non-leap)
assertEquals(dayOfYearUtc(new Date("0001-01-01T00:00:00.000Z")), 1);
assertEquals(dayOfYearUtc(new Date("0001-01-15T00:00:00.000Z")), 15);
assertEquals(dayOfYearUtc(new Date("0001-02-01T00:00:00.000Z")), 32);
assertEquals(dayOfYearUtc(new Date("0001-02-15T00:00:00.000Z")), 46);
assertEquals(dayOfYearUtc(new Date("0001-03-01T00:00:00.000Z")), 60);
assertEquals(dayOfYearUtc(new Date("0001-03-15T00:00:00.000Z")), 74);
assertEquals(dayOfYearUtc(new Date("0001-04-01T00:00:00.000Z")), 91);
assertEquals(dayOfYearUtc(new Date("0001-12-31T00:00:00.000Z")), 365);
// Year minus one tests (non-leap)
assertEquals(
dayOfYearUtc(new Date("-000001-01-01T00:00:00.000Z")),
1,
);
assertEquals(
dayOfYearUtc(new Date("-000001-01-15T00:00:00.000Z")),
15,
);
assertEquals(
dayOfYearUtc(new Date("-000001-02-01T00:00:00.000Z")),
32,
);
assertEquals(
dayOfYearUtc(new Date("-000001-02-15T00:00:00.000Z")),
46,
);
assertEquals(
dayOfYearUtc(new Date("-000001-03-01T00:00:00.000Z")),
60,
);
assertEquals(
dayOfYearUtc(new Date("-000001-03-15T00:00:00.000Z")),
74,
);
assertEquals(
dayOfYearUtc(new Date("-000001-04-01T00:00:00.000Z")),
91,
);
assertEquals(
dayOfYearUtc(new Date("-000001-12-31T00:00:00.000Z")),
365,
);
// 400 BC tests (leap-year)
assertEquals(
dayOfYearUtc(new Date("-000400-01-01T00:00:00.000Z")),
1,
);
assertEquals(
dayOfYearUtc(new Date("-000400-01-15T00:00:00.000Z")),
15,
);
assertEquals(
dayOfYearUtc(new Date("-000400-02-01T00:00:00.000Z")),
32,
);
assertEquals(
dayOfYearUtc(new Date("-000400-02-15T00:00:00.000Z")),
46,
);
assertEquals(
dayOfYearUtc(new Date("-000400-03-01T00:00:00.000Z")),
61,
);
assertEquals(
dayOfYearUtc(new Date("-000400-03-15T00:00:00.000Z")),
75,
);
assertEquals(
dayOfYearUtc(new Date("-000400-04-01T00:00:00.000Z")),
92,
);
assertEquals(
dayOfYearUtc(new Date("-000400-12-31T00:00:00.000Z")),
366,
);
// Special Cases
// Gregorian calendar change (no effect)
assertEquals(dayOfYearUtc(new Date("1582-10-04T03:24:00.000Z")), 277);
assertEquals(dayOfYearUtc(new Date("1582-10-15T03:24:00.000Z")), 288);
},
});
Deno.test({
name: "[std/datetime] dayOfYear",
@ -9,179 +193,179 @@ Deno.test({
// Test YearDay in several different scenarios
// and corner cases
// Non-leap-year tests
assertEquals(dayOfYear(new Date("2007-01-01T00:00:00.000Z")), 1);
assertEquals(dayOfYear(new Date("2007-01-15T00:00:00.000Z")), 15);
assertEquals(dayOfYear(new Date("2007-02-01T00:00:00.000Z")), 32);
assertEquals(dayOfYear(new Date("2007-02-15T00:00:00.000Z")), 46);
assertEquals(dayOfYear(new Date("2007-03-01T00:00:00.000Z")), 60);
assertEquals(dayOfYear(new Date("2007-03-15T00:00:00.000Z")), 74);
assertEquals(dayOfYear(new Date("2007-04-01T00:00:00.000Z")), 91);
assertEquals(dayOfYear(new Date("2007-12-31T00:00:00.000Z")), 365);
assertEquals(dayOfYear(new Date("2007-01-01T00:00:00.000")), 1);
assertEquals(dayOfYear(new Date("2007-01-15T00:00:00.000")), 15);
assertEquals(dayOfYear(new Date("2007-02-01T00:00:00.000")), 32);
assertEquals(dayOfYear(new Date("2007-02-15T00:00:00.000")), 46);
assertEquals(dayOfYear(new Date("2007-03-01T00:00:00.000")), 60);
assertEquals(dayOfYear(new Date("2007-03-15T00:00:00.000")), 74);
assertEquals(dayOfYear(new Date("2007-04-01T00:00:00.000")), 91);
assertEquals(dayOfYear(new Date("2007-12-31T00:00:00.000")), 365);
assertEquals(dayOfYear(new Date("2007-01-01T00:00:00.000Z")), 1);
assertEquals(dayOfYear(new Date("2007-01-01T00:00:00.000")), 1);
assertEquals(
dayOfYear(new Date("2007-02-01T00:00:00.000Z")),
dayOfYear(new Date("2007-02-01T00:00:00.000")),
31 + 1,
);
assertEquals(
dayOfYear(new Date("2007-03-01T00:00:00.000Z")),
dayOfYear(new Date("2007-03-01T00:00:00.000")),
31 + 28 + 1,
);
assertEquals(
dayOfYear(new Date("2007-03-24T00:00:00.000Z")),
dayOfYear(new Date("2007-03-24T00:00:00.000")),
31 + 28 + 24,
);
assertEquals(
dayOfYear(new Date("2007-03-25T00:00:00.000Z")),
dayOfYear(new Date("2007-03-25T00:00:00.000")),
31 + 28 + 25,
);
assertEquals(
dayOfYear(new Date("2007-04-01T00:00:00.000Z")),
dayOfYear(new Date("2007-04-01T00:00:00.000")),
31 + 28 + 31 + 1,
);
assertEquals(
dayOfYear(new Date("2007-05-01T00:00:00.000Z")),
dayOfYear(new Date("2007-05-01T00:00:00.000")),
31 + 28 + 31 + 30 + 1,
);
assertEquals(
dayOfYear(new Date("2007-06-01T00:00:00.000Z")),
dayOfYear(new Date("2007-06-01T00:00:00.000")),
31 + 28 + 31 + 30 + 31 + 1,
);
assertEquals(
dayOfYear(new Date("2007-07-01T00:00:00.000Z")),
dayOfYear(new Date("2007-07-01T00:00:00.000")),
31 + 28 + 31 + 30 + 31 + 30 + 1,
);
assertEquals(
dayOfYear(new Date("2007-08-01T00:00:00.000Z")),
dayOfYear(new Date("2007-08-01T00:00:00.000")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 1,
);
assertEquals(
dayOfYear(new Date("2007-09-01T00:00:00.000Z")),
dayOfYear(new Date("2007-09-01T00:00:00.000")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 1,
);
assertEquals(
dayOfYear(new Date("2007-10-01T00:00:00.000Z")),
dayOfYear(new Date("2007-10-01T00:00:00.000")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 1,
);
assertEquals(
dayOfYear(new Date("2007-10-27T00:00:00.000Z")),
dayOfYear(new Date("2007-10-27T00:00:00.000")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 27,
);
assertEquals(
dayOfYear(new Date("2007-10-28T00:00:00.000Z")),
dayOfYear(new Date("2007-10-28T00:00:00.000")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 28,
);
assertEquals(
dayOfYear(new Date("2007-11-01T00:00:00.000Z")),
dayOfYear(new Date("2007-11-01T00:00:00.000")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 1,
);
assertEquals(
dayOfYear(new Date("2007-12-01T00:00:00.000Z")),
dayOfYear(new Date("2007-12-01T00:00:00.000")),
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 1,
);
// Leap-year tests
assertEquals(dayOfYear(new Date("2008-01-01T00:00:00.000Z")), 1);
assertEquals(dayOfYear(new Date("2008-01-15T00:00:00.000Z")), 15);
assertEquals(dayOfYear(new Date("2008-02-01T00:00:00.000Z")), 32);
assertEquals(dayOfYear(new Date("2008-02-15T00:00:00.000Z")), 46);
assertEquals(dayOfYear(new Date("2008-03-01T00:00:00.000Z")), 61);
assertEquals(dayOfYear(new Date("2008-03-15T00:00:00.000Z")), 75);
assertEquals(dayOfYear(new Date("2008-04-01T00:00:00.000Z")), 92);
assertEquals(dayOfYear(new Date("2008-12-31T00:00:00.000Z")), 366);
assertEquals(dayOfYear(new Date("2008-01-01T00:00:00.000")), 1);
assertEquals(dayOfYear(new Date("2008-01-15T00:00:00.000")), 15);
assertEquals(dayOfYear(new Date("2008-02-01T00:00:00.000")), 32);
assertEquals(dayOfYear(new Date("2008-02-15T00:00:00.000")), 46);
assertEquals(dayOfYear(new Date("2008-03-01T00:00:00.000")), 61);
assertEquals(dayOfYear(new Date("2008-03-15T00:00:00.000")), 75);
assertEquals(dayOfYear(new Date("2008-04-01T00:00:00.000")), 92);
assertEquals(dayOfYear(new Date("2008-12-31T00:00:00.000")), 366);
// Looks like leap-year (but isn't) tests
assertEquals(dayOfYear(new Date("1900-01-01T00:00:00.000Z")), 1);
assertEquals(dayOfYear(new Date("1900-01-15T00:00:00.000Z")), 15);
assertEquals(dayOfYear(new Date("1900-02-01T00:00:00.000Z")), 32);
assertEquals(dayOfYear(new Date("1900-02-15T00:00:00.000Z")), 46);
assertEquals(dayOfYear(new Date("1900-03-01T00:00:00.000Z")), 60);
assertEquals(dayOfYear(new Date("1900-03-15T00:00:00.000Z")), 74);
assertEquals(dayOfYear(new Date("1900-04-01T00:00:00.000Z")), 91);
assertEquals(dayOfYear(new Date("1900-12-31T00:00:00.000Z")), 365);
assertEquals(dayOfYear(new Date("1900-01-01T00:00:00.000")), 1);
assertEquals(dayOfYear(new Date("1900-01-15T00:00:00.000")), 15);
assertEquals(dayOfYear(new Date("1900-02-01T00:00:00.000")), 32);
assertEquals(dayOfYear(new Date("1900-02-15T00:00:00.000")), 46);
assertEquals(dayOfYear(new Date("1900-03-01T00:00:00.000")), 60);
assertEquals(dayOfYear(new Date("1900-03-15T00:00:00.000")), 74);
assertEquals(dayOfYear(new Date("1900-04-01T00:00:00.000")), 91);
assertEquals(dayOfYear(new Date("1900-12-31T00:00:00.000")), 365);
// Year one tests (non-leap)
assertEquals(dayOfYear(new Date("0001-01-01T00:00:00.000Z")), 1);
assertEquals(dayOfYear(new Date("0001-01-15T00:00:00.000Z")), 15);
assertEquals(dayOfYear(new Date("0001-02-01T00:00:00.000Z")), 32);
assertEquals(dayOfYear(new Date("0001-02-15T00:00:00.000Z")), 46);
assertEquals(dayOfYear(new Date("0001-03-01T00:00:00.000Z")), 60);
assertEquals(dayOfYear(new Date("0001-03-15T00:00:00.000Z")), 74);
assertEquals(dayOfYear(new Date("0001-04-01T00:00:00.000Z")), 91);
assertEquals(dayOfYear(new Date("0001-12-31T00:00:00.000Z")), 365);
assertEquals(dayOfYear(new Date("0001-01-01T00:00:00.000")), 1);
assertEquals(dayOfYear(new Date("0001-01-15T00:00:00.000")), 15);
assertEquals(dayOfYear(new Date("0001-02-01T00:00:00.000")), 32);
assertEquals(dayOfYear(new Date("0001-02-15T00:00:00.000")), 46);
assertEquals(dayOfYear(new Date("0001-03-01T00:00:00.000")), 60);
assertEquals(dayOfYear(new Date("0001-03-15T00:00:00.000")), 74);
assertEquals(dayOfYear(new Date("0001-04-01T00:00:00.000")), 91);
assertEquals(dayOfYear(new Date("0001-12-31T00:00:00.000")), 365);
// Year minus one tests (non-leap)
assertEquals(
dayOfYear(new Date("-000001-01-01T00:00:00.000Z")),
dayOfYear(new Date("-000001-01-01T00:00:00.000")),
1,
);
assertEquals(
dayOfYear(new Date("-000001-01-15T00:00:00.000Z")),
dayOfYear(new Date("-000001-01-15T00:00:00.000")),
15,
);
assertEquals(
dayOfYear(new Date("-000001-02-01T00:00:00.000Z")),
dayOfYear(new Date("-000001-02-01T00:00:00.000")),
32,
);
assertEquals(
dayOfYear(new Date("-000001-02-15T00:00:00.000Z")),
dayOfYear(new Date("-000001-02-15T00:00:00.000")),
46,
);
assertEquals(
dayOfYear(new Date("-000001-03-01T00:00:00.000Z")),
dayOfYear(new Date("-000001-03-01T00:00:00.000")),
60,
);
assertEquals(
dayOfYear(new Date("-000001-03-15T00:00:00.000Z")),
dayOfYear(new Date("-000001-03-15T00:00:00.000")),
74,
);
assertEquals(
dayOfYear(new Date("-000001-04-01T00:00:00.000Z")),
dayOfYear(new Date("-000001-04-01T00:00:00.000")),
91,
);
assertEquals(
dayOfYear(new Date("-000001-12-31T00:00:00.000Z")),
dayOfYear(new Date("-000001-12-31T00:00:00.000")),
365,
);
// 400 BC tests (leap-year)
assertEquals(
dayOfYear(new Date("-000400-01-01T00:00:00.000Z")),
dayOfYear(new Date("-000400-01-01T00:00:00.000")),
1,
);
assertEquals(
dayOfYear(new Date("-000400-01-15T00:00:00.000Z")),
dayOfYear(new Date("-000400-01-15T00:00:00.000")),
15,
);
assertEquals(
dayOfYear(new Date("-000400-02-01T00:00:00.000Z")),
dayOfYear(new Date("-000400-02-01T00:00:00.000")),
32,
);
assertEquals(
dayOfYear(new Date("-000400-02-15T00:00:00.000Z")),
dayOfYear(new Date("-000400-02-15T00:00:00.000")),
46,
);
assertEquals(
dayOfYear(new Date("-000400-03-01T00:00:00.000Z")),
dayOfYear(new Date("-000400-03-01T00:00:00.000")),
61,
);
assertEquals(
dayOfYear(new Date("-000400-03-15T00:00:00.000Z")),
dayOfYear(new Date("-000400-03-15T00:00:00.000")),
75,
);
assertEquals(
dayOfYear(new Date("-000400-04-01T00:00:00.000Z")),
dayOfYear(new Date("-000400-04-01T00:00:00.000")),
92,
);
assertEquals(
dayOfYear(new Date("-000400-12-31T00:00:00.000Z")),
dayOfYear(new Date("-000400-12-31T00:00:00.000")),
366,
);
// Special Cases
// Gregorian calendar change (no effect)
assertEquals(dayOfYear(new Date("1582-10-04T03:24:00.000Z")), 277);
assertEquals(dayOfYear(new Date("1582-10-15T03:24:00.000Z")), 288);
assertEquals(dayOfYear(new Date("1582-10-04T03:24:00.000")), 277);
assertEquals(dayOfYear(new Date("1582-10-15T03:24:00.000")), 288);
},
});