Skip to content
Closed
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
23 changes: 23 additions & 0 deletions DesktopClock.Tests/TimeStringFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ public void Format_UsesCountdownFormatWhenProvided()
Assert.Equal((countdownTo - nowDateTime).ToString("c", formatProvider), result);
}

[Theory]
[InlineData("{hh\\:mm\\:ss}", "-01:30:00")] // Tokenized custom format
[InlineData("{%h}h {%m}m", "-1h 30m")] // Tokenized preset style
[InlineData("c", "-01:30:00")] // Standard format keeps a single sign
[InlineData("{bad}", "Bad format")] // Errors are not prefixed
public void Format_PastCountdownShowsNegativeSign(string countdownFormat, string expected)
{
var now = new DateTimeOffset(2024, 1, 1, 10, 0, 0, TimeSpan.Zero);
var nowDateTime = now.DateTime;
var countdownTo = nowDateTime.AddMinutes(-90);

var result = TimeStringFormatter.Format(
now,
nowDateTime,
TimeZoneInfo.Utc,
countdownTo,
"HH:mm",
countdownFormat,
CultureInfo.InvariantCulture);

Assert.Equal(expected, result);
}

[Fact]
public void Format_UsesHumanizerWhenCountdownFormatMissing()
{
Expand Down
8 changes: 7 additions & 1 deletion DesktopClock/Utilities/TimeStringFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public static string Format(
else
{
var countdown = countdownTo - nowDateTime;
result = Tokenizer.FormatWithTokenizerOrFallBack(countdown, countdownFormat, formatProvider);

// Custom TimeSpan formats drop the sign, so format the magnitude and add it back to mark an elapsed target.
var isElapsed = countdown < TimeSpan.Zero;
result = Tokenizer.FormatWithTokenizerOrFallBack(isElapsed ? countdown.Negate() : countdown, countdownFormat, formatProvider);

if (isElapsed && result != Tokenizer.FormatErrorMessage)
result = "-" + result;
}

return result;
Expand Down