Files
Chronos/src/Titano.App/Infrastructure/Formatting.cs
T
Claude 3241b4710d fix(rtl): the preview's frame data keeps its fields in order; charts keep European digits
The right-to-left separator broke the frame data over the preview, a left-to-right
island: after the localized time ("6:03:41 م") the shutter's number read as part
of the previous field and swapped places with the dot. Fields shown in such an
island now use Loc.IslandSeparator, with left-to-right marks in right-to-left
languages. Charts formatted text with the UI culture's digit substitution, so a
number after Arabic words came out in Arabic-Indic digits there only; they now
draw European digits like every text block. The direction marks in source are
written as escapes instead of invisible characters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 00:16:56 +00:00

67 lines
3.3 KiB
C#

using Titano.Core.Localization;
namespace Titano.App.Infrastructure;
/// <summary>Culture-aware number, size and duration formatting for the UI.</summary>
public static class Formatting
{
/// <summary>Localized "{0} KB" formats from bytes to petabytes (BRIEF §16: sizes follow the language, "96,2 Go").</summary>
private static readonly string[] ByteUnits = ["Common.Size.B", "Common.Size.KB", "Common.Size.MB", "Common.Size.GB", "Common.Size.TB", "Common.Size.PB"];
/// <summary>1,536 → "1.5 KB" (decimal units, as Explorer shows for drives).</summary>
public static string Bytes(long bytes)
{
double value = bytes;
var unit = 0;
while (value >= 1000 && unit < ByteUnits.Length - 1)
{
value /= 1000;
unit++;
}
var format = unit == 0 || value >= 100 ? "0" : "0.#";
return Loc.L(ByteUnits[unit], value.ToString(format, Loc.Culture));
}
public static string Count(long n) => n.ToString("N0", Loc.Culture);
/// <summary>Compact duration: "2 h 00 m", "5 min", "40 s".</summary>
public static string Duration(TimeSpan t)
{
if (t < TimeSpan.Zero) t = TimeSpan.Zero;
if (t.TotalHours >= 24) return Loc.L("Common.Duration.Days", (int)t.TotalDays, t.Hours);
if (t.TotalHours >= 1) return Loc.L("Common.Duration.Hours", (int)t.TotalHours, t.Minutes.ToString("00", Loc.Culture));
if (t.TotalMinutes >= 1) return Loc.L("Common.Duration.Minutes", (int)Math.Round(t.TotalMinutes));
return Loc.L("Common.Duration.Seconds", (int)Math.Round(t.TotalSeconds));
}
/// <summary>Clock-style elapsed time for finished jobs: "2:41", "1:02:41".</summary>
public static string Elapsed(TimeSpan t)
{
if (t < TimeSpan.Zero) t = TimeSpan.Zero;
return t.TotalHours >= 1
? ((int)t.TotalHours).ToString(Loc.Culture) + ":" + t.Minutes.ToString("00", Loc.Culture) + ":" + t.Seconds.ToString("00", Loc.Culture)
: ((int)t.TotalMinutes).ToString(Loc.Culture) + ":" + t.Seconds.ToString("00", Loc.Culture);
}
/// <summary>0.62 → "62 %".</summary>
public static string Percent(double fraction) => Loc.L("Common.Percent", Math.Round(Math.Clamp(fraction, 0, 1) * 100).ToString("0", Loc.Culture));
/// <summary>Items per second with one decimal below 100: "11.3", "118".</summary>
public static string Rate(double perSecond) => perSecond.ToString(perSecond < 100 ? "0.#" : "N0", Loc.Culture);
/// <summary>Local time of day in the culture's short format ("09:12").</summary>
public static string TimeOfDay(DateTimeOffset at) => at.ToLocalTime().ToString("t", Loc.Culture);
/// <summary>Calendar date in the culture's short format.</summary>
public static string Date(DateTimeOffset at) => at.ToLocalTime().ToString("d", Loc.Culture);
/// <summary>Camera wall-clock date in the culture's short format.</summary>
public static string Date(DateTime wallClock) => wallClock.ToString("d", Loc.Culture);
/// <summary>
/// A path, address or fingerprint kept left to right inside right-to-left text (a Unicode left-to-right embedding),
/// so its separators and digits stay in place; it reads unchanged in left-to-right languages.
/// </summary>
public static string LeftToRight(string text) => "\u202A" + text + "\u202C";
}