mirror of
https://github.com/denoland/deno.git
synced 2024-11-26 16:09:27 -05:00
fix(std/datetime): timezone bug (#7466)
This commit is contained in:
parent
44343a8aee
commit
28c9d90b4b
2 changed files with 13 additions and 12 deletions
|
@ -49,15 +49,16 @@ export function format(date: Date, formatString: string): string {
|
||||||
export function dayOfYear(date: Date): number {
|
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)
|
// 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
|
// 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() -
|
const diff = date.getTime() -
|
||||||
yearStart.getTime() +
|
yearStart.getTime() +
|
||||||
(yearStart.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
|
(yearStart.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
|
||||||
|
|
||||||
return Math.floor(diff / DAY);
|
return Math.floor(diff / DAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get number of the week in the year (ISO-8601)
|
* Get number of the week in the year (ISO-8601)
|
||||||
* @return Number of the week in year
|
* @return Number of the week in year
|
||||||
|
|
|
@ -160,15 +160,15 @@ Deno.test({
|
||||||
assertEquals(datetime.dayOfYear(new Date("-000001-04-01")), 91);
|
assertEquals(datetime.dayOfYear(new Date("-000001-04-01")), 91);
|
||||||
assertEquals(datetime.dayOfYear(new Date("-000001-12-31")), 365);
|
assertEquals(datetime.dayOfYear(new Date("-000001-12-31")), 365);
|
||||||
|
|
||||||
// // 400 BC tests (leap-year)
|
// 400 BC tests (leap-year)
|
||||||
assertEquals(datetime.dayOfYear(new Date("-00400-01-01")), 1);
|
assertEquals(datetime.dayOfYear(new Date("-000400-01-01")), 1);
|
||||||
assertEquals(datetime.dayOfYear(new Date("-00400-01-15")), 15);
|
assertEquals(datetime.dayOfYear(new Date("-000400-01-15")), 15);
|
||||||
assertEquals(datetime.dayOfYear(new Date("-00400-02-01")), 32);
|
assertEquals(datetime.dayOfYear(new Date("-000400-02-01")), 32);
|
||||||
assertEquals(datetime.dayOfYear(new Date("-00400-02-15")), 46);
|
assertEquals(datetime.dayOfYear(new Date("-000400-02-15")), 46);
|
||||||
assertEquals(datetime.dayOfYear(new Date("-00400-03-01")), 61);
|
assertEquals(datetime.dayOfYear(new Date("-000400-03-01")), 61);
|
||||||
assertEquals(datetime.dayOfYear(new Date("-00400-03-15")), 75);
|
assertEquals(datetime.dayOfYear(new Date("-000400-03-15")), 75);
|
||||||
assertEquals(datetime.dayOfYear(new Date("-00400-04-01")), 92);
|
assertEquals(datetime.dayOfYear(new Date("-000400-04-01")), 92);
|
||||||
assertEquals(datetime.dayOfYear(new Date("-00400-12-31")), 366);
|
assertEquals(datetime.dayOfYear(new Date("-000400-12-31")), 366);
|
||||||
|
|
||||||
// Special Cases
|
// Special Cases
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue