date_time_util.dart 7.4 KB

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