Skip to content

Commit 5c76d47

Browse files
refactor: extract FormatAge into ModelFormatting, removing duplicate logic (#154)
GatewayNodeInfo and SessionInfo both implemented identical age-formatting logic (just now / Xm ago / Xh ago / Xd ago). Consolidate into a single ModelFormatting.FormatAge(DateTime) helper. - Add ModelFormatting.FormatAge(DateTime timestampUtc) - SessionInfo.AgeText now delegates to ModelFormatting.FormatAge - GatewayNodeInfo.FormatAge now delegates to ModelFormatting.FormatAge - No behaviour change; all existing tests pass (525 Shared, 99 Tray) Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]>
1 parent 2be59b9 commit 5c76d47

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/OpenClaw.Shared/Models.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,7 @@ public string RichDisplayText
230230
}
231231
}
232232

233-
public string AgeText
234-
{
235-
get
236-
{
237-
var stamp = UpdatedAt ?? LastSeen;
238-
var delta = DateTime.UtcNow - stamp;
239-
if (delta.TotalSeconds < 60) return "just now";
240-
if (delta.TotalMinutes < 60) return $"{(int)Math.Round(delta.TotalMinutes)}m ago";
241-
if (delta.TotalHours < 48) return $"{(int)Math.Round(delta.TotalHours)}h ago";
242-
return $"{(int)Math.Round(delta.TotalDays)}d ago";
243-
}
244-
}
233+
public string AgeText => ModelFormatting.FormatAge(UpdatedAt ?? LastSeen);
245234

246235
public string ContextSummaryShort
247236
{
@@ -439,19 +428,24 @@ public string DetailText
439428
}
440429
}
441430

442-
private static string FormatAge(DateTime timestampUtc)
431+
private static string FormatAge(DateTime timestampUtc) => ModelFormatting.FormatAge(timestampUtc);
432+
}
433+
434+
/// <summary>Shared display-formatting helpers used by model classes.</summary>
435+
internal static class ModelFormatting
436+
{
437+
/// <summary>
438+
/// Formats a UTC timestamp as a human-readable age string (e.g. "just now", "5m ago", "2h ago", "3d ago").
439+
/// </summary>
440+
internal static string FormatAge(DateTime timestampUtc)
443441
{
444442
var delta = DateTime.UtcNow - timestampUtc;
445443
if (delta.TotalSeconds < 60) return "just now";
446444
if (delta.TotalMinutes < 60) return $"{(int)Math.Round(delta.TotalMinutes)}m ago";
447445
if (delta.TotalHours < 48) return $"{(int)Math.Round(delta.TotalHours)}h ago";
448446
return $"{(int)Math.Round(delta.TotalDays)}d ago";
449447
}
450-
}
451448

452-
/// <summary>Shared display-formatting helpers used by model classes.</summary>
453-
internal static class ModelFormatting
454-
{
455449
/// <summary>
456450
/// Formats a large integer with K/M suffix for compact display (e.g. 1500 → "1.5K", 2_000_000 → "2.0M").
457451
/// </summary>

0 commit comments

Comments
 (0)