import 'package:intl/intl.dart'; /// Extension on int to provide date formatting utilities for Unix timestamps extension UnixTimestampExtension on int? { /// Converts Unix timestamp (seconds since epoch) to a formatted date string String toDate({String format = 'yyyy-MM-dd'}) { if (this == null) { return ''; } // Convert seconds to milliseconds if needed int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( timestampInMs, ); return DateFormat(format).format(dateTime); } /// Converts Unix timestamp to a readable date string String toReadableDate() { if (this == null) { return ''; } int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( timestampInMs, ); final DateTime now = DateTime.now(); final Duration difference = now.difference(dateTime); if (difference.inDays == 0) { return 'Today'; } else if (difference.inDays == 1) { return 'Yesterday'; } else if (difference.inDays < 7) { return '${difference.inDays} days ago'; } else { return DateFormat('MMM dd, yyyy').format(dateTime); } } /// Converts Unix timestamp to a short date format String toShortDate() { if (this == null) { return ''; } int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( timestampInMs, ); return DateFormat('MMM dd').format(dateTime); } /// Converts Unix timestamp to a full date and time format String toDateTime() { if (this == null) { return ''; } int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( timestampInMs, ); return DateFormat('MMM dd, yyyy HH:mm').format(dateTime); } /// Checks if a Unix timestamp is in the past bool get isPast { if (this == null) { return false; } int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( timestampInMs, ); return dateTime.isBefore(DateTime.now()); } /// Gets the number of days remaining until a Unix timestamp int get daysRemaining { if (this == null) { return 0; } int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( timestampInMs, ); final DateTime now = DateTime.now(); final Duration difference = dateTime.difference(now); return difference.inDays; } } /// Extension on DateTime to provide additional formatting utilities extension DateTimeExtension on DateTime { /// Formats the DateTime to a readable string String toReadableString() { final DateTime now = DateTime.now(); final Duration difference = now.difference(this); if (difference.inDays == 0) { return 'Today'; } else if (difference.inDays == 1) { return 'Yesterday'; } else if (difference.inDays < 7) { return '${difference.inDays} days ago'; } else { return DateFormat('MMM dd, yyyy').format(this); } } /// Formats the DateTime to a short date string String toShortDate() { return DateFormat('MMM dd').format(this); } /// Formats the DateTime to a full date and time string String toDateTimeString() { return DateFormat('MMM dd, yyyy HH:mm').format(this); } /// Checks if the DateTime is in the past bool get isPast { return isBefore(DateTime.now()); } /// Gets the number of days remaining until this DateTime int get daysRemaining { final DateTime now = DateTime.now(); final Duration difference = this.difference(now); return difference.inDays; } } /// Utility class for date operations class DateUtils { /// Converts Unix timestamp to a formatted date string static String unixToDate(int? timestamp) { if (timestamp == null) { return ''; } // Convert seconds to milliseconds if needed int timestampInMs = timestamp > 1000000000000 ? timestamp : timestamp * 1000; final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( timestampInMs, ); return DateFormat('MMM dd, yyyy').format(dateTime); } } /// Global function for backward compatibility String unixToDate(int? timestamp) { return DateUtils.unixToDate(timestamp); }