replace unixToDate to timeConvertor

This commit is contained in:
llsajjad
2025-09-13 12:59:47 +03:30
parent c239885d34
commit ddd464c2c2
6 changed files with 15 additions and 184 deletions
+5 -166
View File
@@ -1,169 +1,8 @@
import 'package:intl/intl.dart';
String timeConvertor(int time) {
DateTime date = DateTime.fromMillisecondsSinceEpoch(time);
/// 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 '';
}
String formattedDate =
"${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}";
// 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);
return formattedDate;
}
+4 -4
View File
@@ -53,7 +53,7 @@ class DeadlineItem extends StatelessWidget {
),
SizedBox(height: 4.0.h()),
Text(
unixToDate(detail.submissionDeadline),
timeConvertor(detail.submissionDeadline!),
style: TextStyle(
color: AppColors.grey80,
fontWeight: FontWeight.w400,
@@ -82,7 +82,7 @@ class DeadlineItem extends StatelessWidget {
),
SizedBox(height: 4.0.h()),
Text(
unixToDate(detail.applicationDeadline),
timeConvertor(detail.applicationDeadline!),
style: TextStyle(
color: AppColors.grey80,
fontWeight: FontWeight.w400,
@@ -128,7 +128,7 @@ class DeadlineItem extends StatelessWidget {
),
),
Text(
unixToDate(detail.submissionDeadline),
timeConvertor(detail.submissionDeadline!),
style: TextStyle(
color: AppColors.grey70,
fontWeight: FontWeight.w400,
@@ -150,7 +150,7 @@ class DeadlineItem extends StatelessWidget {
),
),
Text(
unixToDate(detail.applicationDeadline),
timeConvertor(detail.applicationDeadline!),
style: TextStyle(
color: AppColors.grey70,
fontWeight: FontWeight.w400,
@@ -49,7 +49,7 @@ class TenderDetailHeader extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
unixToDate(detail.publicationDate),
timeConvertor(detail.publicationDate!),
style: TextStyle(
color: AppColors.grey60,
fontWeight: FontWeight.w400,
+2 -10
View File
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:tm_app/core/utils/date_utils.dart';
import 'package:tm_app/data/services/model/tender_data/tender_data.dart';
import 'package:tm_app/views/home/strings/home_strings.dart';
import 'package:tm_app/views/home/widgets/tender_card_progress_bar.dart';
@@ -123,13 +124,4 @@ class TendersListItem extends StatelessWidget {
),
);
}
}
String timeConvertor(int time) {
DateTime date = DateTime.fromMillisecondsSinceEpoch(time);
String formattedDate =
"${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}";
return formattedDate;
}
}
+2 -2
View File
@@ -113,7 +113,7 @@ class TenderCard extends StatelessWidget {
Widget _dateText() {
return Text(
DateUtils.unixToDate(tender.publicationDate ?? 0),
timeConvertor(tender.publicationDate ?? 0),
style: TextStyle(
color: AppColors.grey60,
fontSize: 12.0.sp(),
@@ -143,7 +143,7 @@ class TenderCard extends StatelessWidget {
),
),
Text(
DateUtils.unixToDate(tender.tenderDeadline ?? 0),
timeConvertor(tender.tenderDeadline ?? 0),
style: TextStyle(
color: AppColors.grey80,
fontSize: 14.0.sp(),
@@ -74,7 +74,7 @@ class TenderCard extends StatelessWidget {
children: [
// Date
Text(
unixToDate(int.parse(tender.publicationDate!.toString())),
timeConvertor(int.parse(tender.publicationDate!.toString())),
style: TextStyle(
color: AppColors.grey60,
fontSize: 12.0.sp(),