Commit 1f49140

bryfry <bryon@fryer.io>
2025-01-19 19:37:49
add timer
1 parent 3ab1022
Changed files (2)
js/clock.js
@@ -1,22 +1,49 @@
+const timer = document.getElementById('timer');
 const epoch = document.getElementById('epoch');
 const clock = document.getElementById('clock');
 const date = document.getElementById('date');
 const week = document.getElementById('week');
 const updateInterval = 1000;
+const start = Temporal.Now.zonedDateTimeISO();
 
 function clockNumber(s) {
 	return String(s).padStart(2,'0')
 }
 
+function formatDuration(seconds) {
+	if (seconds < 0) return "0s";
+	
+	const units = [
+		{ label: "d", value: 86400 }, // Days
+		{ label: "h", value: 3600 },  // Hours
+		{ label: "m", value: 60 },    // Minutes
+		{ label: "s", value: 1 }      // Seconds
+	];
+	
+	let result = "";
+	for (const unit of units) {
+		if (seconds >= unit.value) {
+			const count = Math.floor(seconds / unit.value);
+			result += `${count}${unit.label}`;
+			seconds %= unit.value;
+		}
+	}
+	
+	return result || "0s";
+}
+
 function updateClock() {
 	const now = Temporal.Now.zonedDateTimeISO();
+	const sinceRefresh = now.epochSeconds - start.epochSeconds;
 
+	const formatted_timer = `${formatDuration(sinceRefresh)}`
 	const formatted_epoch = `${now.epochSeconds}`;
 	const formatted_now = `${clockNumber(now.hour)}:${clockNumber(now.minute)}`;
 	const formatted_date = `${now.toPlainDate().toString()}`;
 	const formatted_week = `week: ${now.weekOfYear}`;
 
 	epoch.innerText = formatted_epoch;
+	timer.innerText = formatted_timer;
 	if (clock.innerText != formatted_now) {
 		clock.innerText = formatted_now;
 		date.innerText = formatted_date;
index.html
@@ -15,6 +15,7 @@
     <main>
       <div class="details">
         <div id="epoch"></div>
+        <div id="timer"></div>
       </div>
       <div id="clock"></div>
       <div class="details">