import 'package:intl/intl.dart'; /// Parses an API timestamp field that may arrive as int, double, or string. /// Returns null for missing, empty, unparseable, or non-positive values /// (a non-positive epoch is treated as "no value" by the API). int? unixTimestampFromJson(dynamic value) { if (value == null) return null; int? parsed; if (value is int) { parsed = value; } else if (value is num) { parsed = value.toInt(); } else if (value is String) { final trimmed = value.trim(); if (trimmed.isEmpty) return null; parsed = int.tryParse(trimmed); } if (parsed == null || parsed <= 0) return null; return parsed; } /// Converts a Unix timestamp from the API to a display string. /// /// The API contract is seconds since epoch, but some legacy fields still /// emit milliseconds. Anything past the year 2286 in seconds (>1e10) is /// assumed to be milliseconds and converted down. Remove this branch once /// every endpoint has been migrated to seconds. String timeConvertor(int? unix, {bool hasTime = false}) { if (unix == null || unix <= 0) return '-'; 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); }