diameter/lib/utils/date_time_utils.dart

56 lines
1.8 KiB
Dart
Raw Normal View History

2022-03-21 00:07:29 +00:00
import 'package:diameter/models/settings.dart';
2021-10-22 23:08:09 +00:00
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
2022-03-21 00:07:29 +00:00
final DateTime dummyDate = DateTime(2000);
2021-10-22 23:08:09 +00:00
class DateTimeUtils {
static String displayDateTime(DateTime? date, {String fallback = ''}) {
if (date == null) {
return fallback;
}
DateTime localDate = date.toLocal();
2022-03-21 00:07:29 +00:00
final DateFormat formatter =
DateFormat('${Settings.get().dateFormat} ${Settings.get().timeFormat}');
2021-10-22 23:08:09 +00:00
return formatter.format(localDate);
}
static String displayDate(DateTime? date, {String fallback = ''}) {
if (date == null) {
return fallback;
}
DateTime localDate = date.toLocal();
2022-03-21 00:07:29 +00:00
final DateFormat formatter =
DateFormat(Settings.get().longDateFormat ?? Settings.get().dateFormat);
2021-10-22 23:08:09 +00:00
return formatter.format(localDate);
}
static String displayTime(DateTime? date,
{String fallback = '', bool? longFormat}) {
if (date == null) {
return fallback;
}
DateTime localDate = date.toLocal();
2022-03-21 00:07:29 +00:00
final DateFormat formatter = DateFormat(longFormat == true
? Settings.get().longTimeFormat ?? Settings.get().timeFormat
: Settings.get().timeFormat);
2021-10-22 23:08:09 +00:00
return formatter.format(localDate);
}
static String displayTimeOfDay(TimeOfDay? time,
{String fallback = '', bool? longFormat}) {
if (time == null) {
return fallback;
}
2022-03-21 00:07:29 +00:00
final DateFormat formatter = DateFormat(longFormat == true
? Settings.get().longTimeFormat ?? Settings.get().timeFormat
: Settings.get().timeFormat);
2021-10-22 23:08:09 +00:00
return formatter.format(convertTimeOfDayToDateTime(time));
}
static DateTime convertTimeOfDayToDateTime(TimeOfDay time) {
return DateTime(
dummyDate.year, dummyDate.month, dummyDate.day, time.hour, time.minute);
}
}