Skip to content

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 7 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
11 changes: 11 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/v1/IdentificationParameter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Text.Json.Serialization;
using MaLoIdentModels.JsonSettings;
using MaLoIdentModels.Validation;

namespace MaLoIdentModels.v1;

Expand All @@ -10,6 +11,16 @@ public class IdentificationParameter
[System.ComponentModel.DataAnnotations.Key]
public Guid? Id { get; set; }

/// <summary>
///
/// </summary>
/// <remarks>
/// Has to be midnight in German local time.
/// See Seite 6 von 111
/// 1.1.2 SD: Ermittlung der MaLo-ID der Marktlokation
/// https://www.bundesnetzagentur.de/DE/Beschlusskammern/1_GZ/BK6-GZ/2022/BK6-22-024/Beschluss/Anlage1b_GPKE_Teil2.pdf?__blob=publicationFile&v=1
/// </remarks>
[GermanMidnightValidation]
[JsonConverter(typeof(DateTimeOffsetWithTrailingZConverter))]
[JsonPropertyName("identificationDateTime")]
public DateTimeOffset IdentificationDateTime { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using FluentAssertions;
using MaLoIdentModels.v1;
Expand Down Expand Up @@ -80,4 +81,24 @@ string dateTimeOffsetString
.IdentificationDateTime.Should()
.Be(new DateTimeOffset(2023, 8, 2, 22, 0, 0, 0, TimeSpan.Zero));
}

[Theory]
[InlineData("2025-01-01T00:00:00Z", false)]
[InlineData("2024-12-31T23:00:00Z", true)]
[InlineData("2025-06-30T22:00:00Z", true)]
[InlineData("2025-06-30T23:00:00Z", false)]
public void Validation_Of_German_Midnight_Works(
string dateTimeOffsetString,
bool isValidExpected
)
{
var parameter = new IdentificationParameter
{
IdentificationDateTime = DateTimeOffset.Parse(dateTimeOffsetString),
};
var context = new ValidationContext(parameter);
var results = new List<ValidationResult>();
var isValidActual = Validator.TryValidateObject(parameter, context, results, true);
isValidActual.Should().Be(isValidExpected);
}
}
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);
}
}
Loading