fix(qt): format the blacklist Added date and the What's New release date in the user's locale

Both were rendered from a hardcoded English month table in Rust, invisible
to i18n. Rust now hands QML the raw timestamp / civil date and QML formats
it with Qt.formatDate(..., Locale.LongFormat) like the purchase views
already do.

cc
This commit is contained in:
vicrodh committed 2026-09-13 03:48:02 -06:00
1 parent 196887534a
commit 083e6ea172
4 files changed
+34 -115

No files matched your search

+5 -1
View File
@@ -186,8 +186,12 @@ Rectangle {
Text {
visible: root.arm !== "dismissed"
width: parent.width
// Unix seconds from the store, formatted in the user's
// locale here (the Rust side carries no month names).
text: QbzSession.tr("Added {}", QbzSession.trRev)
.replace("{}", root.row.addedDisplay || "")
.replace("{}", (root.row.addedAt || 0) > 0
? Qt.formatDate(new Date(root.row.addedAt * 1000), Locale.LongFormat)
: "")
color: theme.textMuted
font.pixelSize: root.kioskHost
? theme.fontLegal * 1.2 : theme.fontLegal
@@ -132,6 +132,12 @@ Item {
if (v === "")
return QbzSession.tr("What's new", QbzSession.trRev)
var d = root.doc.date || ""
// `date` is the release's civil date as written
// (YYYY-MM-DD); render it in the user's locale.
var m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(d)
if (m)
d = Qt.formatDate(new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3])),
Locale.LongFormat)
return QbzSession.tr("What's new in v{}", QbzSession.trRev).replace("{}", v)
+ (d === "" ? "" : " (" + d + ")")
}
-78
View File
@@ -77,8 +77,6 @@ struct ArtistRowDoc {
name: String,
#[serde(rename = "addedAt")]
added_at: i64,
#[serde(rename = "addedDisplay")]
added_display: String,
notes: String,
#[serde(rename = "hasNotes")]
has_notes: bool,
@@ -97,8 +95,6 @@ struct AlbumRowDoc {
art_path: String,
#[serde(rename = "addedAt")]
added_at: i64,
#[serde(rename = "addedDisplay")]
added_display: String,
/// Carried for shape parity with the artist row; the Slint `AlbumRow`
/// renders `added-display` in that slot and never the notes.
notes: String,
@@ -159,47 +155,6 @@ fn set_query_value(q: String) {
}
}
// ---------------------------------------------------------------------------
// Date formatting — "%b %-d, %Y" without pulling `chrono` into this crate
// ---------------------------------------------------------------------------
const MONTHS: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
/// Format a unix-SECONDS timestamp as `"MMM D, YYYY"` (English — `qbz_i18n` has
/// no Rust-side date catalog, same as the reference). Byte-identical to the
/// reference's `chrono` `"%b %-d, %Y"` on a UTC timestamp; `qbz-qt` has no
/// `chrono` dependency, so the civil-date conversion is inlined below rather
/// than adding one for one string.
fn format_added(secs: i64) -> String {
let days = secs.div_euclid(86_400);
// The reference's `DateTime::from_timestamp` yields `None` outside its
// range and renders "". Guard the same way for a corrupt row.
if !(-100_000_000..=100_000_000).contains(&days) {
return String::new();
}
let (year, month, day) = civil_from_days(days);
format!("{} {}, {}", MONTHS[(month - 1) as usize], day, year)
}
/// Howard Hinnant's `civil_from_days`: days since 1970-01-01 -> (year, month,
/// day), pure integer arithmetic, valid for the whole proleptic Gregorian
/// range. Truncating division with the era adjustment is the algorithm as
/// published — do not "simplify" it to `div_euclid`.
fn civil_from_days(days: i64) -> (i64, u32, u32) {
let z = days + 719_468;
let era = (if z >= 0 { z } else { z - 146_096 }) / 146_097;
let doe = z - era * 146_097; // [0, 146096]
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; // [0, 399]
let y = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
let mp = (5 * doy + 2) / 153; // [0, 11]
let d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
let m = if mp < 10 { mp + 3 } else { mp - 9 }; // [1, 12]
(y + i64::from(m <= 2), m as u32, d as u32)
}
// ---------------------------------------------------------------------------
// Builders — filtering is entirely Rust-side; QML never filters
// ---------------------------------------------------------------------------
@@ -221,7 +176,6 @@ fn build_artists() -> (Vec<ArtistRowDoc>, i32) {
id: a.artist_id.to_string(),
name: a.artist_name,
added_at: a.added_at,
added_display: format_added(a.added_at),
has_notes: !notes.is_empty(),
notes,
}
@@ -265,7 +219,6 @@ fn build_albums() -> (Vec<AlbumRowDoc>, i32, Vec<String>) {
cover_url: a.cover_url,
art_path,
added_at: a.added_at,
added_display: format_added(a.added_at),
has_notes: !notes.is_empty(),
notes,
}
@@ -779,34 +732,3 @@ fn emit_artwork(key: String, path: String) {
.library_artwork_ready(QString::from(key.as_str()), QString::from(path.as_str()));
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn format_added_matches_chrono_b_d_y() {
// Epoch itself.
assert_eq!(format_added(0), "Jan 1, 1970");
// No zero padding on the day (%-d), month abbreviated (%b).
assert_eq!(format_added(1_751_500_000), "Jul 2, 2025");
// Leap day.
assert_eq!(format_added(1_709_164_800), "Feb 29, 2024");
// A December date, i.e. the mp >= 10 branch of civil_from_days.
assert_eq!(format_added(1_735_689_599), "Dec 31, 2024");
// Pre-epoch (floor division, not truncation).
assert_eq!(format_added(-1), "Dec 31, 1969");
// A corrupt value renders empty rather than a bogus date.
assert_eq!(format_added(i64::MAX), "");
}
#[test]
fn civil_from_days_month_boundaries() {
assert_eq!(civil_from_days(0), (1970, 1, 1));
// 1972-01-01 is day 730 (two 365-day years); +59 lands on the first
// leap day after the epoch, +60 on the day after it.
assert_eq!(civil_from_days(730), (1972, 1, 1));
assert_eq!(civil_from_days(730 + 59), (1972, 2, 29));
assert_eq!(civil_from_days(730 + 60), (1972, 3, 1));
}
}
+23 -36
View File
@@ -34,7 +34,7 @@
//!
//! # Divergences from the reference
//!
//! - `format_release_date` is rewritten WITHOUT chrono (`qbz-qt` has no chrono
//! - `release_date_iso` keeps only the civil date; QML formats it (`qbz-qt` has no chrono
//! dependency and is not gaining one for a date the GitHub API always emits
//! in a fixed RFC3339 shape). Same output, same raw-string fallback.
//! - The blocks are plain `Serialize` structs instead of Slint model rows; the
@@ -231,7 +231,7 @@ async fn fetch_release_for_version(version: &str) -> Option<FetchedRelease> {
Some(FetchedRelease {
version: normalize_version_tag(&release.tag_name),
date: format_release_date(&release.published_at),
date: release_date_iso(&release.published_at),
body: release.body,
})
}
@@ -240,34 +240,25 @@ fn normalize_version_tag(tag: &str) -> String {
tag.trim().trim_start_matches('v').to_string()
}
const MONTHS: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
/// Format an RFC3339 timestamp as "Mon D, YYYY" (en-US short). Falls back to
/// the raw string on anything malformed.
///
/// CHRONO-FREE (the reference uses `DateTime::parse_from_rfc3339`, and this
/// crate has no chrono dependency). GitHub's `published_at` is always
/// `YYYY-MM-DDTHH:MM:SSZ`, and chrono's `year()/month0()/day()` on a parsed
/// `DateTime<FixedOffset>` report the date AS WRITTEN in the string — so
/// slicing the first ten ASCII bytes is behaviour-identical, offset included.
fn format_release_date(iso: &str) -> String {
/// Reduce GitHub's RFC3339 `published_at` to its civil date AS WRITTEN
/// (`YYYY-MM-DD`, offset ignored — the reference's chrono
/// `year()/month0()/day()` on a parsed `DateTime<FixedOffset>` did the same).
/// Display formatting happens in QML with `Qt.formatDate(..., Locale.LongFormat)`
/// so the header follows the user's locale instead of an English month table.
/// Malformed input is echoed verbatim.
fn release_date_iso(iso: &str) -> String {
let bytes = iso.as_bytes();
if bytes.len() < 10 || bytes[4] != b'-' || bytes[7] != b'-' {
return iso.to_string();
}
let num = |a: usize, b: usize| iso[a..b].parse::<u32>().ok();
let (Some(year), Some(month), Some(day)) = (num(0, 4), num(5, 7), num(8, 10)) else {
let (Some(_year), Some(month), Some(day)) = (num(0, 4), num(5, 7), num(8, 10)) else {
return iso.to_string();
};
let Some(month_name) = month
.checked_sub(1)
.and_then(|m| MONTHS.get(m as usize).copied())
else {
if !(1..=12).contains(&month) || !(1..=31).contains(&day) {
return iso.to_string();
};
format!("{month_name} {day}, {year}")
}
iso[..10].to_string()
}
// ==================== Markdown → blocks + TOC ====================
@@ -603,21 +594,17 @@ mod tests {
}
#[test]
fn release_dates_format_like_the_reference_and_fall_back_raw() {
assert_eq!(format_release_date("2026-07-04T18:22:01Z"), "Jul 4, 2026");
assert_eq!(
format_release_date("2026-12-31T00:00:00+02:00"),
"Dec 31, 2026"
);
assert_eq!(format_release_date("2026-01-09T00:00:00Z"), "Jan 9, 2026");
fn release_dates_reduce_to_their_civil_date_and_fall_back_raw() {
assert_eq!(release_date_iso("2026-07-04T18:22:01Z"), "2026-07-04");
// The date AS WRITTEN, offset ignored (chrono's year()/month0()/day()
// on a parsed DateTime<FixedOffset> behave the same).
assert_eq!(release_date_iso("2026-12-31T00:00:00+02:00"), "2026-12-31");
assert_eq!(release_date_iso("2026-01-09T00:00:00Z"), "2026-01-09");
// Malformed input is echoed verbatim, never blanked.
assert_eq!(format_release_date("not a date"), "not a date");
assert_eq!(format_release_date("2026/07/04"), "2026/07/04");
assert_eq!(
format_release_date("2026-13-04T00:00:00Z"),
"2026-13-04T00:00:00Z"
);
assert_eq!(format_release_date(""), "");
assert_eq!(release_date_iso("not a date"), "not a date");
assert_eq!(release_date_iso("2026/07/04"), "2026/07/04");
assert_eq!(release_date_iso("2026-13-04T00:00:00Z"), "2026-13-04T00:00:00Z");
assert_eq!(release_date_iso(""), "");
}
#[test]