Skip to content

Commit

Permalink
feat(gateway): localize datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
PhotonQuantum committed Aug 23, 2023
1 parent f2c449a commit b7df19f
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rsync-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ reqwest = { version = "0.11", default-features = false, features = ["json"] }
rstest = "0.18"
rsync-core = { path = "../rsync-core", features = ["tests"] }
test-strategy = "0.3"
time = "0.3"
uuid = { version = "1.4", features = ["arbitrary"] }
2 changes: 1 addition & 1 deletion rsync-gateway/src/path_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub struct ListingEntry {
#[cfg_attr(
test,
proptest(
strategy = "proptest::option::of(proptest_arbitrary_interop::arb::<DateTime<Utc>>())"
strategy = "proptest::option::of(crate::tests::datetime_strategy())"
)
)]
pub modify_time: Option<DateTime<Utc>>,
Expand Down
2 changes: 1 addition & 1 deletion rsync-gateway/src/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct RevisionStat {
pub status: RevisionStatus,
#[cfg_attr(
test,
proptest(strategy = "proptest_arbitrary_interop::arb::<DateTime<Utc>>()")
proptest(strategy = "crate::tests::datetime_strategy()")
)]
pub created_at: DateTime<Utc>,
#[cfg_attr(
Expand Down
6 changes: 3 additions & 3 deletions rsync-gateway/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ mod tests {
raw_path: String,
prefix: String,
revision: i32,
#[strategy(proptest_arbitrary_interop::arb::< DateTime < Utc >> ())] generated_at: DateTime<
#[strategy(crate::tests::datetime_strategy())] generated_at: DateTime<
Utc,
>,
query_time: Duration,
Expand Down Expand Up @@ -407,7 +407,7 @@ mod tests {
req_path: Vec<u8>,
prefix: String,
revision: Option<i32>,
#[strategy(proptest_arbitrary_interop::arb::< DateTime < Utc >> ())] generated_at: DateTime<
#[strategy(crate::tests::datetime_strategy())] generated_at: DateTime<
Utc,
>,
query_time: Duration,
Expand Down Expand Up @@ -439,7 +439,7 @@ mod tests {
async fn must_render_revision_stats_prop(
entries: Vec<RevisionStat>,
prefix: String,
#[strategy(proptest_arbitrary_interop::arb::< DateTime < Utc >> ())] generated_at: DateTime<
#[strategy(crate::tests::datetime_strategy())] generated_at: DateTime<
Utc,
>,
query_time: Duration,
Expand Down
8 changes: 6 additions & 2 deletions rsync-gateway/src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::Duration;

use bigdecimal::ToPrimitive;
use bytesize::ByteSize;
use chrono::{DateTime, Utc};
use chrono::{DateTime, SecondsFormat, Utc};
use sailfish::runtime::{Buffer, Render};
use sailfish::TemplateOnce;
use sqlx::postgres::types::PgInterval;
Expand Down Expand Up @@ -35,7 +35,11 @@ fn href_render(name: impl Render) -> impl Display {
}

fn datetime(dt: DateTime<Utc>) -> impl Display {
dt.format("%Y-%m-%d %H:%M:%S")
format!(
r#"<time datetime="{}">{}</time>"#,
dt.to_rfc3339_opts(SecondsFormat::Secs, false),
dt.format("%Y-%m-%d %H:%M:%S %Z")
)
}

fn duration(dur: Duration) -> impl Display {
Expand Down
22 changes: 22 additions & 0 deletions rsync-gateway/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
use actix_web::cookie::time;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use proptest::prop_compose;
use proptest::strategy::{Just, Strategy};
use time::util::days_in_year;

prop_compose! {
pub fn datetime_strategy()(
year in 1970..=2100,
)(
year in Just(year),
ord in 1..=days_in_year(year),
secs in 0..=86399u32,
nano in 0..=999_999_999u32 // unfortunately html time tag doesn't support leap seconds
) -> DateTime<Utc> {
let date = NaiveDate::from_yo_opt(year, u32::from(ord)).expect("valid date");
let time = NaiveTime::from_num_seconds_from_midnight_opt(secs, nano).expect("valid time");
let dt = NaiveDateTime::new(date, time);
DateTime::from_utc(dt, Utc)
}
}

mod db_required {
use std::collections::{BTreeMap, HashMap};
use std::time::UNIX_EPOCH;
Expand Down
7 changes: 2 additions & 5 deletions rsync-gateway/templates/error.stpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" rel="stylesheet">
<style>
.jumbotron {
background-color: #fff
}
</style>
<style><% include!("styles.css"); %></style>

<title><%= code %> - SJTUG Mirror Index</title>
</head>
Expand Down Expand Up @@ -48,6 +44,7 @@
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.slim.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>
<script><% include!("localize.js"); %></script>
</body>

</html>
2 changes: 1 addition & 1 deletion rsync-gateway/templates/footer.stpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p class="small text-muted">该页面由 rsync-sjtug 自动生成。<a href="https://github.com/sjtug/rsync-sjtug">rsync-sjtug</a>
是 SJTUG 用于从 rsync 上游同步到对象存储的工具。</p>
<p class="small text-muted">
最后更新于 <%= datetime(generated_at) | disp %>,查询时间 <%= duration(query_time) | disp %>
最后更新于 <%- datetime(generated_at) | disp %>,查询时间 <%= duration(query_time) | disp %>
</p>
2 changes: 1 addition & 1 deletion rsync-gateway/templates/footer_revision.stpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
是 SJTUG 用于从 rsync 上游同步到对象存储的工具。</p>
<p class="small text-muted">
<a href="<%- href_str(root_href) | disp %>/_revisions">Revision <%= revision %></a>,
最后更新于 <%= datetime(generated_at) | disp %>,查询时间 <%= duration(query_time) | disp %>
最后更新于 <%- datetime(generated_at) | disp %>,查询时间 <%= duration(query_time) | disp %>
</p>
4 changes: 3 additions & 1 deletion rsync-gateway/templates/listing.stpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" rel="stylesheet">
<style><% include!("styles.css"); %></style>

<title>Index of <%= title %> - SJTUG Mirror Index</title>
</head>
Expand Down Expand Up @@ -37,7 +38,7 @@
</td>
<td class="d-none d-sm-block text-right">
<% if let Some(modify_time) = modify_time { %>
<%= datetime(*modify_time) | disp %>
<%- datetime(*modify_time) | disp %>
<% } else { %>
-
<% } %>
Expand All @@ -48,6 +49,7 @@
</table>
<%+ footer %>
</div>
<script><% include!("localize.js"); %></script>
</body>

</html>
15 changes: 15 additions & 0 deletions rsync-gateway/templates/localize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function localizeDatetime(e, index, ar) {
if (e.textContent === undefined) {
return;
}
var d = new Date(e.getAttribute('datetime'));
if (isNaN(d)) {
d = new Date(e.textContent);
if (isNaN(d)) {
return;
}
}
e.textContent = d.toLocaleString([], {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});
}
var timeList = Array.prototype.slice.call(document.getElementsByTagName("time"));
timeList.forEach(localizeDatetime);
4 changes: 3 additions & 1 deletion rsync-gateway/templates/revision_stats.stpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" rel="stylesheet">
<style><% include!("styles.css"); %></style>

<title>Revisions of <%= prefix %> - SJTUG Mirror Index</title>
</head>
Expand All @@ -27,7 +28,7 @@
<% for RevisionStat { revision, status, created_at, elapsed, count, sum } in entries { %>
<tr>
<td><%= revision %></td>
<td><%= datetime(*created_at) | disp %></td>
<td><%- datetime(*created_at) | disp %></td>
<td>
<% if let Some(elapsed) = elapsed { %>
<%= pg_interval(elapsed) | disp %>
Expand Down Expand Up @@ -77,6 +78,7 @@
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.slim.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>
<script><% include!("localize.js"); %></script>
</body>

</html>
9 changes: 9 additions & 0 deletions rsync-gateway/templates/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body {
font-family: sans-serif;
}
time {
display: inline-block;
}
.jumbotron {
background-color: #fff
}

0 comments on commit b7df19f

Please sign in to comment.