Skip to content

Commit

Permalink
Treat backend's CRONS as UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
tchojnacki committed Dec 15, 2023
1 parent 4583b82 commit 4cb22c3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions common/time/consts.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const dayInMs = 1000 * 60 * 60 * 24;
export const dayInMins = 60 * 24;
35 changes: 32 additions & 3 deletions common/time/cron.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import { type CronExpression, parseExpression } from "cron-parser";

import { dayInMins } from "./consts";
import { formatShortOffset } from "./offset";
import { getTzMinuteDiff } from "./timezone";

import type { Translator } from "@/common/i18n";
import { parseNum } from "@/common/parsing";

export class Cron {
private readonly expression: CronExpression;
public static fromUtcString(utcCron: string): Cron | null {
const utcParts = utcCron.split(" ");
const utcMin = parseNum(utcParts[0] ?? "");
const utcHour = parseNum(utcParts[1] ?? "");

public constructor(cron: string) {
this.expression = parseExpression(cron);
if (utcMin === null || utcHour === null) {
return null;
}

const utcTotalMins = utcHour * 60 + utcMin;
const localTotalMins = mod(utcTotalMins - getTzMinuteDiff(), dayInMins);
const localHour = Math.floor(localTotalMins / 60);
const localMin = localTotalMins % 60;

return Cron.fromLocalString(`${localMin} ${localHour} * * *`);
}

public static fromLocalString(localCron: string): Cron | null {
try {
return new Cron(parseExpression(localCron), localCron);
} catch {
return null;
}
}

private constructor(
private readonly expression: CronExpression,
private readonly source: string,
) {}

public nextDate(): Date {
const next = this.expression.next();
this.expression.reset();
Expand Down Expand Up @@ -40,3 +67,5 @@ export class Cron {
return formatShortOffset(tillPrev < tillNext ? prevDate : nextDate, t);
}
}

const mod = (a: number, b: number): number => ((a % b) + b) % b;
1 change: 1 addition & 0 deletions common/time/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Cron } from "./cron";
export { formatShortOffset, formatLongOffset } from "./offset";
export { toMinutesAndSeconds } from "./timer";
export { getTzMinuteDiff } from "./timezone";
1 change: 1 addition & 0 deletions common/time/timezone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getTzMinuteDiff = () => new Date().getTimezoneOffset();
2 changes: 1 addition & 1 deletion logic/medication/reminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Reminder {
data.amountPerIntake,
data.amountOwned ?? null,
data.amountUnit ?? null,
data.cron ? new Cron(data.cron) : null,
data.cron ? Cron.fromUtcString(data.cron) : null,
data.description ?? null,
);
}
Expand Down

0 comments on commit 4cb22c3

Please sign in to comment.