mirror of
https://github.com/immich-app/immich.git
synced 2025-09-29 15:31:13 -04:00
* fix: handle datetime outside the valid range supported by dart * add tests for tryFromSecondsSinceEpoch --------- Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
20 lines
538 B
Dart
20 lines
538 B
Dart
const int _maxMillisecondsSinceEpoch = 8640000000000000; // 275760-09-13
|
|
const int _minMillisecondsSinceEpoch = -62135596800000; // 0001-01-01
|
|
|
|
DateTime? tryFromSecondsSinceEpoch(int? secondsSinceEpoch) {
|
|
if (secondsSinceEpoch == null) {
|
|
return null;
|
|
}
|
|
|
|
final milliSeconds = secondsSinceEpoch * 1000;
|
|
if (milliSeconds < _minMillisecondsSinceEpoch || milliSeconds > _maxMillisecondsSinceEpoch) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return DateTime.fromMillisecondsSinceEpoch(milliSeconds);
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|