time_machine icon indicating copy to clipboard operation
time_machine copied to clipboard

Error when parsing date with timezone offset

Open EugenIvanushkin opened this issue 2 years ago • 1 comments

Hello! I'm trying to parse date time with timezone offset:

await TimeMachine.initialize({'rootBundle': rootBundle});
var localClone =
      ZonedDateTimePattern.createWithInvariantCulture('yyyy-MM-ddTHH:mm z')
          .parse("2024-01-31T06:13 +02:00");

And get the following error:

E/flutter (13788): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: The specified time zone identifier is not recognized. Value being parsed: '2024-01-31T06:13 ^+02:00'. (^ indicates error position.)
E/flutter (13788): #0      ParseResult.getValueOrThrow (package:time_machine/src/text/parse_result.dart:62:5)
E/flutter (13788): #1      ParseResult.value (package:time_machine/src/text/parse_result.dart:36:18)
E/flutter (13788): #2      main (package:alean_travel/main.dart:45:20)
E/flutter (13788): <asynchronous suspension>

What a proper way to parse date with offset?

EugenIvanushkin avatar Apr 05 '24 10:04 EugenIvanushkin

This should work:

    final localClone = OffsetDateTimePattern.createWithInvariantCulture(
      "uuuu'-'MM'-'dd'T'HH':'mm o<Z+HH':'mm>",
    ).parse("2024-01-31T06:13 +02:00");

malmi avatar Sep 24 '24 22:09 malmi