Line data Source code
1 : import 'package:localization/localization.dart';
2 :
3 : extension Floor on DateTime {
4 0 : DateTime floorToSecond() {
5 0 : return DateTime.fromMillisecondsSinceEpoch(
6 0 : (millisecondsSinceEpoch ~/ 1000) * 1000,
7 : );
8 : }
9 : }
10 :
11 : extension DateString on DateTime {
12 0 : String toDateString({bool shorten = true, bool useAdverbs = false}) {
13 : if (useAdverbs) {
14 0 : final now = DateTime.now();
15 0 : final today = DateTime(now.year, now.month, now.day);
16 0 : final inputDate = DateTime(year, month, day);
17 :
18 : return switch (inputDate) {
19 0 : _ when inputDate == today => "time_adverb.today".i18n(),
20 0 : _ when inputDate == today.subtract(const Duration(days: 1)) => "time_adverb.yesterday".i18n(),
21 0 : _ when inputDate == today.add(const Duration(days: 1)) => "time_adverb.tomorrow".i18n(),
22 0 : _ => toDateString(shorten: shorten),
23 : };
24 : }
25 0 : return "$day ${switch (month) {
26 0 : DateTime.january => shorten ? "acronym.months.january".i18n() : "months.january".i18n(),
27 0 : DateTime.february => shorten ? "acronym.months.february".i18n() : "months.february".i18n(),
28 0 : DateTime.march => shorten ? "acronym.months.march".i18n() : "months.march".i18n(),
29 0 : DateTime.april => shorten ? "acronym.months.april".i18n() : "months.april".i18n(),
30 0 : DateTime.may => shorten ? "acronym.months.may".i18n() : "months.may".i18n(),
31 0 : DateTime.june => shorten ? "acronym.months.june".i18n() : "months.june".i18n(),
32 0 : DateTime.july => shorten ? "acronym.months.july".i18n() : "months.july".i18n(),
33 0 : DateTime.august => shorten ? "acronym.months.august".i18n() : "months.august".i18n(),
34 0 : DateTime.september => shorten ? "acronym.months.september".i18n() : "months.september".i18n(),
35 0 : DateTime.october => shorten ? "acronym.months.october".i18n() : "months.october".i18n(),
36 0 : DateTime.november => shorten ? "acronym.months.november".i18n() : "months.november".i18n(),
37 0 : DateTime.december => shorten ? "acronym.months.december".i18n() : "months.december".i18n(),
38 0 : _ => "error".i18n()
39 0 : }} $year";
40 : }
41 : }
42 :
|