61 lines
1.9 KiB
Dart
61 lines
1.9 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 DateTime today() {
|
|
DateTime now = DateTime.now();
|
|
return DateTime(now.year, now.month, now.day);
|
|
}
|
|
|
|
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 = false}) {
|
|
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);
|
|
}
|
|
}
|