Skip to content

Commit

Permalink
Fix download charts after a DST transition
Browse files Browse the repository at this point in the history
When falling back, a bug in date-fns[^0] results in the 23 hour day
being skipped when building the `dates` array in `toChartData` if the
current time is between 00:00 and 01:00 UTC. This results in that day's
data essentially going missing, resulting in the subtly broken charts
noted in rust-lang#5477.

This commit adds a workaround adapted from a comment on
date-fns/date-fns#571[^1] by @bertho-zero, which correctly adjusts the
new date based on the time zone offsets, and means that `dates` is built
as expected.

Fixes rust-lang#5477.

[^0]: date-fns/date-fns#571
[^1]: date-fns/date-fns#571 (comment)
  • Loading branch information
LawnGnome committed Mar 16, 2023
1 parent df6b7b2 commit 17c7534
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion app/components/download-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
import { waitForPromise } from '@ember/test-waiters';
import Component from '@glimmer/component';

import subDays from 'date-fns/subDays';
import { addMinutes, subDays as brokenSubDays, subMinutes } from 'date-fns';
import window from 'ember-window-mock';
import semverSort from 'semver/functions/sort';

Expand Down Expand Up @@ -190,3 +190,18 @@ export function toChartData(data) {
function midnightForDate(date) {
return Date.parse(date.toISOString().slice(0, 10));
}

// This works around a bug in date-fn's subDays() function when crossing a DST
// transition: https://github.com/date-fns/date-fns/issues/571
//
// The specific implementation is based on @bertho-zero's here:
// https://github.com/date-fns/date-fns/issues/571#issuecomment-602496322
function subDays(date, amount) {
const originalTZO = date.getTimezoneOffset();
const endDate = brokenSubDays(date, amount);
const endTZO = endDate.getTimezoneOffset();

const dstDiff = originalTZO - endTZO;

return dstDiff >= 0 ? addMinutes(endDate, dstDiff) : subMinutes(endDate, Math.abs(dstDiff));
}

0 comments on commit 17c7534

Please sign in to comment.