Skip to content

Commit

Permalink
Provide "DateTimeKind" when creating any DateTime objects (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb authored Oct 7, 2024
1 parent 048729e commit ff6fbc6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Axuno.Tools/ExpiringAesEncryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public string Key
}

/// <summary>
/// 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 <see cref="SymmetricAlgorithm.IV"/> initialization vector (UTF8 is used. Characters exceeding 8 bytes are truncated, less will be filled.)
/// </summary>
public string IV
{
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 16 additions & 16 deletions Axuno.Tools/GermanHolidays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -42,39 +42,39 @@ 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));
Add(new GermanHoliday(Id.PfingstSonntag, Type.Public, "Pfingstsonntag", () => _easterSunday.AddDays(49)));
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)))
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand All @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion League/ApiControllers/Cron.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion League/Controllers/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion League/Controllers/Manage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion League/Models/UploadViewModels/AbstractStaticFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit ff6fbc6

Please sign in to comment.