Browse Source

Prefer existing creationTime if tileFromFileName is close to existing time

Neeraj Gupta 2 years ago
parent
commit
ed76ca0b39
2 changed files with 21 additions and 2 deletions
  1. 15 2
      lib/models/file.dart
  2. 6 0
      lib/utils/date_time_util.dart

+ 15 - 2
lib/models/file.dart

@@ -188,12 +188,25 @@ class File extends EnteFile {
         creationTime = exifTime.microsecondsSinceEpoch;
       }
     }
-    // try to get the timestamp from fileName. In case of iOS, file names are
+    // Try to get the timestamp from fileName. In case of iOS, file names are
     // generic IMG_XXXX, so only parse it on Android devices
     if (!hasExifTime && Platform.isAndroid && title != null) {
       final timeFromFileName = parseDateTimeFromFileNameV2(title!);
       if (timeFromFileName != null) {
-        creationTime = timeFromFileName.microsecondsSinceEpoch;
+        // only use timeFromFileName is the delta between current
+        // creationTime and timeFromFileName is larger than some threshold.
+        // This is done because many times the fileTimeStamp will only give us
+        // the date, not time value but the photo_manager's creation time will
+        // contain the time.
+        final bool useFileTimeStamp = creationTime == null ||
+            daysBetween(
+                  DateTime.fromMicrosecondsSinceEpoch(creationTime!),
+                  timeFromFileName,
+                ).abs() <=
+                2;
+        if (useFileTimeStamp) {
+          creationTime = timeFromFileName.microsecondsSinceEpoch;
+        }
       }
     }
     hash = mediaUploadData.hashData?.fileHash;

+ 6 - 0
lib/utils/date_time_util.dart

@@ -54,6 +54,12 @@ String getMonthAndYear(DateTime dateTime) {
   return _months[dateTime.month]! + " " + dateTime.year.toString();
 }
 
+int daysBetween(DateTime from, DateTime to) {
+  from = DateTime(from.year, from.month, from.day);
+  to = DateTime(to.year, to.month, to.day);
+  return (to.difference(from).inHours / 24).round();
+}
+
 //Thu, 30 Jun
 String getDayAndMonth(DateTime dateTime) {
   return _days[dateTime.weekday]! +