Skip to content

Commit d7e43ec

Browse files
authored
Merge pull request #57 from Kisagi07/patch/get-toda-goal-using-client-date-instead-of-server
Use Browser Timezone When Getting Today Total Focus
2 parents 5b951ab + 4cf1be5 commit d7e43ec

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

app/components/SolCycle.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,12 @@ const SolCycle = () => {
242242
setDailyTarget(response.toString());
243243
}
244244
});
245-
getTodayTotalFocus().then((response) => {
245+
246+
// Get locale for today total focus
247+
const locale = navigator.language;
248+
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
249+
250+
getTodayTotalFocus(timeZone, locale).then((response) => {
246251
if (response === "FAIL") {
247252
toast("Failed to get today total focus", "red");
248253
} else if (response) {

lib/getTodayTotalFocus.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import redis from "@/app/upstash";
44
import getTodayDateString from "@/utils/getTodayDateString";
55

6-
const getTodayTotalFocus = async () => {
6+
const getTodayTotalFocus = async (timezone:string, locale:string) => {
77
try {
88
const response = await redis.get<DayFocus[]>("total-focus");
99
if (response) {
10-
const todayDate = getTodayDateString();
10+
const todayDate = getTodayDateString(timezone, locale);
1111
const today = response.find((day) => day.date === todayDate);
1212
if (today) {
1313
return today.total;

utils/getTodayDateString.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
const getTodayDateString = () => {
1+
const getTodayDateString = (timeZone:string, locale:string) => {
22
const now = new Date();
3-
const year = now.getFullYear();
4-
const month = now.getMonth();
5-
const date = now.getDate();
6-
const dateString = `${year}-${month + 1}-${date}`;
7-
return dateString;
3+
4+
const formatter = new Intl.DateTimeFormat(locale,{timeZone, year: "numeric", month: "2-digit", "day": "2-digit"});
5+
6+
// Format returns "YYYY-MM-DD" for "en-CA" locale
7+
const parts = formatter.formatToParts(now);
8+
const year = parts.find(p => p.type === "year")?.value;
9+
const month = parts.find(p => p.type === "month")?.value;
10+
const day = parts.find(p => p.type === "day")?.value;
11+
12+
return `${year}-${month}-${day}`;
813
};
914
export default getTodayDateString;

0 commit comments

Comments
 (0)