Skip to content

Commit 8193569

Browse files
fehmerMiodec
andauthored
impr: leaderboard showing times in utc and local time (@fehmer) (monkeytypegame#6332)
Co-authored-by: Miodec <[email protected]>
1 parent b84f400 commit 8193569

File tree

3 files changed

+61
-33
lines changed

3 files changed

+61
-33
lines changed

frontend/src/html/pages/leaderboards.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="bigtitle">
55
<div class="text">-</div>
66
<div class="subtext">
7-
<div class="text"></div>
7+
<div class="text" data-balloon-pos="down" data-balloon-break></div>
88
<div class="divider hidden"></div>
99
<button class="textButton hidden" data-action="toggleYesterday">
1010
<i class="fas fa-backward"></i>

frontend/src/styles/media-queries-purple.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
}
2323
}
2424
}
25+
.pageLeaderboards {
26+
.content .bigtitle .text:after {
27+
left: 0.1rem;
28+
transform: none;
29+
}
30+
}
2531
header {
2632
nav {
2733
font-size: 0.9rem;

frontend/src/ts/pages/leaderboards.ts

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@ import * as Notifications from "../elements/notifications";
1111
import Format from "../utils/format";
1212
import { Auth, isAuthenticated } from "../firebase";
1313
import * as DB from "../db";
14-
import { format } from "date-fns";
14+
import {
15+
endOfDay,
16+
endOfWeek,
17+
format,
18+
startOfDay,
19+
startOfWeek,
20+
subDays,
21+
subHours,
22+
subMinutes,
23+
} from "date-fns";
1524
import { differenceInSeconds } from "date-fns/differenceInSeconds";
1625
import * as DateTime from "../utils/date-and-time";
1726
import { getHtmlByUserFlags } from "../controllers/user-flag-controller";
@@ -22,22 +31,20 @@ import {
2231
isDevEnvironment,
2332
} from "../utils/misc";
2433
import { abbreviateNumber } from "../utils/numbers";
25-
import {
26-
getCurrentWeekTimestamp,
27-
getLastWeekTimestamp,
28-
getStartOfDayTimestamp,
29-
} from "@monkeytype/util/date-and-time";
3034
import { formatDistanceToNow } from "date-fns/formatDistanceToNow";
3135
import { z } from "zod";
3236
import { LocalStorageWithSchema } from "../utils/local-storage-with-schema";
3337
import {
3438
safeParse as parseUrlSearchParams,
3539
serialize as serializeUrlSearchParams,
3640
} from "zod-urlsearchparams";
41+
import { UTCDateMini } from "@date-fns/utc";
3742
// import * as ServerConfiguration from "../ape/server-configuration";
3843

3944
const LeaderboardTypeSchema = z.enum(["allTime", "weekly", "daily"]);
4045
type LeaderboardType = z.infer<typeof LeaderboardTypeSchema>;
46+
const utcDateFormat = "EEEE, do MMMM yyyy";
47+
const localDateFormat = "EEEE, do MMMM yyyy HH:mm";
4148

4249
type AllTimeState = {
4350
type: "allTime";
@@ -166,15 +173,15 @@ function updateTitle(): void {
166173
`);
167174
}
168175

169-
let timestamp = getStartOfDayTimestamp(new Date().getTime());
170-
176+
let timestamp = startOfDay(new UTCDateMini());
171177
if (state.yesterday) {
172-
timestamp -= 24 * 60 * 60 * 100;
178+
timestamp = subHours(timestamp, 24);
173179
}
174180

175-
const dateString = format(timestamp, "EEEE, do MMMM yyyy");
176-
$(".page.pageLeaderboards .bigtitle .subtext > .text").text(
177-
`${dateString}`
181+
updateTimeText(
182+
format(timestamp, utcDateFormat) + " UTC",
183+
utcToLocalDate(timestamp),
184+
utcToLocalDate(endOfDay(timestamp))
178185
);
179186
} else if (state.type === "weekly") {
180187
$(".page.pageLeaderboards .bigtitle .subtext").removeClass("hidden");
@@ -199,18 +206,20 @@ function updateTitle(): void {
199206
`);
200207
}
201208

202-
let fn = getCurrentWeekTimestamp();
203-
209+
let timestamp = startOfWeek(new UTCDateMini(), { weekStartsOn: 1 });
204210
if (state.lastWeek) {
205-
fn = getLastWeekTimestamp();
211+
timestamp = subDays(timestamp, 7);
206212
}
207-
208-
const dateString = `${format(fn, "EEEE, do MMMM yyyy")} - ${format(
209-
fn + 6 * 24 * 60 * 60 * 1000,
210-
"EEEE, do MMMM yyyy"
211-
)}`;
212-
$(".page.pageLeaderboards .bigtitle .subtext > .text").text(
213-
`${dateString}`
213+
const endingTimestamp = endOfWeek(timestamp, { weekStartsOn: 1 });
214+
215+
const dateString = `${format(timestamp, utcDateFormat)} - ${format(
216+
endingTimestamp,
217+
utcDateFormat
218+
)} UTC`;
219+
updateTimeText(
220+
dateString,
221+
utcToLocalDate(timestamp),
222+
utcToLocalDate(endingTimestamp)
214223
);
215224
}
216225
}
@@ -979,12 +988,7 @@ let updateTimer: number | undefined;
979988

980989
function updateTimerElement(): void {
981990
if (state.type === "daily") {
982-
const date = new Date();
983-
date.setUTCHours(0, 0, 0, 0);
984-
date.setDate(date.getDate() + 1);
985-
const dateNow = new Date();
986-
dateNow.setUTCMilliseconds(0);
987-
const diff = differenceInSeconds(date, dateNow);
991+
const diff = differenceInSeconds(new Date(), endOfDay(new UTCDateMini()));
988992

989993
$(".page.pageLeaderboards .titleAndButtons .timer").text(
990994
"Next reset in: " + DateTime.secondsToString(diff, true)
@@ -998,10 +1002,8 @@ function updateTimerElement(): void {
9981002
"Next update in: " + DateTime.secondsToString(totalSeconds, true)
9991003
);
10001004
} else if (state.type === "weekly") {
1001-
const nextWeekTimestamp =
1002-
getCurrentWeekTimestamp() + 7 * 24 * 60 * 60 * 1000;
1003-
const currentTime = new Date().getTime();
1004-
const totalSeconds = Math.floor((nextWeekTimestamp - currentTime) / 1000);
1005+
const nextWeekTimestamp = endOfWeek(new UTCDateMini(), { weekStartsOn: 1 });
1006+
const totalSeconds = differenceInSeconds(new Date(), nextWeekTimestamp);
10051007
$(".page.pageLeaderboards .titleAndButtons .timer").text(
10061008
"Next reset in: " +
10071009
DateTime.secondsToString(totalSeconds, true, true, ":", true, true)
@@ -1318,3 +1320,23 @@ export const page = new Page({
13181320
$(async () => {
13191321
Skeleton.save("pageLeaderboards");
13201322
});
1323+
1324+
function utcToLocalDate(timestamp: UTCDateMini): Date {
1325+
return subMinutes(timestamp, new Date().getTimezoneOffset());
1326+
}
1327+
1328+
function updateTimeText(
1329+
dateString: string,
1330+
localStart: Date,
1331+
localEnd: Date
1332+
): void {
1333+
const localDateString =
1334+
"local time \n" +
1335+
format(localStart, localDateFormat) +
1336+
" - \n" +
1337+
format(localEnd, localDateFormat);
1338+
1339+
const text = $(".page.pageLeaderboards .bigtitle .subtext > .text");
1340+
text.text(`${dateString}`);
1341+
text.attr("aria-label", localDateString);
1342+
}

0 commit comments

Comments
 (0)