-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add validation, that IdentificationDateTime
is German midnight
#42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5f462af
feat: Add validation, that `IdentificationDateTime` is German midnight
43b3826
remove assertion
6900e71
Update MaLoIdentModels/MaLoIdentModels/Validation/GermanMidnightValid…
hf-kklein d50450e
remove assertions entirely
c2af168
Merge remote-tracking branch 'origin/validation' into validation
3575c1c
Merge branch 'main' into validation
hf-kklein 7c818eb
fix build error
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
MaLoIdentModels/MaLoIdentModels/Validation/GermanMidnightValidationAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace MaLoIdentModels.Validation; | ||
|
||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
/// <summary> | ||
/// an attribute that checks that a datetimeoffset is 00:00 Uhr in German local time | ||
/// </summary> | ||
public class GermanMidnightValidationAttribute : ValidationAttribute | ||
{ | ||
public const string GermanTimeZoneSerializedAsString = | ||
"Central Europe Standard Time;60;(UTC+01:00) Belgrad, Bratislava (Pressburg), Budapest, Ljubljana, Prag (Praha);Mitteleuropäische Zeit ;Mitteleuropäische Sommerzeit ;[01:01:0001;12:31:9999;60;[0;02:00:00;3;5;0;];[0;03:00:00;10;5;0;];];"; | ||
|
||
private static TimeZoneInfo GermanTimeZoneInfo => | ||
TimeZoneInfo.FromSerializedString(GermanTimeZoneSerializedAsString); | ||
|
||
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) | ||
{ | ||
if (value is not DateTimeOffset dto) | ||
{ | ||
return new ValidationResult("Invalid datetime format."); | ||
} | ||
|
||
var germanLocalTime = TimeZoneInfo.ConvertTime(dto, GermanTimeZoneInfo); | ||
|
||
if (germanLocalTime.Hour != 0 || germanLocalTime.Minute != 0 || germanLocalTime.Second != 0) | ||
{ | ||
return new ValidationResult( | ||
$"The date must be at 00:00 (midnight) German time but was '{dto:o}'." | ||
); | ||
} | ||
return ValidationResult.Success; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
MaLoIdentModels/MaLoIdentModelsTests/v1Tests/SerializeGermanTimezoneInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using FluentAssertions; | ||
using MaLoIdentModels.Validation; | ||
|
||
namespace MaLoIdentModelsTests.v1Tests; | ||
|
||
public class CreateTimeZoneFile | ||
{ | ||
/// <summary> | ||
/// saves the German timezone to a serialized string: https://learn.microsoft.com/en-us/dotnet/standard/datetime/save-time-zones-to-an-embedded-resource#example | ||
/// </summary> | ||
[Fact] | ||
public void Serialized_Timezoneinfo_Is_Actual_German_Timezone() | ||
{ | ||
TimeZoneInfo tzi; | ||
try | ||
{ | ||
tzi = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time"); | ||
} | ||
catch (TimeZoneNotFoundException) | ||
{ | ||
//Assert.IsTrue(false, "You cannot use this method on your machine."); // this occurs in github actions. it's ok. | ||
return; | ||
} | ||
|
||
tzi.SupportsDaylightSavingTime.Should().BeTrue(); | ||
var expected = tzi.ToSerializedString(); | ||
GermanMidnightValidationAttribute.GermanTimeZoneSerializedAsString.Should().Be(expected); | ||
TimeZoneInfo | ||
.FromSerializedString( | ||
GermanMidnightValidationAttribute.GermanTimeZoneSerializedAsString | ||
) | ||
.Should() | ||
.BeEquivalentTo(tzi); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.