33 lines
1009 B
Dart
33 lines
1009 B
Dart
import 'package:intl/intl.dart';
|
|
|
|
/// Parses API date fields that may arrive as int, double, or string.
|
|
int? unixTimestampFromJson(dynamic value) {
|
|
if (value == null) return null;
|
|
if (value is int) return value;
|
|
if (value is num) return value.toInt();
|
|
if (value is String) {
|
|
final trimmed = value.trim();
|
|
if (trimmed.isEmpty) return null;
|
|
return int.tryParse(trimmed);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Converts a Unix timestamp (seconds) from the API to a display string.
|
|
/// Mirrors Next.js `unixToDate` using `moment.unix(unix)`.
|
|
String timeConvertor(int? unix, {bool hasTime = false}) {
|
|
if (unix == null || unix == 0) return '-';
|
|
|
|
// Values above ~year 2286 in seconds are treated as milliseconds.
|
|
final seconds = unix > 9999999999 ? unix ~/ 1000 : unix;
|
|
final date = DateTime.fromMillisecondsSinceEpoch(
|
|
seconds * 1000,
|
|
isUtc: true,
|
|
).toLocal();
|
|
|
|
if (hasTime) {
|
|
return DateFormat('yyyy-MM-dd HH:mm').format(date);
|
|
}
|
|
return DateFormat('yyyy-MM-dd').format(date);
|
|
}
|