Created with <3 with dartpad.dev.
Last active
September 10, 2022 16:35
-
-
Save Roms1383/7d0e9d50cd3c99091c2521a947d021d5 to your computer and use it in GitHub Desktop.
dart conversion from different size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'); | |
} |
There definitely seems to be a bug because:
- without multiplying by
1000
, one gets a correctDateTime
- without multiplying by
1000
, one cannot retrieve the original last portion of the digit's microseconds (e.g.154
in the last example)
Tested on stable: Flutter 3.3.1
Dart 2.18.0
.
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
console output: