Metadata: Fix typo in LocationUTC() function #4622 #4946

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2025-04-23 17:54:33 +02:00
parent e59fdded1b
commit f2a89063c6
3 changed files with 23 additions and 3 deletions

View file

@ -12,5 +12,5 @@ func Strip(t time.Time) (result time.Time) {
result, _ = time.ParseInLocation("2006:01:02 15:04:05", t.Format("2006:01:02 15:04:05"), time.UTC)
return result
return result.Truncate(time.Second)
}

View file

@ -28,8 +28,8 @@ func TruncateUTC(t time.Time) time.Time {
// LocationUTC returns the time at the locale with the time zone set to UTC.
func LocationUTC(t time.Time, loc *time.Location) (result time.Time) {
var err error
if result, err = time.ParseInLocation("2006-01-02T15:04:05", t.In(loc).Format("2006-01-02T15:04:05"), time.UTC); err != nil {
return result
if result, err = time.ParseInLocation("2006-01-02T15:04:05", t.In(loc).Format("2006-01-02T15:04:05"), time.UTC); err == nil {
return result.Truncate(time.Second)
} else {
return t
}

View file

@ -35,3 +35,23 @@ func TestTruncateUTC(t *testing.T) {
assert.NotEqual(t, ns, result.Nanosecond())
}
}
func TestLocationUTC(t *testing.T) {
loc := Find("Europe/Berlin")
now := time.Now().In(loc).UTC()
ns := now.Nanosecond()
t.Logf("now: %s", now.String())
result := LocationUTC(now, loc)
t.Logf("result: %s", result.String())
assert.Equal(t, TimeUTC, result.Location())
timeZone, _ := result.Zone()
assert.Equal(t, UTC, timeZone)
assert.Equal(t, 0, result.Nanosecond())
if ns > 0 {
assert.NotEqual(t, ns, result.Nanosecond())
}
}