Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix human-friendly duration formatting #51

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions taxy-webui/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use fancy_duration::FancyDuration;
use fancy_duration::{DurationPart, FancyDuration};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use wasm_bindgen::UnwrapThrowExt;
use web_time::{Duration, SystemTime, UNIX_EPOCH};

pub fn format_time(unix_time: i64) -> String {
pub fn format_duration(unix_time: i64) -> String {
let time = OffsetDateTime::from_unix_timestamp(unix_time).unwrap_throw();
let timestamp = time.format(&Rfc3339).unwrap_throw();
let date = timestamp.split('T').next().unwrap_throw().to_string();
Expand All @@ -13,8 +13,16 @@ pub fn format_time(unix_time: i64) -> String {
.unwrap_throw();

let duration = if let Ok(duration) = time.duration_since(SystemTime::now()) {
let duration = FancyDuration::new(duration).to_string();
duration.split(' ').next().unwrap_throw().to_string()
FancyDuration::new(duration)
.filter(&[
DurationPart::Years,
DurationPart::Months,
DurationPart::Weeks,
DurationPart::Days,
DurationPart::Hours,
])
.truncate(1)
.to_string()
} else {
"Expired".to_string()
};
Expand Down
8 changes: 4 additions & 4 deletions taxy-webui/src/pages/cert_list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::auth::use_ensure_auth;
use crate::format::format_time;
use crate::format::format_duration;
use crate::pages::Route;
use crate::store::{AcmeStore, CertStore};
use crate::API_ENDPOINT;
Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn cert_list() -> Html {
{entry.id.to_string()}
</td>
<td class="px-4 py-4">
{format_time(entry.not_after)}
{format_duration(entry.not_after)}
</td>
<td class="px-4 py-4 w-0 whitespace-nowrap" align="right">
<a class="cursor-pointer font-medium text-blue-600 dark:text-blue-400 hover:underline mr-5" onclick={download_onclick}>{"Download"}</a>
Expand Down Expand Up @@ -289,7 +289,7 @@ pub fn cert_list() -> Html {
}
</td>
<td class="px-4 py-4">
{format_time(entry.not_after)}
{format_duration(entry.not_after)}
</td>
<td class="px-4 py-4 w-0 whitespace-nowrap" align="right">
<a class="cursor-pointer font-medium text-blue-600 dark:text-blue-400 hover:underline mr-5" onclick={download_onclick}>{"Download"}</a>
Expand Down Expand Up @@ -371,7 +371,7 @@ pub fn cert_list() -> Html {
</td>
<td class="px-4 py-4">
if let Some(time) = entry.next_renewal {
{ format_time(time) }
{ format_duration(time) }
}
</td>
<td class="px-4 py-4 w-0 whitespace-nowrap" align="center">
Expand Down
Loading