Ich bin dabei das RFC1123-Datumsformat in DateTime-Objekt umzuwandeln und umgekehrt. Das DateTime-Objekt nach RFC-Datums-String funktioniert einwandfrei, aber da ich in Deutschland lebe (MEZ-Timezone) bekomme ich falsche Ergebnisse.Datumsformat von RFC1123 in DateTime-Objekt umwandeln
Also einmal, hier ist meine Klasse für die Umwandlung:
public interface IRFCDate
{
DateTime ToDateTime();
}
public class RFCDate : IRFCDate
{
private string dateString { get; set; } = null;
private DateTime? dateObject { get; set; } = null;
public DateTime ToDateTime()
{
if (dateObject.HasValue) return dateObject.Value;
string regexPattern = @"[a-zA-Z]+, [0-9]+ [a-zA-Z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ (?<timezone>[a-zA-Z]+)";
Regex findTimezone = new Regex(regexPattern, RegexOptions.Compiled);
string timezone = findTimezone.Match(dateString).Result("${timezone}");
string format = $"ddd, dd MMM yyyy HH:mm:ss {timezone}";
dateObject = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
return dateObject.Value;
}
public IRFCDate From(IConvertible value)
{
if (value is string)
dateString = value.ToString();
else if (value is DateTime)
dateObject = (DateTime)value;
else
throw new NotSupportedException($"Parametertype has to be either string or DateTime. '{value.GetType()}' is unsupported.");
return this;
}
}
mein xUnit-Testfall wie folgt aussieht:
[Fact]
public void StringToDateTime()
{
DateTime expectedValue = new DateTime(2001, 1, 1);
string RFCDatestring = "Mon, 01 Jan 2001 00:00:00 GMT";
DateTime actualValue = RFCDatestring.To<DateTime>();
Assert.Equal(expectedValue, actualValue);
}
Um in diesem Fall ruft
return new RFCDate().From(@this).ToDateTime();
So die Ergebnis bei der Ausführung meines Testfalls ist:
Assert.Equal() Ausfall
Erwartet: 2001-01-01T00: 00: 00,0000000
Aktuell: 2001-01-01T01: 00: 00.0000000 + 01: 00
Hat jemand Ideen wie man das beheben kann? Der tatsächliche Wert sollte 00:00 Uhr statt 1 Uhr sein.