jama5262 / jiffy

Jiffy is a Flutter (Android, IOS and Web) date time package for parsing, manipulating, querying and formatting dates
https://pub.dev/packages/jiffy
MIT License
588 stars 126 forks source link

`Jiffy.parse` ignores `Z` timezone suffix in ISO 8601 date/time string #281

Open lukehutch opened 4 months ago

lukehutch commented 4 months ago

Describe the bug

final timestamp = '2024-07-02T08:07:50Z';

final a = DateTime.parse(timestamp);
// a = DateTime (2024-07-02 08:07:50.000Z)

final b = DateTime.parse(timestamp).toUtc();
// b = DateTime (2024-07-02 08:07:50.000Z)

final c = Jiffy.parse(timestamp).dateTime;
// c = DateTime (2024-07-02 08:07:50.000)

final d = Jiffy.parse(timestamp).dateTime.toUtc();
// d = DateTime (2024-07-02 14:07:50.000Z)

In c, the timezone is missing, even though the Z suffix is present in the string timestamp.

Consequently, the DateTime is created in the local timezone (by the looks of it). This leads to conversion to UTC being wrong (d).

Jiffy 6.3.1.

jama5262 commented 4 months ago

Hey @lukehutch , that's correct. By default, Jiffy parses the timestamp as local time. To parse a UTC string as UTC, you need to tell Jiffy that the string you are parsing is a UTC by setting the isUtc variable to true, like this:

final timestampUtc = '2024-07-02T08:07:50Z';
final c = Jiffy.parse(timestampUtc, isUtc: true);
print(c.isUtc); // this will print true

You can find more info about string parsing here https://github.com/jama5262/jiffy/tree/master/doc#string-formatting

Hope this solves your issue

jama5262 commented 4 months ago

Please do reopen this issue if the above solution did not solve your problem

lukehutch commented 4 months ago

@jama5262 then Jiffy does not conform to even a useful subset of the ISO 8601 standard! And the user does not even get a warning that their date was parsed wrongly. The Z is the correct way to specify a UTC date, not a Boolean argument! The boolean argument is only necessary if there is some non-confoemant system that does not specify the Z. The Z should not be silently ignored!

I don't have a way to reopen this, but it needs to be reopened. This is a horrible bug, and the "solution" is not a solution.

If you insist that Jiffy should not detect the Z (which would be a very, very bad idea), then at least please throw an exception if the date ends in Z, otherwise you are going to create really bad bugs in user code. It took days to find the source of this bug in my own system.

jama5262 commented 4 months ago

@lukehutch Understandable, yeah Jiffy should automatically detect it

Will work on a fix 👍

lukehutch commented 4 months ago

ISO 8601 is a large standard, but this is an important part of it.

Since you agreed to work on a fix (thank you), the other important part is to parse the +/- timezone suffix. That really needs to be done too.