diameter/lib/utils/date_time_utils.dart
2022-03-21 01:07:29 +01:00

56 lines
1.8 KiB
Dart

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