Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix: Use exifOffset to get creationTime

Neeraj Guptac2c44acbfbda
diff --git a/lib/utils/exif_util.dart b/lib/utils/exif_util.dart
index aa7ed48873..58cbb8c9ec 100644
--- a/lib/utils/exif_util.dart
+++ b/lib/utils/exif_util.dart
@@ -11,6 +11,11 @@ import 'package:photos/utils/file_util.dart';
const kDateTimeOriginal = "EXIF DateTimeOriginal";
const kImageDateTime = "Image DateTime";
+const kExifOffSetKeys = [
+ "EXIF OffsetTime",
+ "EXIF OffsetTimeOriginal",
+ "EXIF OffsetTimeDigitized",
+];
const kExifDateTimePattern = "yyyy:MM:dd HH:mm:ss";
const kEmptyExifDateTime = "0000:00:00 00:00:00";
@@ -56,7 +61,14 @@ Future<DateTime?> getCreationTimeFromEXIF(
? exif[kImageDateTime]!.printable
: null;
if (exifTime != null && exifTime != kEmptyExifDateTime) {
- return DateFormat(kExifDateTimePattern).parse(exifTime);
+ String? exifOffsetTime;
+ for (final key in kExifOffSetKeys) {
+ if (exif.containsKey(key)) {
+ exifOffsetTime = exif[key]!.printable;
+ break;
+ }
+ }
+ return getDateTimeInDeviceTimezone(exifTime, exifOffsetTime);
}
} catch (e) {
_logger.severe("failed to getCreationTimeFromEXIF", e);
@@ -64,6 +76,32 @@ Future<DateTime?> getCreationTimeFromEXIF(
return null;
}
+DateTime getDateTimeInDeviceTimezone(String exifTime, String? offsetString) {
+ final DateTime result = DateFormat(kExifDateTimePattern).parse(exifTime);
+ if (offsetString == null) {
+ return result;
+ }
+ try {
+ final List<String> splitHHMM = offsetString.split(":");
+ // Parse the offset from the photo's time zone
+ final int offsetHours = int.parse(splitHHMM[0]);
+ final int offsetMinutes =
+ int.parse(splitHHMM[1]) * (offsetHours.isNegative ? -1 : 1);
+ // Adjust the date for the offset to get the photo's correct UTC time
+ final photoUtcDate =
+ result.add(Duration(hours: -offsetHours, minutes: -offsetMinutes));
+ // Getting the current device's time zone offset from UTC
+ final now = DateTime.now();
+ final localOffset = now.timeZoneOffset;
+ // Adjusting the photo's UTC time to the device's local time
+ final deviceLocalTime = photoUtcDate.add(localOffset);
+ return deviceLocalTime;
+ } catch (e, s) {
+ _logger.severe("tz offset adjust failed $offsetString", e, s);
+ }
+ return result;
+}
+
Location? locationFromExif(Map<String, IfdTag> exif) {
try {
return gpsDataFromExif(exif).toLocationObj();