mirror of
https://github.com/bunny-lab-io/Borealis.git
synced 2025-10-27 09:21:57 -06:00
Added Server DateTime in ServerInfo Page
This commit is contained in:
@@ -1,14 +1,42 @@
|
||||
import React from "react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { Paper, Box, Typography } from "@mui/material";
|
||||
|
||||
export default function ServerInfo() {
|
||||
const [serverTime, setServerTime] = useState(null);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
let isMounted = true;
|
||||
const fetchTime = async () => {
|
||||
try {
|
||||
const resp = await fetch('/api/server/time');
|
||||
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
|
||||
const data = await resp.json();
|
||||
if (isMounted) {
|
||||
setServerTime(data?.display || data?.iso || null);
|
||||
setError(null);
|
||||
}
|
||||
} catch (e) {
|
||||
if (isMounted) setError(String(e));
|
||||
}
|
||||
};
|
||||
fetchTime();
|
||||
const id = setInterval(fetchTime, 60000); // update once per minute
|
||||
return () => { isMounted = false; clearInterval(id); };
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Paper sx={{ m: 2, p: 0, bgcolor: "#1e1e1e" }} elevation={2}>
|
||||
<Box sx={{ p: 2 }}>
|
||||
<Typography variant="h6" sx={{ color: "#58a6ff", mb: 1 }}>Server Info</Typography>
|
||||
<Typography sx={{ color: '#aaa' }}>Basic server information will appear here.</Typography>
|
||||
<Typography sx={{ color: '#aaa', mb: 1 }}>Basic server information will appear here for informative and debug purposes.</Typography>
|
||||
<Box sx={{ display: 'flex', gap: 2, alignItems: 'baseline' }}>
|
||||
<Typography sx={{ color: '#ccc', fontWeight: 600, minWidth: 120 }}>Server Time</Typography>
|
||||
<Typography sx={{ color: error ? '#ff6b6b' : '#ddd', fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace' }}>
|
||||
{error ? `Error: ${error}` : (serverTime || 'Loading...')}
|
||||
</Typography>
|
||||
</Box>
|
||||
</Box>
|
||||
</Paper>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -69,6 +69,43 @@ def serve_dist(path):
|
||||
def health():
|
||||
return jsonify({"status": "ok"})
|
||||
|
||||
# ---------------------------------------------
|
||||
# Server Time Endpoint
|
||||
# ---------------------------------------------
|
||||
@app.route("/api/server/time", methods=["GET"])
|
||||
def api_server_time():
|
||||
try:
|
||||
from datetime import datetime, timezone
|
||||
now_local = datetime.now().astimezone()
|
||||
now_utc = datetime.now(timezone.utc)
|
||||
tzinfo = now_local.tzinfo
|
||||
offset = tzinfo.utcoffset(now_local) if tzinfo else None
|
||||
# Friendly display string, e.g., "September 23rd 2025 @ 12:49AM"
|
||||
def _ordinal(n: int) -> str:
|
||||
if 11 <= (n % 100) <= 13:
|
||||
suf = 'th'
|
||||
else:
|
||||
suf = {1: 'st', 2: 'nd', 3: 'rd'}.get(n % 10, 'th')
|
||||
return f"{n}{suf}"
|
||||
month = now_local.strftime("%B")
|
||||
day_disp = _ordinal(now_local.day)
|
||||
year = now_local.strftime("%Y")
|
||||
hour24 = now_local.hour
|
||||
hour12 = hour24 % 12 or 12
|
||||
minute = now_local.minute
|
||||
ampm = "AM" if hour24 < 12 else "PM"
|
||||
display = f"{month} {day_disp} {year} @ {hour12}:{minute:02d}{ampm}"
|
||||
return jsonify({
|
||||
"epoch": int(now_local.timestamp()),
|
||||
"iso": now_local.isoformat(),
|
||||
"utc_iso": now_utc.isoformat().replace("+00:00", "Z"),
|
||||
"timezone": str(tzinfo) if tzinfo else "",
|
||||
"offset_seconds": int(offset.total_seconds()) if offset else 0,
|
||||
"display": display,
|
||||
})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
# ---------------------------------------------
|
||||
# Auth + Users (DB-backed)
|
||||
# ---------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user