Skip to content

Instantly share code, notes, and snippets.

@Roms1383
Last active September 10, 2022 16:35
Show Gist options
  • Save Roms1383/31bcd2c74cb3e23dc4dd33176bfa224f to your computer and use it in GitHub Desktop.
Save Roms1383/31bcd2c74cb3e23dc4dd33176bfa224f to your computer and use it in GitHub Desktop.
lossy conversion ?
void main() {
final past_ms = -150151152153154;
final future_ms = 150151152153154;
final past = DateTime.fromMicrosecondsSinceEpoch(past_ms, isUtc: true);
print('$past (ms ${past.microsecondsSinceEpoch})');
final future = DateTime.fromMicrosecondsSinceEpoch(future_ms, isUtc: true);
print('$future (ms ${future.microsecondsSinceEpoch})');
}
@Roms1383
Copy link
Author

Roms1383 commented Sep 10, 2022

output in dart console:

1965-03-30 03:20:47.847Z (ms -150151152153000)
1974-10-04 20:39:12.153Z (ms 150151152153000)

so reverse conversion in Rust is lossy ?
notice how the last part of the digit e.g. 154 in 150151152153154 or 150_151_152_153_154 is missing from DateTime microsecondsSinceEpoch.

@Roms1383
Copy link
Author

On the other side, the equivalent in Rust:

1965-03-30 03:20:48.846846 UTC (ms -150151151153154)
1974-10-04 20:39:12.153154 UTC (ms 150151152153154)

Check Rust playground gist.

@Roms1383
Copy link
Author

Well actually it gets weirder when comparing with different int sizes, see this another gist.

@Roms1383
Copy link
Author

Tested on stable: Flutter 3.3.1 Dart 2.18.0.

@Roms1383
Copy link
Author

update: conversion isn't lossy on platform other than web and DartPad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment