Skip to content

Commit

Permalink
fix(datetime): timezone bug (denoland/deno#7466)
Browse files Browse the repository at this point in the history
  • Loading branch information
timreichen authored and denobot committed Feb 1, 2021
1 parent 577eb3e commit 32c2524
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
5 changes: 3 additions & 2 deletions datetime/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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
Expand Down
18 changes: 9 additions & 9 deletions datetime/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 32c2524

Please sign in to comment.