Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a couple of bugs related to UTC offsets and day-counting. #569

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/scripts/evermarch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

const moment = require("moment-timezone");

const march1 = moment.tz("2020-03-01T00:00:00Z", "America/New_York");
const march1 = moment.tz("2020-03-01T00:00:00", "America/New_York");

module.exports = (robot) => {
robot.message(/evermarch/i, ({ message, say }) => {
const now = moment.tz("America/New_York");

const days = Math.ceil(moment.duration(now.diff(march1)).as("days"));
const days = now.diff(march1, "days") + 1;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculate the diff as days instead of as milliseconds. This will take leap years and stuff into account. Then add one because the diff between March 1 and March 1 is 0 days, but it's the 1st of March.


say({
icon_emoji: ":calendar-this-is-fine:",
Expand Down
63 changes: 52 additions & 11 deletions src/scripts/evermarch.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const moment = require("moment-timezone");
const { getApp } = require("../utils/test");
const evermarch = require("./evermarch");

Expand Down Expand Up @@ -26,11 +27,30 @@ describe("The Evermarch", () => {
});

it("is correct starting March 2, 2020", () => {
// Go very slightly ahead of midnight because at exactly midnight, the diff
// rounds to exactly one day, so the ceil() logic doesn't yet count it as an
// extra day. But a millisecond later, it will. This seems fine. Millisecond
// precision is plenty.
jest.setSystemTime(Date.parse("2020-03-02T00:00:01Z"));
jest.setSystemTime(
moment.tz("2020-03-02T00:00:00", "America/New_York").toDate(),
);
Comment on lines +30 to +32
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests should be in eastern time too. And once we switch to calculating whole day diffs, we don't need the little offset to make things work. They just work!


const message = {
message: { thread_ts: "thread timestamp" },
say: jest.fn(),
};

evermarch(app);
const handler = app.getHandler();
handler(message);

expect(message.say).toHaveBeenCalledWith({
icon_emoji: ":calendar-this-is-fine:",
text: "Today is March 2, 2020, in the Evermarch reckoning.",
thread_ts: "thread timestamp",
});
});

it("is correct at the very end of March 2, 2020", () => {
jest.setSystemTime(
moment.tz("2020-03-02T23:59:59", "America/New_York").toDate(),
);

const message = {
message: { thread_ts: "thread timestamp" },
Expand All @@ -48,12 +68,31 @@ describe("The Evermarch", () => {
});
});

it("is correct at the very start of March 3, 2020", () => {
jest.setSystemTime(
moment.tz("2020-03-03T00:00:00", "America/New_York").toDate(),
);

const message = {
message: { thread_ts: "thread timestamp" },
say: jest.fn(),
};

evermarch(app);
const handler = app.getHandler();
handler(message);

expect(message.say).toHaveBeenCalledWith({
icon_emoji: ":calendar-this-is-fine:",
text: "Today is March 3, 2020, in the Evermarch reckoning.",
thread_ts: "thread timestamp",
});
});

it("is correct in the further future from March 1, 2020", () => {
// Go very slightly ahead of midnight because at exactly midnight, the diff
// rounds to exactly one day, so the ceil() logic doesn't yet count it as an
// extra day. But a millisecond later, it will. This seems fine. Millisecond
// precision is plenty.
jest.setSystemTime(Date.parse("2024-10-15T00:00:01Z"));
jest.setSystemTime(
moment.tz("2024-10-15T01:00:00", "America/New_York").toDate(),
);

const message = {
message: { thread_ts: "thread timestamp" },
Expand All @@ -72,7 +111,9 @@ describe("The Evermarch", () => {
});

it("is gets to March 2020, 2020, on the expected date", () => {
jest.setSystemTime(Date.parse("2025-09-10T12:00:00Z"));
jest.setSystemTime(
moment.tz("2025-09-10T00:00:00", "America/New_York").toDate(),
);
Comment on lines -75 to +116
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this one used to have to be at noon in order to pass is, I think, because of issues with DST. The old millisecond-based diff was bound to drift over time, which we don't really want. So now we don't do it.


const message = {
message: { thread_ts: "thread timestamp" },
Expand Down
Loading