| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.Globalization;
- using Humanizer;
- namespace DesktopClock.Tests;
- public class TimeStringFormatterTests
- {
- [Fact]
- public void Format_UsesFormatWhenNoCountdown()
- {
- var now = new DateTimeOffset(2024, 1, 1, 10, 15, 0, TimeSpan.Zero);
- var timeZone = TimeZoneInfo.CreateCustomTimeZone("UtcPlus2", TimeSpan.FromHours(2), "UtcPlus2", "UtcPlus2");
- var result = TimeStringFormatter.Format(
- now,
- now.DateTime,
- timeZone,
- default,
- "HH:mm",
- null,
- CultureInfo.InvariantCulture);
- Assert.Equal("12:15", result);
- }
- [Fact]
- public void Format_UsesFormatProviderForStandardFormats()
- {
- var now = new DateTimeOffset(2024, 1, 2, 10, 15, 0, TimeSpan.Zero);
- var formatProvider = new CultureInfo("en-GB");
- var result = TimeStringFormatter.Format(
- now,
- now.DateTime,
- TimeZoneInfo.Utc,
- default,
- "d",
- null,
- formatProvider);
- Assert.Equal(now.ToString("d", formatProvider), result);
- }
- [Fact]
- public void Format_UsesTokenizedCountdownFormat()
- {
- var now = new DateTimeOffset(2024, 1, 1, 10, 0, 0, TimeSpan.Zero);
- var nowDateTime = now.DateTime;
- var countdownTo = nowDateTime.AddHours(1).AddMinutes(2).AddSeconds(3);
- var result = TimeStringFormatter.Format(
- now,
- nowDateTime,
- TimeZoneInfo.Utc,
- countdownTo,
- "HH:mm",
- "{hh\\:mm}",
- CultureInfo.InvariantCulture);
- Assert.Equal("01:02", result);
- }
- [Fact]
- public void Format_UsesCountdownFormatWhenProvided()
- {
- var now = new DateTimeOffset(2024, 1, 1, 10, 0, 0, TimeSpan.Zero);
- var nowDateTime = now.DateTime;
- var countdownTo = nowDateTime.AddMinutes(90);
- var formatProvider = CultureInfo.InvariantCulture;
- var result = TimeStringFormatter.Format(
- now,
- nowDateTime,
- TimeZoneInfo.Utc,
- countdownTo,
- "HH:mm",
- "c",
- formatProvider);
- Assert.Equal((countdownTo - nowDateTime).ToString("c", formatProvider), result);
- }
- [Fact]
- public void Format_UsesHumanizerWhenCountdownFormatMissing()
- {
- var originalCulture = CultureInfo.CurrentCulture;
- var originalUiCulture = CultureInfo.CurrentUICulture;
- try
- {
- var enUs = new CultureInfo("en-US");
- CultureInfo.CurrentCulture = enUs;
- CultureInfo.CurrentUICulture = enUs;
- var now = new DateTimeOffset(2024, 1, 1, 10, 0, 0, TimeSpan.Zero);
- var nowDateTime = now.DateTime;
- var countdownTo = nowDateTime.AddDays(1);
- var result = TimeStringFormatter.Format(
- now,
- nowDateTime,
- TimeZoneInfo.Utc,
- countdownTo,
- "HH:mm",
- " ",
- CultureInfo.CurrentCulture);
- Assert.Equal(countdownTo.Humanize(utcDate: false, dateToCompareAgainst: nowDateTime), result);
- }
- finally
- {
- CultureInfo.CurrentCulture = originalCulture;
- CultureInfo.CurrentUICulture = originalUiCulture;
- }
- }
- [Fact]
- public void Format_ConvertsUsingSelectedTimeZone()
- {
- var now = new DateTimeOffset(2024, 1, 1, 23, 30, 0, TimeSpan.Zero);
- var timeZone = TimeZoneInfo.CreateCustomTimeZone("UtcMinus5", TimeSpan.FromHours(-5), "UtcMinus5", "UtcMinus5");
- var result = TimeStringFormatter.Format(
- now,
- now.DateTime,
- timeZone,
- default,
- "HH:mm",
- null,
- CultureInfo.InvariantCulture);
- Assert.Equal("18:30", result);
- }
- }
|