date_time_util.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:intl/intl.dart';
  4. import 'package:photos/theme/ente_theme.dart';
  5. const Set<int> monthWith31Days = {1, 3, 5, 7, 8, 10, 12};
  6. const Set<int> monthWith30Days = {4, 6, 9, 11};
  7. Map<int, String> _months = {
  8. 1: "Jan",
  9. 2: "Feb",
  10. 3: "March",
  11. 4: "April",
  12. 5: "May",
  13. 6: "Jun",
  14. 7: "July",
  15. 8: "Aug",
  16. 9: "Sep",
  17. 10: "Oct",
  18. 11: "Nov",
  19. 12: "Dec",
  20. };
  21. Map<int, String> _fullMonths = {
  22. 1: "January",
  23. 2: "February",
  24. 3: "March",
  25. 4: "April",
  26. 5: "May",
  27. 6: "June",
  28. 7: "July",
  29. 8: "August",
  30. 9: "September",
  31. 10: "October",
  32. 11: "November",
  33. 12: "December",
  34. };
  35. Map<int, String> _days = {
  36. 1: "Mon",
  37. 2: "Tue",
  38. 3: "Wed",
  39. 4: "Thu",
  40. 5: "Fri",
  41. 6: "Sat",
  42. 7: "Sun",
  43. };
  44. final currentYear = int.parse(DateTime.now().year.toString());
  45. const searchStartYear = 1970;
  46. //Jun 2022
  47. String getMonthAndYear(DateTime dateTime) {
  48. return _months[dateTime.month]! + " " + dateTime.year.toString();
  49. }
  50. //Thu, 30 Jun
  51. String getDayAndMonth(DateTime dateTime) {
  52. return _days[dateTime.weekday]! +
  53. ", " +
  54. dateTime.day.toString() +
  55. " " +
  56. _months[dateTime.month]!;
  57. }
  58. //30 Jun, 2022
  59. String getDateAndMonthAndYear(DateTime dateTime) {
  60. return dateTime.day.toString() +
  61. " " +
  62. _months[dateTime.month]! +
  63. ", " +
  64. dateTime.year.toString();
  65. }
  66. String getDay(DateTime dateTime) {
  67. return _days[dateTime.weekday]!;
  68. }
  69. String getMonth(DateTime dateTime) {
  70. return _months[dateTime.month]!;
  71. }
  72. String getFullMonth(DateTime dateTime) {
  73. return _fullMonths[dateTime.month]!;
  74. }
  75. String getAbbreviationOfYear(DateTime dateTime) {
  76. return (dateTime.year % 100).toString();
  77. }
  78. //14:32
  79. String getTime(DateTime dateTime) {
  80. final hours = dateTime.hour > 9
  81. ? dateTime.hour.toString()
  82. : "0" + dateTime.hour.toString();
  83. final minutes = dateTime.minute > 9
  84. ? dateTime.minute.toString()
  85. : "0" + dateTime.minute.toString();
  86. return hours + ":" + minutes;
  87. }
  88. //11:22 AM
  89. String getTimeIn12hrFormat(DateTime dateTime) {
  90. return DateFormat.jm().format(dateTime);
  91. }
  92. //Thu, Jun 30, 2022 - 14:32
  93. String getFormattedTime(DateTime dateTime) {
  94. return getDay(dateTime) +
  95. ", " +
  96. getMonth(dateTime) +
  97. " " +
  98. dateTime.day.toString() +
  99. ", " +
  100. dateTime.year.toString() +
  101. " - " +
  102. getTime(dateTime);
  103. }
  104. //30 Jun'22
  105. String getFormattedDate(DateTime dateTime) {
  106. return dateTime.day.toString() +
  107. " " +
  108. getMonth(dateTime) +
  109. "'" +
  110. getAbbreviationOfYear(dateTime);
  111. }
  112. String getFullDate(DateTime dateTime) {
  113. return getDay(dateTime) +
  114. ", " +
  115. getMonth(dateTime) +
  116. " " +
  117. dateTime.day.toString() +
  118. " " +
  119. dateTime.year.toString();
  120. }
  121. String daysLeft(int futureTime) {
  122. final int daysLeft = ((futureTime - DateTime.now().microsecondsSinceEpoch) /
  123. Duration.microsecondsPerDay)
  124. .ceil();
  125. return '$daysLeft day' + (daysLeft <= 1 ? "" : "s");
  126. }
  127. String formatDuration(Duration position) {
  128. final ms = position.inMilliseconds;
  129. int seconds = ms ~/ 1000;
  130. final int hours = seconds ~/ 3600;
  131. seconds = seconds % 3600;
  132. final minutes = seconds ~/ 60;
  133. seconds = seconds % 60;
  134. final hoursString = hours >= 10
  135. ? '$hours'
  136. : hours == 0
  137. ? '00'
  138. : '0$hours';
  139. final minutesString = minutes >= 10
  140. ? '$minutes'
  141. : minutes == 0
  142. ? '00'
  143. : '0$minutes';
  144. final secondsString = seconds >= 10
  145. ? '$seconds'
  146. : seconds == 0
  147. ? '00'
  148. : '0$seconds';
  149. final formattedTime =
  150. '${hoursString == '00' ? '' : hoursString + ':'}$minutesString:$secondsString';
  151. return formattedTime;
  152. }
  153. bool isLeapYear(DateTime dateTime) {
  154. final year = dateTime.year;
  155. if (year % 4 == 0) {
  156. if (year % 100 == 0) {
  157. if (year % 400 == 0) {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. } else {
  163. return true;
  164. }
  165. } else {
  166. return false;
  167. }
  168. }
  169. Widget getDayWidget(
  170. BuildContext context,
  171. int timestamp,
  172. ) {
  173. final colorScheme = getEnteColorScheme(context);
  174. final textTheme = getEnteTextTheme(context);
  175. return Container(
  176. alignment: Alignment.centerLeft,
  177. child: Text(
  178. getDayTitle(timestamp),
  179. style: (getDayTitle(timestamp) == "Today")
  180. ? textTheme.body
  181. : textTheme.body.copyWith(color: colorScheme.textMuted),
  182. ),
  183. );
  184. }
  185. String getDayTitle(int timestamp) {
  186. final date = DateTime.fromMicrosecondsSinceEpoch(timestamp);
  187. final now = DateTime.now();
  188. var title = getDayAndMonth(date);
  189. if (date.year == now.year && date.month == now.month) {
  190. if (date.day == now.day) {
  191. title = "Today";
  192. } else if (date.day == now.day - 1) {
  193. title = "Yesterday";
  194. }
  195. }
  196. if (date.year != DateTime.now().year) {
  197. title += " " + date.year.toString();
  198. }
  199. return title;
  200. }
  201. String secondsToHHMMSS(int value) {
  202. int h, m, s;
  203. h = value ~/ 3600;
  204. m = ((value - h * 3600)) ~/ 60;
  205. s = value - (h * 3600) - (m * 60);
  206. final String hourLeft =
  207. h.toString().length < 2 ? "0" + h.toString() : h.toString();
  208. final String minuteLeft =
  209. m.toString().length < 2 ? "0" + m.toString() : m.toString();
  210. final String secondsLeft =
  211. s.toString().length < 2 ? "0" + s.toString() : s.toString();
  212. final String result = "$hourLeft:$minuteLeft:$secondsLeft";
  213. return result;
  214. }
  215. bool isValidDate({
  216. required int day,
  217. required int month,
  218. required int year,
  219. }) {
  220. if (day < 0 || day > 31 || month < 0 || month > 12 || year < 0) {
  221. return false;
  222. }
  223. if (monthWith30Days.contains(month) && day > 30) {
  224. return false;
  225. }
  226. if (month == 2) {
  227. if (day > 29) {
  228. return false;
  229. }
  230. if (day == 29 && year % 4 != 0) {
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. final RegExp exp = RegExp('[\\.A-Za-z]*');
  237. DateTime? parseDateTimeFromFileNameV2(String fileName) {
  238. String val = fileName.replaceAll(exp, '');
  239. if (val.isNotEmpty && !isNumeric(val[0])) {
  240. val = val.substring(1, val.length);
  241. }
  242. if (val.isNotEmpty && !isNumeric(val[val.length - 1])) {
  243. val = val.substring(0, val.length - 1);
  244. }
  245. final int countOfHyphen = val.split("-").length - 1;
  246. final int countUnderScore = val.split("_").length - 1;
  247. String valForParser = val;
  248. if (countOfHyphen == 1) {
  249. valForParser = val.replaceAll("-", "T");
  250. } else if (countUnderScore == 1 || countUnderScore == 2) {
  251. valForParser = val.replaceFirst("_", "T");
  252. if (countUnderScore == 2) {
  253. valForParser = valForParser.split("_")[0];
  254. }
  255. } else if (countOfHyphen == 2) {
  256. valForParser = val.replaceAll(".", ":");
  257. } else if (countOfHyphen == 6) {
  258. final splits = val.split("-");
  259. valForParser =
  260. "${splits[0]}${splits[1]}${splits[2]}T${splits[3]}${splits[4]}${splits[5]}";
  261. }
  262. final result = DateTime.tryParse(valForParser);
  263. if (kDebugMode && result == null) {
  264. debugPrint("Failed to parse $fileName dateTime from $valForParser");
  265. }
  266. return result;
  267. }
  268. bool isNumeric(String? s) {
  269. if (s == null) {
  270. return false;
  271. }
  272. return double.tryParse(s) != null;
  273. }