date_time_util.dart 7.7 KB

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