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

fix(std/datetime): timezone bug (#7466)

This commit is contained in:
Tim Reichen 2020-09-15 16:09:40 +02:00 committed by GitHub
parent 44343a8aee
commit 28c9d90b4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 12 deletions

View file

@ -49,15 +49,16 @@ export function format(date: Date, formatString: string): string {
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)
// Using setFullYear as a workaround
const yearStart = new Date(date);
yearStart.setFullYear(date.getFullYear(), 0, 0);
const yearStart = new Date(date);
yearStart.setUTCFullYear(date.getUTCFullYear(), 0, 0);
const diff = date.getTime() -
yearStart.getTime() +
(yearStart.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
return Math.floor(diff / DAY);
}
/**
* Get number of the week in the year (ISO-8601)
* @return Number of the week in year

View file

@ -160,15 +160,15 @@ Deno.test({
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);
// 400 BC tests (leap-year)
assertEquals(datetime.dayOfYear(new Date("-000400-01-01")), 1);
assertEquals(datetime.dayOfYear(new Date("-000400-01-15")), 15);
assertEquals(datetime.dayOfYear(new Date("-000400-02-01")), 32);
assertEquals(datetime.dayOfYear(new Date("-000400-02-15")), 46);
assertEquals(datetime.dayOfYear(new Date("-000400-03-01")), 61);
assertEquals(datetime.dayOfYear(new Date("-000400-03-15")), 75);
assertEquals(datetime.dayOfYear(new Date("-000400-04-01")), 92);
assertEquals(datetime.dayOfYear(new Date("-000400-12-31")), 366);
// Special Cases