1

The server where I am working in some routes it returns a Date with one extra number and it was causing a weird behavior in our application. Then I isolated the weird behavior to the following code:

import 'package:intl/intl.dart';

void main() {
  final format = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
  print(format.parse('2020-08-05T14:23:55.974582+00:00'));
  print(format.parse('2020-08-05T14:23:55.9745829+00:00'));
}

produces the output:

2020-08-05 14:40:09.582
2020-08-05 17:06:20.829

Why the extra number 9 introduces 3 hours difference? I can not understand why.

I created a repl so you can try: https://repl.it/join/jfhehoqt-leonardosilva25

when I try to parse the same string in swift, it gives a different result than dart, both strings gives me the same date https://repl.it/join/ncjffdth-leonardosilva25

original swift code from repl for reference:

import Foundation

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ"

print(dateFormatter.date(from: "2020-08-05T14:23:55.974582+00:00")!)
print(dateFormatter.date(from: "2020-08-05T14:23:55.9745829+00:00")!)
Leonardo da Silva
  • 1,285
  • 2
  • 10
  • 25
  • 1
    This has nothing to do with Flutter nor the `intl` package, 9745829 milliseconds are almost 2.7 hours, so yeah, it's logical that when you add a 9 to the end the difference will be huge as 974582 boils down to only 16 minutes or so. – Ayman Barghout Aug 05 '20 at 14:57
  • 1
    my bad, i fixed the tags – Leonardo da Silva Aug 05 '20 at 14:59
  • what confuses me more is that in swift both give the same result https://repl.it/join/ncjffdth-leonardosilva25 – Leonardo da Silva Aug 05 '20 at 15:00
  • I expressed my self wrong. in swift it does not give the same result as dart, it gives a different result than dart, because both strings return the same date – Leonardo da Silva Aug 05 '20 at 15:05
  • Yes, it's my bad, I didn't check the repl before commenting. I have no idea why it's not working in Swift, but the way I see it, intl is handling fractional seconds pretty well and they convert them correctly to date. – Ayman Barghout Aug 05 '20 at 15:21
  • it seems to be a bug in dart intl https://github.com/dart-lang/intl/issues/69 – Leonardo da Silva Aug 05 '20 at 15:40
  • 1
    Good catch! I didn't know that only the three numbers are milliseconds, it is my fault I am glad you got your answer and sorry for confusing you! I think the package maintainer treats all fractions as milliseconds as I thought :D – Ayman Barghout Aug 05 '20 at 15:43

0 Answers0