-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-timezone.js
More file actions
54 lines (44 loc) · 2.11 KB
/
test-timezone.js
File metadata and controls
54 lines (44 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Test script to verify timezone fix
const votes = [
{ created_at: "2024-01-15T08:00:00Z", value: 4 }, // 08:00 UTC should match Breakfast (07:00-10:30)
{ created_at: "2024-01-15T13:00:00Z", value: 3 }, // 13:00 UTC should match Lunch (11:30-14:30)
{ created_at: "2024-01-15T20:00:00Z", value: 5 }, // 20:00 UTC should match Dinner (18:00-21:30)
];
const mealPeriods = [
{ name: "Breakfast", start_time: "07:00", end_time: "10:30" },
{ name: "Lunch", start_time: "11:30", end_time: "14:30" },
{ name: "Dinner", start_time: "18:00", end_time: "21:30" },
];
// OLD (BROKEN) method using toTimeString()
function getMealPeriodForVoteOLD(vote, mealPeriods) {
const voteTime = new Date(vote.created_at).toTimeString().slice(0, 5);
console.log(`OLD: Vote ${vote.created_at} -> Local time: ${voteTime}`);
const meal = mealPeriods.find((meal) => {
return voteTime >= meal.start_time && voteTime <= meal.end_time;
});
return meal ? meal.name : 'No meal period';
}
// NEW (FIXED) method using UTC time
function getMealPeriodForVoteNEW(vote, mealPeriods) {
const voteDate = new Date(vote.created_at);
const utcHours = voteDate.getUTCHours().toString().padStart(2, '0');
const utcMinutes = voteDate.getUTCMinutes().toString().padStart(2, '0');
const voteTime = `${utcHours}:${utcMinutes}`;
console.log(`NEW: Vote ${vote.created_at} -> UTC time: ${voteTime}`);
const meal = mealPeriods.find((meal) => {
return voteTime >= meal.start_time && voteTime <= meal.end_time;
});
return meal ? meal.name : 'No meal period';
}
console.log("=== TIMEZONE FIX TEST ===");
console.log("Current local timezone:", Intl.DateTimeFormat().resolvedOptions().timeZone);
console.log("");
votes.forEach((vote, index) => {
console.log(`--- Vote ${index + 1}: ${vote.created_at} (${vote.value} stars) ---`);
const oldResult = getMealPeriodForVoteOLD(vote, mealPeriods);
const newResult = getMealPeriodForVoteNEW(vote, mealPeriods);
console.log(`OLD method result: ${oldResult}`);
console.log(`NEW method result: ${newResult}`);
console.log(`Fixed: ${oldResult !== newResult ? 'YES' : 'NO'}`);
console.log("");
});