1

Can't get NodaDateTime to recognise any other offset than UTC in format: +HH

The following works fine:

var pattern = ZonedDateTimePattern.CreateWithInvariantCulture("dd/MM/yyyy HH:mm:ss +o<HH>", DateTimeZoneProviders.Tzdb);

var dateString = "24/03/2020 13:44:58 +00";

var result = pattern.Parse(dateString);

The following doesn't:

var pattern = ZonedDateTimePattern.CreateWithInvariantCulture("dd/MM/yyyy HH:mm:ss +o<HH>", DateTimeZoneProviders.Tzdb);

var dateString = "24/03/2020 13:44:58 +10";

var result = pattern.Parse(dateString);

In fact no offset other than +00 works. And it also exhibits the same behavior with +o

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161

1 Answers1

1

The problem is that you're trying to parse something as a date/time and time zone, but you're only actually providing an offset. That means Noda Time is using the default time zone from the "template" in the pattern, which is UTC. It then checks that the offset in the value is valid for the local date/time in the value, in UTC... which it never will be unless it's 0. The exception tries to make that clear:

The specified offset is invalid for the given date/time. Value being parsed: '24/03/2020 13:44:58 +10'.

The value you've got doesn't really represent a "date and time with time zone" - it represents a "date and time with UTC offset". In Noda Time, that's handled by the OffsetDateTime type. Just use OffsetDateTimePattern and all will be well:

var pattern = OffsetDateTimePattern.CreateWithInvariantCulture("dd/MM/yyyy HH:mm:ss +o<HH>");
var dateString = "24/03/2020 13:44:58 +10";
var result = pattern.Parse(dateString);
Console.WriteLine(result.Value); // Successful :)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194