using System.Globalization; using Anubis.Baskets; using Anubis.Data; namespace Anubis.Tests; /// /// The server clock of the research document (Phase 12, issue #119): New York time plus seven hours, the server day, the daily /// pause, the hourly window and the weekend rule. Four dates cover winter, summer and the weeks in which the United States /// and Europe change the clock on different days (2026: US 8 March and 1 November, Europe 29 March and 25 October). /// [TestClass] public sealed class ServerClockTests { [TestMethod] [TestCategory(Category.Unit)] [DataRow("2026-01-15", 2, "2026-01-15T08:00:00Z", "2026-01-14T22:00:00Z")] [DataRow("2026-03-16", 3, "2026-03-16T07:00:00Z", "2026-03-15T21:00:00Z")] [DataRow("2026-07-15", 3, "2026-07-15T07:00:00Z", "2026-07-14T21:00:00Z")] [DataRow("2026-10-27", 3, "2026-10-27T07:00:00Z", "2026-10-26T21:00:00Z")] public void TheServerClockIsNewYorkPlusSevenHoursOnFourDates(string day, int offsetHours, string tenServerUtc, string midnightUtc) { var date = DateOnly.Parse(day, CultureInfo.InvariantCulture); var ten = Utc(tenServerUtc); var midnight = Utc(midnightUtc); Assert.AreEqual(ten, ServerClock.At(date, new TimeOnly(10, 0))); Assert.AreEqual(midnight, ServerClock.DayStart(date)); Assert.AreEqual(TimeSpan.FromHours(offsetHours), ServerClock.OffsetFromUtc(ten)); Assert.AreEqual(date.ToDateTime(new TimeOnly(10, 0)), ServerClock.ToServer(ten)); // The server day starts at server midnight, not a tick earlier. Assert.AreEqual(date, ServerClock.Day(midnight)); Assert.AreEqual(date.AddDays(-1), ServerClock.Day(midnight.AddTicks(-1))); Assert.AreEqual(date, ServerClock.Day(ten)); } [TestMethod] [TestCategory(Category.Unit)] [DataRow("2026-01-15")] [DataRow("2026-03-16")] [DataRow("2026-07-15")] [DataRow("2026-10-27")] public void TheServerDayChangesAtFivePmNewYork(string day) { var date = DateOnly.Parse(day, CultureInfo.InvariantCulture); var five = Zones.At(date, new TimeOnly(17, 0), Zones.NewYork); Assert.AreEqual(date, ServerClock.Day(five.AddSeconds(-1))); Assert.AreEqual(date.AddDays(1), ServerClock.Day(five)); Assert.AreEqual(ServerClock.DayStart(date.AddDays(1)), five); } [TestMethod] [TestCategory(Category.Unit)] [DataRow("2026-01-15", "2026-01-15T21:55:00Z", "2026-01-15T23:10:00Z")] [DataRow("2026-03-16", "2026-03-16T20:55:00Z", "2026-03-16T22:10:00Z")] [DataRow("2026-07-15", "2026-07-15T20:55:00Z", "2026-07-15T22:10:00Z")] [DataRow("2026-10-27", "2026-10-27T20:55:00Z", "2026-10-27T22:10:00Z")] public void TheDailyPauseRunsFrom1655To1810NewYorkHalfOpen(string day, string fromUtc, string toUtc) { var from = Utc(fromUtc); var to = Utc(toUtc); Assert.AreEqual(Zones.At(DateOnly.Parse(day, CultureInfo.InvariantCulture), new TimeOnly(16, 55), Zones.NewYork), from); Assert.IsFalse(ServerClock.InDailyPause(from.AddSeconds(-1))); Assert.IsTrue(ServerClock.InDailyPause(from)); Assert.IsTrue(ServerClock.InDailyPause(from.AddMinutes(30))); Assert.IsTrue(ServerClock.InDailyPause(to.AddSeconds(-1))); Assert.IsFalse(ServerClock.InDailyPause(to)); // The pause crosses server midnight; no entries inside it, whatever else allows them. Assert.IsTrue(ServerClock.InDailyPause(ServerClock.DayStart(DateOnly.Parse(day, CultureInfo.InvariantCulture).AddDays(1)))); Assert.IsFalse(ServerClock.EntriesAllowed(from, sessionOn: false)); Assert.IsTrue(ServerClock.EntriesAllowed(to, sessionOn: false)); } [TestMethod] [TestCategory(Category.Unit)] [DataRow("2026-01-16T14:00:00Z", "2026-01-19T00:00:00Z")] [DataRow("2026-07-17T13:00:00Z", "2026-07-19T23:00:00Z")] public void TheWeekendRuleBlocksFromFriday1600ToMonday0200ServerTimeHalfOpen(string fridayUtc, string mondayUtc) { var friday = Utc(fridayUtc); var monday = Utc(mondayUtc); Assert.AreEqual(DayOfWeek.Friday, ServerClock.ToServer(friday).DayOfWeek); Assert.AreEqual(new TimeOnly(16, 0), TimeOnly.FromDateTime(ServerClock.ToServer(friday))); Assert.AreEqual(DayOfWeek.Monday, ServerClock.ToServer(monday).DayOfWeek); Assert.AreEqual(new TimeOnly(2, 0), TimeOnly.FromDateTime(ServerClock.ToServer(monday))); Assert.IsFalse(ServerClock.InWeekend(friday.AddSeconds(-1))); Assert.IsTrue(ServerClock.InWeekend(friday)); Assert.IsTrue(ServerClock.InWeekend(friday.AddDays(1))); Assert.IsTrue(ServerClock.InWeekend(friday.AddDays(2))); Assert.IsTrue(ServerClock.InWeekend(monday.AddSeconds(-1))); Assert.IsFalse(ServerClock.InWeekend(monday)); Assert.IsFalse(ServerClock.InWeekend(monday.AddDays(3))); Assert.IsFalse(ServerClock.EntriesAllowed(friday, sessionOn: false)); Assert.IsTrue(ServerClock.EntriesAllowed(monday, sessionOn: false)); } [TestMethod] [TestCategory(Category.Unit)] [DataRow("2026-01-15")] [DataRow("2026-03-16")] [DataRow("2026-07-15")] [DataRow("2026-10-27")] public void TheHourlyWindowRunsFrom1000To2000ServerTimeHalfOpen(string day) { var date = DateOnly.Parse(day, CultureInfo.InvariantCulture); var ten = ServerClock.At(date, new TimeOnly(10, 0)); var eight = ServerClock.At(date, new TimeOnly(20, 0)); Assert.IsFalse(ServerClock.InSession(ten.AddSeconds(-1))); Assert.IsTrue(ServerClock.InSession(ten)); Assert.IsTrue(ServerClock.InSession(eight.AddSeconds(-1))); Assert.IsFalse(ServerClock.InSession(eight)); // The window only matters when it is switched on. Assert.IsFalse(ServerClock.EntriesAllowed(eight, sessionOn: true)); Assert.IsTrue(ServerClock.EntriesAllowed(eight, sessionOn: false)); Assert.IsTrue(ServerClock.EntriesAllowed(ten, sessionOn: true)); } [TestMethod] [TestCategory(Category.Unit)] public void AfterTheEquityStopEntriesResumeAtTheNextServerDay() { // Thursday 15 January 2026, 15:00 New York = 22:00 server: the next server day starts at 17:00 New York. var stop = new DateTimeOffset(2026, 1, 15, 20, 0, 0, TimeSpan.Zero); Assert.AreEqual(new DateTimeOffset(2026, 1, 15, 22, 0, 0, TimeSpan.Zero), ServerClock.NextDayStart(stop)); // One minute after the change the next day is a whole day later. var after = new DateTimeOffset(2026, 1, 15, 22, 1, 0, TimeSpan.Zero); Assert.AreEqual(new DateTimeOffset(2026, 1, 16, 22, 0, 0, TimeSpan.Zero), ServerClock.NextDayStart(after)); } [TestMethod] [TestCategory(Category.Unit)] public void AServerTimeTurnsBackIntoTheSameMoment() { var gen = new Gen(Property.DefaultSeed); var start = new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero); for (var i = 0; i < 500; i++) { var moment = start.AddMinutes(gen.Int(0, 3 * 365 * 24 * 60)); var server = ServerClock.ToServer(moment); // Away from the hour in which New York's clock goes back, a server time names one moment only. if (server.DayOfWeek == DayOfWeek.Sunday && server.Hour is >= 7 and < 10) { continue; } Assert.AreEqual(moment, ServerClock.FromServer(server), moment.ToString("O", CultureInfo.InvariantCulture)); } } private static DateTimeOffset Utc(string text) => DateTimeOffset.Parse(text, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); }