Refactor date handling in tender data model and UI components. Added unixTimestampFromJson for parsing timestamps and updated timeConvertor to handle null values. Updated relevant model fields and UI to use the new parsing logic. Added unit tests for date utilities.

This commit is contained in:
AmirReza Jamali
2026-05-24 15:28:47 +03:30
parent 4525e06119
commit 2515190576
9 changed files with 71 additions and 23 deletions
+27 -4
View File
@@ -1,9 +1,32 @@
import 'package:intl/intl.dart';
String timeConvertor(int timestamp) {
// Convert seconds → milliseconds
final date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
/// 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;
}
// Format to yyyy-MM-dd
/// 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);
}