Skip to content

armgit / ente

Public Imported from GitHub
0

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

zero pad

Manav Rathi3ff384156538
diff --git a/web/packages/new/photos/services/exif-update.ts b/web/packages/new/photos/services/exif-update.ts
index 7393396ef9..814eb71c68 100644
--- a/web/packages/new/photos/services/exif-update.ts
+++ b/web/packages/new/photos/services/exif-update.ts
@@ -119,6 +119,42 @@ const dataURLToBlob = (dataURI: string) =>
/**
* Convert the given {@link Date} to a format that is expected by Exif for the
* DateTimeOriginal tag.
+ *
+ * [Note: Exif dates]
+ *
+ * Common Exif date time tags, an in particular "DateTimeOriginal", are
+ * specified in the form:
+ *
+ * yyyy:MM:DD HH:mm:ss
+ *
+ * These values thus do not have an associated UTC offset or TZ. The common
+ * convention (based on my current understanding) is that these times are
+ * interpreted to be the local time where the photo was taken.
+ *
+ * Recently, there seems to be increasing support for the (newly standardized)
+ * "OffsetTimeOriginal" and related fields, which specifies time zone for
+ * "DateTimeOriginal" (and related fields).
+ *
+ * However, when the offset time tag is not present (a frequent occurrence, not
+ * just for older photos but also for screenshots generated by OSes as of 2024),
+ * we don't really know, and stick with the common convention:
+ *
+ * - When reading, assume that the Exif date is in the local TZ when deriving
+ * a UTC timestamp from it.
+ *
+ * - When writing, convert the UTC timestamp to local time.
*/
-const convertToExifDateFormat = (date: Date) =>
- `${date.getFullYear()}:${date.getMonth() + 1}:${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
+const convertToExifDateFormat = (date: Date) => {
+ // TODO: Exif - Handle offsettime if present
+ const yyyy = date.getFullYear();
+ const MM = zeroPad2(date.getMonth() + 1);
+ const dd = zeroPad2(date.getDate());
+ const HH = zeroPad2(date.getHours());
+ const mm = zeroPad2(date.getMinutes());
+ const ss = zeroPad2(date.getSeconds());
+
+ return `${yyyy}:${MM}:${dd} ${HH}:${mm}:${ss}`;
+};
+
+/** Zero pad the given number to 2 digits. */
+const zeroPad2 = (n: number) => (n < 10 ? `0${n}` : `${n}`);