Skip to content

Instantly share code, notes, and snippets.

@Roms1383
Last active September 10, 2022 16:35
Show Gist options
  • Save Roms1383/7d0e9d50cd3c99091c2521a947d021d5 to your computer and use it in GitHub Desktop.
Save Roms1383/7d0e9d50cd3c99091c2521a947d021d5 to your computer and use it in GitHub Desktop.
dart conversion from different size

dart conversion from different size

Created with <3 with dartpad.dev.

void main() {
smallest_int();
print('----------------');
smaller_int();
print('----------------');
is_it_a_bug_or_a_feature();
}
void smallest_int() {
print('equivalent in Rust: 1970-01-01 00:02:30.151152 UTC (ms 150151152)');
final ms = 150151152;
print('ms: $ms');
final date = DateTime.fromMicrosecondsSinceEpoch(ms, isUtc: true);
print('date: $date');
print('ms since epoch: ${date.microsecondsSinceEpoch}');
}
void smaller_int() {
print('equivalent in Rust: 1970-01-02 17:42:31.152153 UTC (ms 150151152153)');
final ms = 150151152153;
print('ms: $ms');
final date = DateTime.fromMicrosecondsSinceEpoch(ms, isUtc: true);
print('date: $date');
print('ms since epoch: ${date.microsecondsSinceEpoch}');
}
void is_it_a_bug_or_a_feature() {
print(
'equivalent in Rust: 1974-10-04 20:39:12.153154 UTC (ms 150151152153154)');
final ms = 150151152153154;
final modified = 150151152153154 * 1000;
print('ms: $ms');
print('ms multiplied by 1000: $modified');
final date = DateTime.fromMicrosecondsSinceEpoch(ms, isUtc: true);
final date_modified =
DateTime.fromMicrosecondsSinceEpoch(modified, isUtc: true);
print('date: $date');
print('date with multiplied ms: $date_modified');
print('ms since epoch: ${date.microsecondsSinceEpoch}');
print(
'ms since epoch date with multiplied ms: ${date_modified.microsecondsSinceEpoch}');
}
@Roms1383
Copy link
Author

Roms1383 commented Sep 10, 2022

console output:

equivalent in Rust: 1970-01-01 00:02:30.151152 UTC (ms 150151152)
ms: 150151152
date: 1970-01-01 00:02:30.151Z
ms since epoch: 150151000
----------------
equivalent in Rust: 1970-01-02 17:42:31.152153 UTC (ms 150151152153)
ms: 150151152153
date: 1970-01-02 17:42:31.152Z
ms since epoch: 150151152000
----------------
equivalent in Rust: 1974-10-04 20:39:12.153154 UTC (ms 150151152153154)
ms: 150151152153154
ms multiplied by 1000: 150151152153154000
date: 1974-10-04 20:39:12.153Z
date with multiplied ms: 6728-02-07 13:22:33.154Z
ms since epoch: 150151152153000
ms since epoch date with multiplied ms: 150151152153154000

@Roms1383
Copy link
Author

There definitely seems to be a bug because:

  • without multiplying by 1000, one gets a correct DateTime
  • without multiplying by 1000, one cannot retrieve the original last portion of the digit's microseconds (e.g. 154 in the last example)

@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