Skip to content

Commit 44072de

Browse files
authored
Fix chore timezone & add midnight refresh
1 parent 241a116 commit 44072de

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

client/src/components/ChoreWidget.jsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import axios from 'axios';
2727
import { API_BASE_URL } from '../utils/apiConfig.js';
2828
import { getDeviceApiBase } from '../utils/deviceName.js';
2929
import { shouldShowChoreToday, getTodayDateString, convertDaysToCrontab } from '../utils/choreHelpers.js';
30+
import { getServerTimezoneSync } from '../utils/timezone.js';
3031

3132
const USERS_UPDATED_EVENT = 'homeglow:users-updated';
3233

@@ -93,6 +94,34 @@ const ChoreWidget = ({ transparentBackground, refreshInterval = 0 }) => {
9394
}
9495
}, [refreshInterval]);
9596

97+
useEffect(() => {
98+
function getMsUntilServerMidnight() {
99+
const tz = getServerTimezoneSync();
100+
const now = new Date();
101+
const formatter = new Intl.DateTimeFormat('en-CA', {
102+
timeZone: tz,
103+
year: 'numeric',
104+
month: '2-digit',
105+
day: '2-digit',
106+
hour: '2-digit',
107+
minute: '2-digit',
108+
second: '2-digit',
109+
hour12: false
110+
});
111+
const parts = formatter.formatToParts(now);
112+
const get = (type) => parseInt(parts.find(p => p.type === type).value, 10);
113+
const h = get('hour'), m = get('minute'), s = get('second');
114+
const secondsUntilMidnight = (24 * 3600) - (h * 3600 + m * 60 + s);
115+
return secondsUntilMidnight * 1000 + 1000;
116+
}
117+
118+
const timerId = setTimeout(() => {
119+
fetchData();
120+
}, getMsUntilServerMidnight());
121+
122+
return () => clearTimeout(timerId);
123+
}, [history]);
124+
96125
useEffect(() => {
97126
if (!deviceSettingsLoaded) {
98127
return;

client/src/utils/choreHelpers.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ export function shouldShowChoreToday(schedule) {
1212

1313
try {
1414
const tz = getServerTimezoneSync();
15-
const today = new Date();
16-
today.setHours(0, 0, 0, 0);
15+
const todayStr = getTodayDateString();
16+
const [year, month, day] = todayStr.split('-').map(Number);
17+
const todayInServerTz = new Date(year, month - 1, day);
1718

1819
const interval = CronExpressionParser.parse(schedule.crontab, {
19-
currentDate: new Date(today.getTime() + 24 * 60 * 60 * 1000),
20+
currentDate: new Date(todayInServerTz.getTime() + 24 * 60 * 60 * 1000),
2021
tz
2122
});
2223

2324
const prevOccurrence = interval.prev().toDate();
24-
prevOccurrence.setHours(0, 0, 0, 0);
25+
const prevFormatter = new Intl.DateTimeFormat('en-CA', {
26+
timeZone: tz,
27+
year: 'numeric',
28+
month: '2-digit',
29+
day: '2-digit'
30+
});
31+
const prevStr = prevFormatter.format(prevOccurrence);
2532

26-
return prevOccurrence.getTime() === today.getTime();
33+
return prevStr === todayStr;
2734
} catch (error) {
2835
console.error('Error parsing crontab:', schedule.crontab, error);
2936
return false;

0 commit comments

Comments
 (0)