From 14e4aa46e924ce9bc622d5886f0135cddc5ed437 Mon Sep 17 00:00:00 2001 From: axunonb Date: Mon, 7 Oct 2024 09:04:08 +0200 Subject: [PATCH] Provide "DateTimeKind" when creating any DateTime objects --- Axuno.Tools/ExpiringAesEncryptor.cs | 6 ++-- Axuno.Tools/GermanHolidays.cs | 32 +++++++++---------- League/ApiControllers/Cron.cs | 2 +- League/Controllers/Account.cs | 2 +- League/Controllers/Manage.cs | 2 +- .../UploadViewModels/AbstractStaticFile.cs | 2 +- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Axuno.Tools/ExpiringAesEncryptor.cs b/Axuno.Tools/ExpiringAesEncryptor.cs index f69aec2..1733e64 100644 --- a/Axuno.Tools/ExpiringAesEncryptor.cs +++ b/Axuno.Tools/ExpiringAesEncryptor.cs @@ -137,7 +137,7 @@ public string Key } /// - /// Gets or set the System.Security.Cryptography.SymmetricAlgorithm.IV initalization vector (UTF8 is used. Characters exceeding 8 bytes are truncated, less will be filled.) + /// Gets or set the initialization vector (UTF8 is used. Characters exceeding 8 bytes are truncated, less will be filled.) /// public string IV { @@ -228,9 +228,9 @@ public DecryptionResult Decrypt(string text, T container) var longAsBytesLength = BitConverter.GetBytes((long) 0).Length; // first 8 bytes are the startOn ticks - result.StartsOn = new DateTime(BitConverter.ToInt64(output, 0)); + result.StartsOn = new DateTime(BitConverter.ToInt64(output, 0), DateTimeKind.Unspecified); // next 8 bytes are the expiresOn ticks - result.ExpiresOn = new DateTime(BitConverter.ToInt64(output, longAsBytesLength)); + result.ExpiresOn = new DateTime(BitConverter.ToInt64(output, longAsBytesLength), DateTimeKind.Unspecified); // The string starts after byte 16 result.RawText = _utf8Encoding.GetString(output, longAsBytesLength * 2, output.Length - longAsBytesLength * 2); diff --git a/Axuno.Tools/GermanHolidays.cs b/Axuno.Tools/GermanHolidays.cs index c1e9fd2..36db419 100644 --- a/Axuno.Tools/GermanHolidays.cs +++ b/Axuno.Tools/GermanHolidays.cs @@ -28,9 +28,9 @@ public void GenerateGermanHolidays() { _easterSunday = GetEasterSunday(); - Add(new GermanHoliday(Id.Neujahr, Type.Public, "Neujahr", () => new DateTime(Year, 1, 1))); + Add(new GermanHoliday(Id.Neujahr, Type.Public, "Neujahr", () => new DateTime(Year, 1, 10, 0, 0, 0, DateTimeKind.Unspecified))); Add(new GermanHoliday(Id.HeiligeDreiKoenige, Type.Public, "Heilige Drei Könige", - () => new DateTime(Year, 1, 6))); + () => new DateTime(Year, 1, 6, 0, 0, 0, DateTimeKind.Unspecified))); Add(new GermanHoliday(Id.RosenMontag, Type.Commemoration, "Rosenmontag", () => _easterSunday.AddDays(-48))); Add(new GermanHoliday(Id.FaschingsDienstag, Type.Commemoration, "Faschingsdienstag", () => _easterSunday.AddDays(-47))); @@ -42,7 +42,7 @@ public void GenerateGermanHolidays() Add(new GermanHoliday(Id.KarSamstag, Type.Commemoration, "Karsamstag", () => _easterSunday.AddDays(-1))); Add(new GermanHoliday(Id.OsterSonntag, Type.Public, "Ostersonntag", () => _easterSunday)); Add(new GermanHoliday(Id.OsterMontag, Type.Public, "Ostermontag", () => _easterSunday.AddDays(1))); - Add(new GermanHoliday(Id.Maifeiertag, Type.Public, "Maifeiertag", () => new DateTime(Year, 5, 1))); + Add(new GermanHoliday(Id.Maifeiertag, Type.Public, "Maifeiertag", () => new DateTime(Year, 5, 1, 0, 0, 0, DateTimeKind.Unspecified))); Add(new GermanHoliday(Id.ChristiHimmelfahrt, Type.Public, "Christi Himmelfahrt", () => _easterSunday.AddDays(39))); Add(new GermanHoliday(Id.Muttertag, Type.Commemoration, "Muttertag", GetMuttertag)); @@ -50,31 +50,31 @@ public void GenerateGermanHolidays() Add(new GermanHoliday(Id.PfingstMontag, Type.Public, "Pfingstmontag", () => _easterSunday.AddDays(50))); Add(new GermanHoliday(Id.Fronleichnam, Type.Public, "Fronleichnam", () => _easterSunday.AddDays(60))); Add(new GermanHoliday(Id.MariaHimmelfahrt, Type.Public, "Maria Himmelfahrt", - () => new DateTime(Year, 8, 15))); + () => new DateTime(Year, 8, 15, 0, 0, 0, DateTimeKind.Unspecified))); Add(new GermanHoliday(Id.AugsburgerFriedensfest, Type.Commemoration, "Augsburger Friedensfest", - () => new DateTime(Year, 8, 8))); + () => new DateTime(Year, 8, 8, 0, 0, 0, DateTimeKind.Unspecified))); Add(new GermanHoliday(Id.TagDerDeutschenEinheit, Type.Public, "Tag der deutschen Einheit", - () => new DateTime(Year, 10, 3))); + () => new DateTime(Year, 10, 3, 0, 0, 0, DateTimeKind.Unspecified))); Add(new GermanHoliday(Id.Reformationstag, Type.Public, "Reformationstag", - () => new DateTime(Year, 10, 31))); - Add(new GermanHoliday(Id.Allerheiligen, Type.Public, "Allerheiligen", () => new DateTime(Year, 11, 1))); + () => new DateTime(Year, 10, 31, 0, 0, 0, DateTimeKind.Unspecified))); + Add(new GermanHoliday(Id.Allerheiligen, Type.Public, "Allerheiligen", () => new DateTime(Year, 11, 1, 0, 0, 0, DateTimeKind.Unspecified))); Add(new GermanHoliday(Id.BussUndBettag, Type.Public, "Buß- und Bettag", GetBussUndBettag)); Add(new GermanHoliday(Id.Volkstrauertag, Type.Commemoration, "Volkstrauertag", () => GetAdventDate(1).AddDays(-14))); Add(new GermanHoliday(Id.TotenSonntag, Type.Commemoration, "Totensonntag", () => GetAdventDate(1).AddDays(-7))); - Add(new GermanHoliday(Id.Nikolaus, Type.Commemoration, "Nikolaus", () => new DateTime(Year, 12, 6))); + Add(new GermanHoliday(Id.Nikolaus, Type.Commemoration, "Nikolaus", () => new DateTime(Year, 12, 6, 0, 0, 0, DateTimeKind.Unspecified))); Add(new GermanHoliday(Id.Advent1, Type.Commemoration, "1. Advent", () => GetAdventDate(1))); Add(new GermanHoliday(Id.Advent2, Type.Commemoration, "2. Advent", () => GetAdventDate(2))); Add(new GermanHoliday(Id.Advent3, Type.Commemoration, "3. Advent", () => GetAdventDate(3))); Add(new GermanHoliday(Id.Advent4, Type.Commemoration, "4. Advent", () => GetAdventDate(4))); Add(new GermanHoliday(Id.HeiligerAbend, Type.Commemoration, "Heiliger Abend", - () => new DateTime(Year, 12, 24))); + () => new DateTime(Year, 12, 24, 0, 0, 0, DateTimeKind.Unspecified))); Add(new GermanHoliday(Id.Weihnachtsfeiertag1, Type.Public, "1. Weihnachtsfeiertag", - () => new DateTime(Year, 12, 25))); + () => new DateTime(Year, 12, 25, 0, 0, 0, DateTimeKind.Unspecified))); Add(new GermanHoliday(Id.Weihnachtsfeiertag2, Type.Public, "2. Weihnachtsfeiertag", - () => new DateTime(Year, 12, 26))); - Add(new GermanHoliday(Id.Silvester, Type.Commemoration, "Silvester", () => new DateTime(Year, 12, 31))); + () => new DateTime(Year, 12, 26, 0, 0, 0, DateTimeKind.Unspecified))); + Add(new GermanHoliday(Id.Silvester, Type.Commemoration, "Silvester", () => new DateTime(Year, 12, 31, 0, 0, 0, DateTimeKind.Unspecified))); #if DEBUG // Make sure every holiday in enum HolidayId is handled in this collection foreach (Id holidayId in Enum.GetValues(typeof(Id))) @@ -196,7 +196,7 @@ private DateTime GetEasterSunday() month = 3; } - return new DateTime(Year, month, day); + return new DateTime(Year, month, day, 0, 0, 0, DateTimeKind.Unspecified); } /// @@ -210,7 +210,7 @@ private DateTime GetAdventDate(int num) throw new InvalidOperationException("Only Advents 1 to 4 are allowed."); // 4th Advent is the latest Sunday before 25th December - var firstChristmasDay = new DateTime(Year, 12, 25); + var firstChristmasDay = new DateTime(Year, 12, 25, 0, 0, 0, DateTimeKind.Unspecified); return firstChristmasDay.DayOfWeek switch { @@ -275,7 +275,7 @@ private DateTime GetBussUndBettag() private DateTime GetMuttertag() { // second Sunday in May. If this is Whitsun, then 1 week earlier - var muttertag = new DateTime(Year, 5, 1).AddDays(7); + var muttertag = new DateTime(Year, 5, 1, 0, 0, 0, DateTimeKind.Unspecified).AddDays(7); while (muttertag.DayOfWeek != DayOfWeek.Sunday) muttertag = muttertag.AddDays(1); if (muttertag != _easterSunday.AddDays(49)) return muttertag; diff --git a/League/ApiControllers/Cron.cs b/League/ApiControllers/Cron.cs index 212e5f0..d772619 100644 --- a/League/ApiControllers/Cron.cs +++ b/League/ApiControllers/Cron.cs @@ -116,7 +116,7 @@ public IActionResult AutoMail(string key, string? referenceDate) } // Strip hours, minutes, seconds - cronDateTime = new DateTime(cronDateTime.Year, cronDateTime.Month, cronDateTime.Day); + cronDateTime = new DateTime(cronDateTime.Year, cronDateTime.Month, cronDateTime.Day, 0, 0, 0, DateTimeKind.Utc); var formattedDate = cronDateTime.ToString("d", CultureInfo.InvariantCulture); if (forceDate || !HasAlreadyRun(cronDateTime)) diff --git a/League/Controllers/Account.cs b/League/Controllers/Account.cs index 197853f..7053c11 100644 --- a/League/Controllers/Account.cs +++ b/League/Controllers/Account.cs @@ -646,7 +646,7 @@ private async Task SendCodeByEmail(ApplicationUser user, EmailPurpose purpose) string code; var deadline = DateTime.UtcNow.Add(_dataProtectionTokenProviderOptions.Value.TokenLifespan); // round down to full hours - deadline = new DateTime(deadline.Year, deadline.Month, deadline.Day, deadline.Hour, 0, 0); + deadline = new DateTime(deadline.Year, deadline.Month, deadline.Day, deadline.Hour, 0, 0, DateTimeKind.Utc); switch (purpose) { diff --git a/League/Controllers/Manage.cs b/League/Controllers/Manage.cs index c3dc5d8..1550642 100644 --- a/League/Controllers/Manage.cs +++ b/League/Controllers/Manage.cs @@ -707,7 +707,7 @@ private async Task SendEmail(ApplicationUser user, string newEmail) { var deadline = DateTime.UtcNow.Add(_dataProtectionTokenProviderOptions.Value.TokenLifespan); // round down to full hours - deadline = new DateTime(deadline.Year, deadline.Month, deadline.Day, deadline.Hour, 0, 0); + deadline = new DateTime(deadline.Year, deadline.Month, deadline.Day, deadline.Hour, 0, 0, DateTimeKind.Utc); var code = (await _userManager.GenerateChangeEmailTokenAsync(user, newEmail)).Base64UrlEncode(); _sendEmailTask.SetMessageCreator(new ChangePrimaryUserEmailCreator diff --git a/League/Models/UploadViewModels/AbstractStaticFile.cs b/League/Models/UploadViewModels/AbstractStaticFile.cs index c228309..9cf2c0c 100644 --- a/League/Models/UploadViewModels/AbstractStaticFile.cs +++ b/League/Models/UploadViewModels/AbstractStaticFile.cs @@ -108,7 +108,7 @@ protected void DeleteMostRecentFile(DirectoryInfo dirInfo, string searchPattern) return long.TryParse(fileInfo.Name[(tickIndex + DateTimeTicksMarker.Length)..], NumberStyles.Any, null, out var ticks) - ? (fileInfo.Name, new DateTime(ticks)) + ? (fileInfo.Name, new DateTime(ticks, DateTimeKind.Utc)) : (fileInfo.Name, fileInfo.LastWriteTimeUtc); } }