date_time_util.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import "package:flutter/cupertino.dart";
  2. import 'package:flutter/foundation.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: "Mar",
  10. 4: "Apr",
  11. 5: "May",
  12. 6: "Jun",
  13. 7: "Jul",
  14. 8: "Aug",
  15. 9: "Sep",
  16. 10: "Oct",
  17. 11: "Nov",
  18. 12: "Dec",
  19. };
  20. final currentYear = DateTime.now().year;
  21. const searchStartYear = 1970;
  22. bool areFromSameDay(int firstCreationTime, int secondCreationTime) {
  23. final firstDate = DateTime.fromMicrosecondsSinceEpoch(firstCreationTime);
  24. final secondDate = DateTime.fromMicrosecondsSinceEpoch(secondCreationTime);
  25. return firstDate.year == secondDate.year &&
  26. firstDate.month == secondDate.month &&
  27. firstDate.day == secondDate.day;
  28. }
  29. // Create link default names:
  30. // Same day: "Dec 19, 2022"
  31. // Same month: "Dec 19 - 22, 2022"
  32. // Base case: "Dec 19, 2022 - Jan 7, 2023"
  33. String getNameForDateRange(int firstCreationTime, int secondCreationTime) {
  34. final startTime = DateTime.fromMicrosecondsSinceEpoch(firstCreationTime);
  35. final endTime = DateTime.fromMicrosecondsSinceEpoch(secondCreationTime);
  36. // different year
  37. if (startTime.year != endTime.year) {
  38. return "${_months[startTime.month]!} ${startTime.day}, ${startTime.year} - "
  39. "${_months[endTime.month]!} ${endTime.day}, ${endTime.year}";
  40. }
  41. // same year, diff month
  42. if (startTime.month != endTime.month) {
  43. return "${_months[startTime.month]!} ${startTime.day} - "
  44. "${_months[endTime.month]!} ${endTime.day}, ${endTime.year}";
  45. }
  46. // same month and year, diff day
  47. if (startTime.day != endTime.day) {
  48. return "${_months[startTime.month]!} ${startTime.day} - "
  49. "${_months[endTime.month]!} ${endTime.day}, ${endTime.year}";
  50. }
  51. // same day
  52. return "${_months[endTime.month]!} ${endTime.day}, ${endTime.year}";
  53. }
  54. //11:22 AM
  55. String getTimeIn12hrFormat(DateTime dateTime) {
  56. return DateFormat.jm().format(dateTime);
  57. }
  58. //Thu, Jun 30, 2022 - 14:32
  59. String getFormattedTime(BuildContext context, DateTime dateTime) {
  60. return DateFormat(
  61. 'E, MMM d, y - HH:mm',
  62. Localizations.localeOf(context).languageCode,
  63. ).format(
  64. dateTime,
  65. );
  66. }
  67. String formatDuration(Duration position) {
  68. final ms = position.inMilliseconds;
  69. int seconds = ms ~/ 1000;
  70. final int hours = seconds ~/ 3600;
  71. seconds = seconds % 3600;
  72. final minutes = seconds ~/ 60;
  73. seconds = seconds % 60;
  74. final hoursString = hours >= 10
  75. ? '$hours'
  76. : hours == 0
  77. ? '00'
  78. : '0$hours';
  79. final minutesString = minutes >= 10
  80. ? '$minutes'
  81. : minutes == 0
  82. ? '00'
  83. : '0$minutes';
  84. final secondsString = seconds >= 10
  85. ? '$seconds'
  86. : seconds == 0
  87. ? '00'
  88. : '0$seconds';
  89. final formattedTime =
  90. '${hoursString == '00' ? '' : hoursString + ':'}$minutesString:$secondsString';
  91. return formattedTime;
  92. }
  93. String secondsToHHMMSS(int value) {
  94. int h, m, s;
  95. h = value ~/ 3600;
  96. m = ((value - h * 3600)) ~/ 60;
  97. s = value - (h * 3600) - (m * 60);
  98. final String hourLeft =
  99. h.toString().length < 2 ? "0" + h.toString() : h.toString();
  100. final String minuteLeft =
  101. m.toString().length < 2 ? "0" + m.toString() : m.toString();
  102. final String secondsLeft =
  103. s.toString().length < 2 ? "0" + s.toString() : s.toString();
  104. final String result = "$hourLeft:$minuteLeft:$secondsLeft";
  105. return result;
  106. }
  107. bool isValidDate({
  108. required int day,
  109. required int month,
  110. required int year,
  111. }) {
  112. if (day < 0 || day > 31 || month < 0 || month > 12 || year < 0) {
  113. return false;
  114. }
  115. if (monthWith30Days.contains(month) && day > 30) {
  116. return false;
  117. }
  118. if (month == 2) {
  119. if (day > 29) {
  120. return false;
  121. }
  122. if (day == 29 && year % 4 != 0) {
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. final RegExp exp = RegExp('[\\.A-Za-z]*');
  129. DateTime? parseDateTimeFromFileNameV2(
  130. String fileName, {
  131. /* to avoid parsing incorrect date time from the filename, the max and min
  132. year limits the chances of parsing incorrect date times
  133. */
  134. int minYear = 1990,
  135. int? maxYear,
  136. }) {
  137. // add next year to avoid corner cases for 31st Dec
  138. maxYear ??= currentYear + 1;
  139. String val = fileName.replaceAll(exp, '');
  140. if (val.isNotEmpty && !isNumeric(val[0])) {
  141. val = val.substring(1, val.length);
  142. }
  143. if (val.isNotEmpty && !isNumeric(val[val.length - 1])) {
  144. val = val.substring(0, val.length - 1);
  145. }
  146. final int countOfHyphen = val.split("-").length - 1;
  147. final int countUnderScore = val.split("_").length - 1;
  148. String valForParser = val;
  149. if (countOfHyphen == 1) {
  150. valForParser = val.replaceAll("-", "T");
  151. } else if (countUnderScore == 1 || countUnderScore == 2) {
  152. valForParser = val.replaceFirst("_", "T");
  153. if (countUnderScore == 2) {
  154. valForParser = valForParser.split("_")[0];
  155. }
  156. } else if (countOfHyphen == 2) {
  157. valForParser = val.replaceAll(".", ":");
  158. } else if (countOfHyphen == 6) {
  159. final splits = val.split("-");
  160. valForParser =
  161. "${splits[0]}${splits[1]}${splits[2]}T${splits[3]}${splits[4]}${splits[5]}";
  162. }
  163. final result = DateTime.tryParse(valForParser);
  164. if (kDebugMode && result == null) {
  165. debugPrint("Failed to parse $fileName dateTime from $valForParser");
  166. }
  167. if (result != null && result.year >= minYear && result.year <= maxYear) {
  168. return result;
  169. }
  170. return null;
  171. }
  172. bool isNumeric(String? s) {
  173. if (s == null) {
  174. return false;
  175. }
  176. return double.tryParse(s) != null;
  177. }