Metadata: Ignore date string defaults caused by software errors #3229

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2023-02-24 15:53:59 +01:00
parent 2bf6b2a7f6
commit 96ea732637
3 changed files with 89 additions and 13 deletions

View File

@ -60,8 +60,8 @@ func DateTime(s, timeZone string) (t time.Time) {
}
}()
// Empty time string?
if EmptyTime(s) {
// Ignore defaults.
if DateTimeDefault(s) {
return time.Time{}
}

View File

@ -8,7 +8,7 @@ import (
func Empty(s string) bool {
if s == "" {
return true
} else if s = strings.Trim(s, "%* "); s == "" || s == "0" || s == "-1" || EmptyTime(s) {
} else if s = strings.Trim(s, "%* "); s == "" || s == "0" || s == "-1" || DateTimeDefault(s) {
return true
} else if s = strings.ToLower(s); s == "nil" || s == "null" || s == "nan" {
return true
@ -22,10 +22,12 @@ func NotEmpty(s string) bool {
return !Empty(s)
}
// EmptyTime tests if the string is empty or matches an unknown time pattern.
func EmptyTime(s string) bool {
// EmptyDateTime tests if the string is empty or matches an unknown time pattern.
func EmptyDateTime(s string) bool {
switch s {
case "":
case "", "-", ":", "z", "zz", "Z", "nil", "null", "none", "nan", "NaN":
return true
case "0", "00", "0000", "0000:00:00", "00:00:00", "0000-00-00", "00-00-00":
return true
case "0000:00:00 00:00:00", "0000-00-00 00-00-00", "0000-00-00 00:00:00":
return true
@ -35,3 +37,20 @@ func EmptyTime(s string) bool {
return false
}
}
// DateTimeDefault tests if the datetime string is not empty and not a default value.
func DateTimeDefault(s string) bool {
switch s {
case "1970-01-01", "1970-01-01 00:00:00":
// Unix epoch.
return true
case "1980-01-01", "1980-01-01 00:00:00":
// Common default.
return true
case "2002:12:08 12:00:00":
// Android Bug: https://issuetracker.google.com/issues/36967504
return true
default:
return EmptyDateTime(s)
}
}

View File

@ -102,20 +102,77 @@ func TestNotEmpty(t *testing.T) {
})
}
func TestEmptyTime(t *testing.T) {
t.Run("EmptyString", func(t *testing.T) {
assert.True(t, EmptyTime(""))
func TestEmptyDateTime(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
assert.True(t, EmptyDateTime(""))
})
t.Run("0", func(t *testing.T) {
assert.True(t, EmptyDateTime("0"))
})
t.Run("00-00-00", func(t *testing.T) {
assert.True(t, EmptyDateTime("00-00-00"))
})
t.Run("0000-00-00", func(t *testing.T) {
assert.True(t, EmptyDateTime("0000-00-00"))
})
t.Run("00:00:00", func(t *testing.T) {
assert.True(t, EmptyDateTime("00:00:00"))
})
t.Run("0000:00:00", func(t *testing.T) {
assert.True(t, EmptyDateTime("0000:00:00"))
})
t.Run("0000-00-00 00-00-00", func(t *testing.T) {
assert.True(t, EmptyTime("0000-00-00 00-00-00"))
assert.True(t, EmptyDateTime("0000-00-00 00-00-00"))
})
t.Run("0000:00:00 00:00:00", func(t *testing.T) {
assert.True(t, EmptyTime("0000:00:00 00:00:00"))
assert.True(t, EmptyDateTime("0000:00:00 00:00:00"))
})
t.Run("0000-00-00 00:00:00", func(t *testing.T) {
assert.True(t, EmptyTime("0000-00-00 00:00:00"))
assert.True(t, EmptyDateTime("0000-00-00 00:00:00"))
})
t.Run("0001-01-01 00:00:00 +0000 UTC", func(t *testing.T) {
assert.True(t, EmptyTime("0001-01-01 00:00:00 +0000 UTC"))
assert.True(t, EmptyDateTime("0001-01-01 00:00:00 +0000 UTC"))
})
}
func TestDateTimeDefault(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
assert.True(t, DateTimeDefault(""))
})
t.Run("nil", func(t *testing.T) {
assert.True(t, DateTimeDefault("nil"))
})
t.Run("2002", func(t *testing.T) {
assert.False(t, DateTimeDefault("2002"))
})
t.Run("1970-01-01", func(t *testing.T) {
assert.True(t, DateTimeDefault("1970-01-01"))
})
t.Run("1980-01-01", func(t *testing.T) {
assert.True(t, DateTimeDefault("1980-01-01"))
})
t.Run("1970-01-01 00:00:00", func(t *testing.T) {
assert.True(t, DateTimeDefault("1970-01-01 00:00:00"))
})
t.Run("1980-01-01 00:00:00", func(t *testing.T) {
assert.True(t, DateTimeDefault("1980-01-01 00:00:00"))
})
t.Run("2002:12:08 12:00:00", func(t *testing.T) {
assert.True(t, DateTimeDefault("2002:12:08 12:00:00"))
})
t.Run("0000-00-00", func(t *testing.T) {
assert.True(t, DateTimeDefault("0000-00-00"))
})
t.Run("0000-00-00 00-00-00", func(t *testing.T) {
assert.True(t, DateTimeDefault("0000-00-00 00-00-00"))
})
t.Run("0000:00:00 00:00:00", func(t *testing.T) {
assert.True(t, DateTimeDefault("0000:00:00 00:00:00"))
})
t.Run("0000-00-00 00:00:00", func(t *testing.T) {
assert.True(t, DateTimeDefault("0000-00-00 00:00:00"))
})
t.Run("0001-01-01 00:00:00 +0000 UTC", func(t *testing.T) {
assert.True(t, DateTimeDefault("0001-01-01 00:00:00 +0000 UTC"))
})
}