diameter/lib/utils/date_time_utils.dart

61 lines
1.9 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 {
2022-03-21 00:08:05 +00:00
static DateTime today() {
DateTime now = DateTime.now();
return DateTime(now.year, now.month, now.day);
}
2021-10-22 23:08:09 +00:00
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,
2022-03-21 00:08:05 +00:00
{String fallback = '', bool longFormat = false}) {
2021-10-22 23:08:09 +00:00
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);
}
}