From 9a699f234d5c0c7beaa1a6bb86f585a8dee384bf Mon Sep 17 00:00:00 2001 From: Luca Carlon Date: Thu, 29 Jun 2023 10:36:16 +0200 Subject: [PATCH] Metadata: When parsing dates from exiftool, remap year 0 to 1 (#2508) Year 0 is frequently used in exiftool but is not accepted in the Go time package. time package uses year 1 for "zero" dates instead. Therefore, remap year 0 from exiftool to year 1. --- pkg/txt/datetime.go | 9 ++++++++- pkg/txt/datetime_test.go | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/txt/datetime.go b/pkg/txt/datetime.go index 8b8db77b4..31bbd7a74 100644 --- a/pkg/txt/datetime.go +++ b/pkg/txt/datetime.go @@ -132,8 +132,15 @@ func DateTime(s, timeZone string) (t time.Time) { } // Create rounded timestamp from parsed input values. + // Year 0 is treated separately as it has a special meaning in exiftool. Golang + // does not seem to accept value 0 for the year, but considers a date to be + // "zero" when year is 1. + year := IntVal(m[v["year"]], 0, YearMax, time.Now().Year()) + if year == 0 { + year = 1 + } t = time.Date( - IntVal(m[v["year"]], 1, YearMax, time.Now().Year()), + year, time.Month(IntVal(m[v["month"]], 1, 12, 1)), IntVal(m[v["day"]], 1, 31, 1), IntVal(m[v["h"]], 0, 23, 0), diff --git a/pkg/txt/datetime_test.go b/pkg/txt/datetime_test.go index 2f46bfd1c..0f3ce205b 100644 --- a/pkg/txt/datetime_test.go +++ b/pkg/txt/datetime_test.go @@ -119,6 +119,9 @@ func TestDateTime(t *testing.T) { assert.Equal(t, "2020-10-17 17:48:24.9508123 +0000 UTC", result.UTC().String()) assert.Equal(t, "2020-10-17 17:48:24.9508123", result.Format("2006-01-02 15:04:05.999999999")) }) + t.Run("UTC/0000:00:00 00:00:00", func(t *testing.T) { + assert.True(t, DateTime("0000:00:00 00:00:00", "UTC").IsZero()) + }) t.Run("2022-09-03T17:48:26-07:00", func(t *testing.T) { result := DateTime("2022-09-03T17:48:26-07:00", "") assert.Equal(t, "2022-09-04 00:48:26 +0000 UTC", result.UTC().String())