date_time_util.dart 8.3 KB

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