switch to absolute times when old enough

This commit is contained in:
Ville Rantanen
2023-07-28 15:29:16 +03:00
parent f1c453d3d4
commit 7fe10f05a2
4 changed files with 48 additions and 39 deletions

View File

@@ -848,40 +848,30 @@ def utility_processor():
# Try the sane thing first
dt = (n - t) // 10**9
if dt < 1:
return "less than a second"
return "less than a second ago"
if dt < 2:
return f"1 second"
return f"1 second ago"
if dt < 60:
return f"{dt} seconds"
return f"{dt} seconds ago"
if dt < 119:
return f"1 minute"
return f"1 minute ago"
if dt < 3600:
return f"{dt // 60} minutes"
if dt < 3600 * 2:
return f"1 hour"
return f"{dt // 60} minutes ago"
if dt < 3600 * 24:
return f"{dt // 3600} hours"
return f"{dt // 3600} hours ago"
if dt < 3600 * 24 * 7:
return f"{dt // (3600 * 24)} days ago"
if dt < 3600 * 24 * 31:
return f"{dt // (3600 * 24)} days"
return f"{dt // (3600 * 24 * 7)} weeks ago"
# Try some very rough estimate, whatever
f = lambda x: datetime.utcfromtimestamp(x // 10**9)
n, t = f(n), f(t)
def f(x, y, s):
return f'{y - x} {s}{"s" if y - x > 1 else ""}'
if t.year < n.year:
return f(t.year, n.year, "year")
if t.month < n.month:
return f(t.month, n.month, "month")
assert False, "unreachable"
as_datetime = datetime.fromtimestamp(t // 10**9)
return as_datetime.strftime("%Y-%m-%d %H:%M")
def format_since(t):
n = time.time_ns()
if n < t:
return "in a distant future"
return _format_time_delta(n, t) + " ago"
return _format_time_delta(n, t)
def format_until(t):
n = time.time_ns()
@@ -890,7 +880,7 @@ def utility_processor():
return _format_time_delta(t, n)
def format_time(t):
return datetime.utcfromtimestamp(t / 10**9).replace(microsecond=0)
return datetime.fromtimestamp(t / 10**9).replace(microsecond=0)
def rand_password():
"""

View File

@@ -57,10 +57,11 @@ th, td {
textarea {
width: min(100%, 500px);
height: 15em;
height: 25em;
font-size: 1em;
}
input[type=text], input[type=password] {
width: min(100%, 20em);
font-family: monospace;
@@ -211,8 +212,21 @@ table.form > * > tr > td, th {
display: block;
}
/* Admin */
.admin_h2 {
background-color: rgba(0,0,0,0.15);
}
main.admin {
width: 95%;
margin: auto;
}
main.admin textarea {
height: 5em;
}
.config_label {
max-width: 15em;
width: 15em;
}

View File

@@ -11,13 +11,18 @@
{%- endif -%}
</head>
<body>
<h1>{{ title }}</h1>
<p>
<nav>
<div>
<span> &laquo; </span><a href="{{ url_for('index') }}">Forum Home</a>
<span> | </span><a href="{{ url_for('admin') }}">Admin panel</a>
</p>
</div>
</nav>
<main class=admin>
<h1>{{ title }}</h1>
{%- for category, msg in get_flashed_messages(True) -%}
<p class="flash {{ category }}">{{ msg }}</p>
{%- endfor %}
{%- block content %}{% endblock -%}
</main>
</body>

View File

@@ -44,8 +44,8 @@
<h3>Add user</h3>
<form method=post action=user/new/>
<table>
<tr><td>Name</td><td><input type=text name=name></td></tr>
<tr><td>Password</td><td><input type=password name=password></td></tr>
<tr><td class=config_label>Name</td><td><input type=text name=name></td></tr>
<tr><td class=config_label>Password</td><td><input type=password name=password></td></tr>
</table>
<input type=submit value="Add user">
</form>
@@ -82,11 +82,11 @@
<form method=post action="forum/new/">
<table>
<tr>
<td>Name</td>
<td class=config_label>Name</td>
<td><input type=text name=name></td>
</tr>
<tr>
<td>Description</td>
<td class=config_label>Description</td>
<td><textarea name=description></textarea></td>
</tr>
</table>
@@ -99,27 +99,27 @@
<form action=config/edit/ method=post>
<table>
<tr>
<td>Server name</td>
<td class=config_label>Server name</td>
<td><input type=text name=server_name value="{{ config.server_name }}"></td>
</tr>
<tr>
<td>Server description</td>
<td class=config_label>Server description</td>
<td><textarea name=server_description>{{ config.server_description }}</textarea></td>
</tr>
<tr>
<td>Registration enabled</td>
<td class=config_label>Registration enabled</td>
<td><input name=registration_enabled type=checkbox {{ 'checked' if config.registration_enabled else '' }}></td>
</tr>
<tr>
<td>Login required</td>
<td class=config_label>Login required</td>
<td><input name=login_required type=checkbox {{ 'checked' if config.login_required else '' }}></td>
</tr>
<tr>
<td>Number of threads per page</td>
<td class=config_label>Number of threads per page</td>
<td><input type=number name=threads_per_page value="{{ config.threads_per_page }}"></td>
</tr>
<tr>
<td>User defined CSS file in static/ folder</td>
<td class=config_label>User defined CSS file in static/ folder</td>
<td><input type=text name=user_css value="{{ config.user_css }}"></td>
</tr>
</table>