Accept YYYYMMDD format in filenames (e.g. WhatsApp)
This commit is contained in:
parent
7910994166
commit
37c40c9b3d
3 changed files with 39 additions and 6 deletions
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
var DateRegexp = regexp.MustCompile("\\D\\d{4}[\\-_]\\d{2}[\\-_]\\d{2,}")
|
||||
var DatePathRegexp = regexp.MustCompile("\\D\\d{4}/\\d{1,2}/?\\d*")
|
||||
var DateNoSepRegexp = regexp.MustCompile("\\D(?P<year>\\d{4})(?P<month>\\d{2})(?P<day>\\d{2})\\D")
|
||||
var DateTimeRegexp = regexp.MustCompile("\\D\\d{2,4}[\\-_]\\d{2}[\\-_]\\d{2}.{1,4}\\d{2}\\D\\d{2}\\D\\d{2,}")
|
||||
var DateIntRegexp = regexp.MustCompile("\\d{1,4}")
|
||||
var YearRegexp = regexp.MustCompile("\\d{4,5}")
|
||||
|
|
|
@ -73,6 +73,38 @@ func DateFromFilePath(s string) (result time.Time) {
|
|||
return result
|
||||
}
|
||||
|
||||
result = time.Date(
|
||||
year,
|
||||
time.Month(month),
|
||||
day,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
time.UTC)
|
||||
} else if found = DateNoSepRegexp.Find(b); len(found) > 0 { // Is it a date path like "20200103"?
|
||||
match := DateNoSepRegexp.FindSubmatch(b)
|
||||
|
||||
if len(match) != 4 {
|
||||
return result
|
||||
}
|
||||
|
||||
matchMap := make(map[string]string)
|
||||
for i, name := range DateNoSepRegexp.SubexpNames() {
|
||||
if i != 0 {
|
||||
matchMap[name] = string(match[i])
|
||||
}
|
||||
}
|
||||
|
||||
year := ExpandYear(matchMap["year"])
|
||||
month := Int(matchMap["month"])
|
||||
day := Int(matchMap["day"])
|
||||
|
||||
// Perform date plausibility check.
|
||||
if year < YearMin || year > YearMax || month < MonthMin || month > MonthMax || day < DayMin || day > DayMax {
|
||||
return result
|
||||
}
|
||||
|
||||
result = time.Date(
|
||||
year,
|
||||
time.Month(month),
|
||||
|
|
|
@ -61,13 +61,13 @@ func TestDateFromFilePath(t *testing.T) {
|
|||
})
|
||||
t.Run("/2020/1212/20130518_142022_3D657EBD.jpg", func(t *testing.T) {
|
||||
result := DateFromFilePath("/2020/1212/20130518_142022_3D657EBD.jpg")
|
||||
//assert.False(t, result.IsZero())
|
||||
assert.True(t, result.IsZero())
|
||||
assert.False(t, result.IsZero())
|
||||
assert.Equal(t, "2013-05-18 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
t.Run("20130518_142022_3D657EBD.jpg", func(t *testing.T) {
|
||||
result := DateFromFilePath("20130518_142022_3D657EBD.jpg")
|
||||
//assert.False(t, result.IsZero())
|
||||
assert.True(t, result.IsZero())
|
||||
assert.False(t, result.IsZero())
|
||||
assert.Equal(t, "2013-05-18 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
t.Run("telegram_2020_01_30_09_57_18.jpg", func(t *testing.T) {
|
||||
result := DateFromFilePath("telegram_2020_01_30_09_57_18.jpg")
|
||||
|
@ -237,10 +237,10 @@ func TestDateFromFilePath(t *testing.T) {
|
|||
})
|
||||
t.Run("IMG-20191120-WA0001.jpg", func(t *testing.T) {
|
||||
result := DateFromFilePath("IMG-20191120-WA0001.jpg")
|
||||
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
|
||||
assert.Equal(t, "2019-11-20 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
t.Run("VID-20191120-WA0001.jpg", func(t *testing.T) {
|
||||
result := DateFromFilePath("VID-20191120-WA0001.jpg")
|
||||
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
|
||||
assert.Equal(t, "2019-11-20 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue