Skip to content
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
11 changes: 9 additions & 2 deletions src/scripts/timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ const TIMEZONES = {
};

const matcher =
/(\d{1,2}:\d{2}\s?(am|pm)?)\s?(((ak|a|c|e|m|p)(s|d)?t)|:(eastern|central|mountain|pacific)-time-zone:)?/i;
/(\d{1,2}:\d{2}\s?(am|pm)?)\s?(local(?:\s+time)?|((ak|a|c|e|m|p)(s|d)?t)|:(eastern|central|mountain|pacific)-time-zone:)?/i;

// "local time" means whatever the reader's clock says, not the author's, so
// there's nothing to convert. Mark those matches so we drop them up front.
const isLocalTimeMatch = (timezone) =>
typeof timezone === "string" && /^local/i.test(timezone);

module.exports = (app) => {
helpMessage.registerNonInteractive(
Expand All @@ -56,7 +61,9 @@ module.exports = (app) => {
let m = null;
let ampm = null;

const matches = [...text.matchAll(RegExp(matcher, "gi"))];
const matches = [...text.matchAll(RegExp(matcher, "gi"))].filter(
([, , , timezone]) => !isLocalTimeMatch(timezone),
);

// If there aren't any matches, that can be because this was crossposted.
// We don't want to have the bot respond to those because the authorship of
Expand Down
16 changes: 15 additions & 1 deletion src/scripts/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("Handy Tau-bot timezone conversions", () => {
timezone(app);

expect(app.message).toHaveBeenCalledWith(
/(\d{1,2}:\d{2}\s?(am|pm)?)\s?(((ak|a|c|e|m|p)(s|d)?t)|:(eastern|central|mountain|pacific)-time-zone:)?/i,
/(\d{1,2}:\d{2}\s?(am|pm)?)\s?(local(?:\s+time)?|((ak|a|c|e|m|p)(s|d)?t)|:(eastern|central|mountain|pacific)-time-zone:)?/i,
expect.any(Function),
);
});
Expand Down Expand Up @@ -242,6 +242,20 @@ describe("Handy Tau-bot timezone conversions", () => {
expect(slack.postEphemeralMessage).not.toHaveBeenCalled();
});

it('does not respond when the time is followed by "local time"', async () => {
message.event.text = "Is 10:51 local time too early for more pizza talk?";
await handler(message);

expect(slack.postEphemeralMessage).not.toHaveBeenCalled();
});

it('does not respond when the time is followed by "local"', async () => {
message.event.text = "meeting at 9:00 local";
await handler(message);

expect(slack.postEphemeralMessage).not.toHaveBeenCalled();
});

it("correctly converts 24 hour time", async () => {
message.event.text = "23:42 mst";
await handler(message);
Expand Down
Loading