Skip to content

Commit 0d4e430

Browse files
committed
refactor: Implement timezone-aware date calculations for dashboard summaries and simplify elapsed time tracking by removing break time subtraction.
1 parent 3f3ecb9 commit 0d4e430

File tree

3 files changed

+64
-42
lines changed

3 files changed

+64
-42
lines changed

app/actions/dashboard-summary.ts

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,68 @@ export async function getDashboardSummary(
3333
userId: number,
3434
): Promise<{ success: boolean; data?: SummaryData; error?: string }> {
3535
try {
36-
// Get today's data
37-
const today = new Date()
38-
const startOfToday = new Date(today)
39-
startOfToday.setHours(0, 0, 0, 0)
40-
const endOfToday = new Date(today)
41-
endOfToday.setHours(23, 59, 59, 999)
42-
43-
// Get yesterday's data for comparison
44-
const yesterday = new Date(today)
45-
yesterday.setDate(yesterday.getDate() - 1)
46-
const startOfYesterday = new Date(yesterday)
47-
startOfYesterday.setHours(0, 0, 0, 0)
48-
const endOfYesterday = new Date(yesterday)
49-
endOfYesterday.setHours(23, 59, 59, 999)
50-
51-
// Get start of week
52-
const startOfWeek = new Date(today)
53-
startOfWeek.setDate(today.getDate() - today.getDay()) // Start from Sunday
54-
startOfWeek.setHours(0, 0, 0, 0)
55-
56-
// Get start of last week
57-
const startOfLastWeek = new Date(startOfWeek)
58-
startOfLastWeek.setDate(startOfLastWeek.getDate() - 7)
59-
const endOfLastWeek = new Date(startOfWeek)
60-
endOfLastWeek.setMilliseconds(-1)
61-
62-
// Get start of month
63-
const startOfMonth = new Date(today.getFullYear(), today.getMonth(), 1)
64-
startOfMonth.setHours(0, 0, 0, 0)
65-
66-
// Get start of last month
67-
const startOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1)
68-
startOfLastMonth.setHours(0, 0, 0, 0)
69-
const endOfLastMonth = new Date(startOfMonth)
70-
endOfLastMonth.setMilliseconds(-1)
36+
const tz = 'America/Sao_Paulo'
37+
38+
// Get year, month (0-11), day (1-31) in BRT timezone from a given Date
39+
const getBrParts = (d: Date) => {
40+
const parts = new Intl.DateTimeFormat('en-US', {
41+
timeZone: tz,
42+
year: 'numeric', month: 'numeric', day: 'numeric'
43+
}).formatToParts(d)
44+
45+
return {
46+
year: parseInt(parts.find(p => p.type === 'year')!.value),
47+
month: parseInt(parts.find(p => p.type === 'month')!.value) - 1,
48+
date: parseInt(parts.find(p => p.type === 'day')!.value)
49+
}
50+
}
51+
52+
const brParts = getBrParts(new Date())
53+
54+
const createBrDate = (y: number, m: number, d: number, isEndOfDay: boolean) => {
55+
const pad = (n: number) => String(n).padStart(2, '0')
56+
const timeStr = isEndOfDay ? "23:59:59.999" : "00:00:00.000"
57+
return new Date(`${y}-${pad(m + 1)}-${pad(d)}T${timeStr}-03:00`)
58+
}
59+
60+
const getMathDate = (y: number, m: number, d: number) => new Date(y, m, d)
61+
62+
// Today
63+
const { year, month, date } = brParts
64+
const todayMath = getMathDate(year, month, date)
65+
66+
const startOfToday = createBrDate(year, month, date, false)
67+
const endOfToday = createBrDate(year, month, date, true)
68+
69+
// Yesterday
70+
const yesterdayMath = new Date(todayMath)
71+
yesterdayMath.setDate(yesterdayMath.getDate() - 1)
72+
const startOfYesterday = createBrDate(yesterdayMath.getFullYear(), yesterdayMath.getMonth(), yesterdayMath.getDate(), false)
73+
const endOfYesterday = createBrDate(yesterdayMath.getFullYear(), yesterdayMath.getMonth(), yesterdayMath.getDate(), true)
74+
75+
// This week (starting Sunday)
76+
const startOfWeekMath = new Date(todayMath)
77+
startOfWeekMath.setDate(startOfWeekMath.getDate() - startOfWeekMath.getDay())
78+
const startOfWeek = createBrDate(startOfWeekMath.getFullYear(), startOfWeekMath.getMonth(), startOfWeekMath.getDate(), false)
79+
80+
// Last week
81+
const startOfLastWeekMath = new Date(startOfWeekMath)
82+
startOfLastWeekMath.setDate(startOfLastWeekMath.getDate() - 7)
83+
const startOfLastWeek = createBrDate(startOfLastWeekMath.getFullYear(), startOfLastWeekMath.getMonth(), startOfLastWeekMath.getDate(), false)
84+
85+
const endOfLastWeekMath = new Date(startOfWeekMath)
86+
endOfLastWeekMath.setDate(endOfLastWeekMath.getDate() - 1)
87+
const endOfLastWeek = createBrDate(endOfLastWeekMath.getFullYear(), endOfLastWeekMath.getMonth(), endOfLastWeekMath.getDate(), true)
88+
89+
// This month
90+
const startOfMonth = createBrDate(year, month, 1, false)
91+
92+
// Last month
93+
const startOfLastMonthMath = new Date(year, month - 1, 1)
94+
const startOfLastMonth = createBrDate(startOfLastMonthMath.getFullYear(), startOfLastMonthMath.getMonth(), 1, false)
95+
96+
const endOfLastMonthMath = new Date(year, month, 0) // Last day of last month
97+
const endOfLastMonth = createBrDate(endOfLastMonthMath.getFullYear(), endOfLastMonthMath.getMonth(), endOfLastMonthMath.getDate(), true)
7198

7299
// Today's data
73100
const todayResult = await sql`

app/dashboard/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export default async function DashboardPage() {
1717

1818
// Get dashboard summary data
1919
const summaryResult = await getDashboardSummary(user.id)
20-
console.log("Dashboard summary result:", summaryResult)
2120
const summary = summaryResult.success ? summaryResult.data : null
2221

2322
const getGreeting = () => {

components/time-tracker.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function TimeTracker({ userId }: { userId: number }) {
5656
setTotalBreakTime(breakResult.data)
5757
}
5858
}
59-
59+
6060
} catch (error) {
6161
console.error("Error checking active time entry:", error)
6262
toast({
@@ -78,12 +78,8 @@ export function TimeTracker({ userId }: { userId: number }) {
7878
if (status === "working" && startTime) {
7979
interval = setInterval(() => {
8080
const now = new Date()
81-
let totalElapsed = Math.floor((now.getTime() - startTime.getTime()) / 1000) * 1000
82-
if (totalBreakTime > 0) {
83-
totalElapsed -= totalBreakTime
84-
}
81+
const totalElapsed = Math.floor((now.getTime() - startTime.getTime()) / 1000) * 1000
8582
setElapsedTime(totalElapsed)
86-
setTotalBreakTime(totalBreakTime)
8783
}, 1000)
8884
} else if (status === "break" && breakStartTime) {
8985
interval = setInterval(() => {

0 commit comments

Comments
 (0)