Metadata: Apply existing time zone when changing TakenAt

This commit is contained in:
Michael Mayer 2021-01-26 13:00:54 +01:00
parent 91e6a33197
commit 05b417acfa
2 changed files with 28 additions and 17 deletions

View file

@ -857,6 +857,11 @@ func (m *Photo) SetTakenAt(taken, local time.Time, zone, source string) {
return
}
// Default local time to taken if zero or invalid.
if local.IsZero() || local.Year() < 1000 {
local = taken
}
// Round times to avoid jitter.
taken = taken.Round(time.Second).UTC()
local = local.Round(time.Second)
@ -866,23 +871,21 @@ func (m *Photo) SetTakenAt(taken, local time.Time, zone, source string) {
return
}
// Set UTC time and date source.
m.TakenAt = taken
m.TakenSrc = source
if local.IsZero() || local.Year() < 1000 {
m.TakenAtLocal = m.TakenAt
} else {
m.TakenAtLocal = local
}
// Set time zone.
if zone != "" {
// Convert time and set zone as needed.
if loc, err := time.LoadLocation(zone); err == nil && zone != "" {
// Apply new time zone.
m.TimeZone = zone
}
// Apply time zone.
if m.TimeZone != "" {
m.TakenAt = m.GetTakenAt()
m.TakenAtLocal = m.TakenAt.In(loc)
} else if m.TimeZone == "" {
// Ignore time zone.
m.TakenAtLocal = local
} else if loc, err := time.LoadLocation(m.TimeZone); err == nil {
// Apply existing time zone.
m.TakenAtLocal = m.TakenAt.In(loc)
}
m.UpdateDateFields()

View file

@ -575,11 +575,19 @@ func TestPhoto_SetTakenAt(t *testing.T) {
assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAt)
assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAtLocal)
m.SetTakenAt(time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC),
time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), "Europe/Berlin", SrcName)
newTime := time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC)
newLoc, _ := time.LoadLocation("Europe/Berlin")
assert.Equal(t, time.Date(2013, 11, 11, 8, 7, 18, 0, time.UTC), m.TakenAt)
assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAtLocal)
m.SetTakenAt(newTime, newTime, "Europe/Berlin", SrcName)
/*
t.Logf("TakenAt: %s\n", m.TakenAt)
t.Logf("TakenAtLocal: %s\n", m.TakenAtLocal)
t.Logf("Expected: %s\n", newTime.In(newLoc))
*/
assert.Equal(t, newTime, m.TakenAt)
assert.Equal(t, newTime.In(newLoc), m.TakenAtLocal)
})
t.Run("time > max year", func(t *testing.T) {
m := PhotoFixtures.Get("Photo15")