Skip to content

Commit

Permalink
fix TakeTimeFromName tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simulot committed Oct 14, 2023
1 parent 9fc19c5 commit 6b4c945
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
5 changes: 5 additions & 0 deletions helpers/tzone/timezone.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ var (
onceSetLocal sync.Once
)

// return the local location
// use tzlocal package
//
// to determine the local even on Windows
// check the env variable TZ
func Local() (*time.Location, error) {
onceSetLocal.Do(func() {
var tz string
Expand Down
10 changes: 8 additions & 2 deletions immich/metadata/namesdate.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package metadata

import (
"immich-go/helpers/tzone"
"regexp"
"strconv"
"time"
)

// TakeTimeFromName extracts time components from the given name string and returns a time.Time value.
// The name string is expected to contain digits representing year, month, day, hour, minute, and second.
// The name string is expected to contain digits representing year, month, day, hour, minute, and second in local.
// Note: Pixel phone names photos with the UTC time
//
// Return a time.Time value created using the parsed time components.
// The location is set to time.UTC for consistency.
Expand All @@ -16,6 +18,10 @@ import (
var guessTimePattern = regexp.MustCompile(`(\d{4})\D?(\d\d)\D?(\d\d)\D?(\d\d)?\D?(\d\d)?\D?(\d\d)?`)

func TakeTimeFromName(name string) time.Time {
local, err := tzone.Local()
if err != nil {
panic(err)
}
mm := guessTimePattern.FindStringSubmatch(name)
m := [7]int{}
if len(mm) >= 4 {
Expand All @@ -25,7 +31,7 @@ func TakeTimeFromName(name string) time.Time {
}

}
t := time.Date(m[0], time.Month(m[1]), m[2], m[3], m[4], m[5], 0, time.Local)
t := time.Date(m[0], time.Month(m[1]), m[2], m[3], m[4], m[5], 0, local)
if t.Year() != m[0] || t.Month() != time.Month(m[1]) || t.Day() != m[2] ||
t.Hour() != m[3] || t.Minute() != m[4] || t.Second() != m[5] {
// Date is invalid, return an error or default time value
Expand Down
29 changes: 19 additions & 10 deletions immich/metadata/namesdate_test.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,55 @@
package metadata

import (
"reflect"
"immich-go/helpers/tzone"
"os"
"testing"
"time"
)

func TestTakeTimeFromName(t *testing.T) {
os.Setenv("TZ", "Europe/Paris")
local, err := tzone.Local()

if err != nil {
t.Error(err)
return
}
tests := []struct {
name string
expected time.Time
}{
{
name: "PXL_20220909_154515546.TS.mp4",
expected: time.Date(2022, 9, 9, 15, 45, 15, 0, time.UTC),
expected: time.Date(2022, 9, 9, 15, 45, 15, 0, local),
},
{
name: "Screenshot from 2022-12-17 19-45-43.png",
expected: time.Date(2022, 12, 17, 19, 45, 43, 0, time.UTC),
expected: time.Date(2022, 12, 17, 19, 45, 43, 0, local),
},
{
name: "Bebop2_20180719194940+0200.mp4",
expected: time.Date(2018, 07, 19, 19, 49, 40, 0, time.UTC),
expected: time.Date(2018, 07, 19, 19, 49, 40, 0, local),
},
{
name: "AR_EFFECT_20141126193511.mp4",
expected: time.Date(2014, 11, 26, 19, 35, 11, 0, time.UTC),
expected: time.Date(2014, 11, 26, 19, 35, 11, 0, local),
},
{
name: "2023-07-20 14:15:30", // Format: YYYY-MM-DD HH:MM:SS
expected: time.Date(2023, 7, 20, 14, 15, 30, 0, time.UTC),
expected: time.Date(2023, 7, 20, 14, 15, 30, 0, local),
},
{
name: "20001010120000", // Format: YYYYMMDDHHMMSS
expected: time.Date(2000, 10, 10, 12, 0, 0, 0, time.UTC),
expected: time.Date(2000, 10, 10, 12, 0, 0, 0, local),
},
{
name: "2023_07_20_10_09_20.mp4",
expected: time.Date(2023, 07, 20, 10, 9, 20, 0, time.UTC),
expected: time.Date(2023, 07, 20, 10, 9, 20, 0, local),
},
{
name: "19991231",
expected: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC),
expected: time.Date(1999, 12, 31, 0, 0, 0, 0, local),
},
{
name: "991231-125200",
Expand All @@ -52,9 +60,10 @@ func TestTakeTimeFromName(t *testing.T) {
expected: time.Time{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TakeTimeFromName(tt.name); !reflect.DeepEqual(got, tt.expected) {
if got := TakeTimeFromName(tt.name); !got.Equal(tt.expected) {
t.Errorf("GuessTimeTakeOnName() = %v, want %v", got, tt.expected)
}
})
Expand Down

0 comments on commit 6b4c945

Please sign in to comment.