Improve date matching regular expressions #304
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
parent
5f6c2af78e
commit
a81fa0c959
2 changed files with 30 additions and 17 deletions
|
@ -6,10 +6,10 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var DateRegexp = regexp.MustCompile("\\d{4}[\\-_]\\d{2}[\\-_]\\d{2}")
|
||||
var DateCanonicalRegexp = regexp.MustCompile("\\d{8}_\\d{6}_\\w{8}\\.")
|
||||
var DatePathRegexp = regexp.MustCompile("\\d{4}\\/\\d{1,2}\\/?\\d{0,2}")
|
||||
var DateTimeRegexp = regexp.MustCompile("\\d{4}[\\-_]\\d{2}[\\-_]\\d{2}.{1,4}\\d{2}\\D\\d{2}\\D\\d{2}")
|
||||
var DateRegexp = regexp.MustCompile("\\D\\d{4}[\\-_]\\d{2}[\\-_]\\d{2}\\D")
|
||||
var DateCanonicalRegexp = regexp.MustCompile("\\D\\d{8}_\\d{6}_\\w+\\.")
|
||||
var DatePathRegexp = regexp.MustCompile("\\D\\d{4}\\/\\d{1,2}\\/?\\d{0,2}\\D")
|
||||
var DateTimeRegexp = regexp.MustCompile("\\D\\d{4}[\\-_]\\d{2}[\\-_]\\d{2}.{1,4}\\d{2}\\D\\d{2}\\D\\d{2}\\D")
|
||||
var DateIntRegexp = regexp.MustCompile("\\d{1,4}")
|
||||
|
||||
var (
|
||||
|
@ -40,8 +40,8 @@ func Time(s string) (result time.Time) {
|
|||
|
||||
b := []byte(s)
|
||||
|
||||
if found := DateCanonicalRegexp.Find(b); len(found) == 25 { // Is it a canonical name like "20120727_093920_97425909.jpg"?
|
||||
if date, err := time.Parse("20060102_150405", string(found[0:15])); err == nil {
|
||||
if found := DateCanonicalRegexp.Find(b); len(found) > 0 { // Is it a canonical name like "20120727_093920_97425909.jpg"?
|
||||
if date, err := time.Parse("20060102_150405", string(found[1:16])); err == nil {
|
||||
result = date.Round(time.Second).UTC()
|
||||
}
|
||||
} else if found := DateTimeRegexp.Find(b); len(found) > 0 { // Is it a date with time like "2020-01-30_09-57-18"?
|
||||
|
|
|
@ -7,9 +7,9 @@ import (
|
|||
)
|
||||
|
||||
func TestTime(t *testing.T) {
|
||||
t.Run("/2020_01_30/1212/20130518_142022_3D657EBD.jpg", func(t *testing.T) {
|
||||
result := Time("/2020_01_30/1212/20130518_142022_3D657EBD.jpg")
|
||||
assert.False(t, result.IsZero())
|
||||
t.Run("/2020/1212/20130518_142022_3D657EBD.jpg", func(t *testing.T) {
|
||||
result := Time("/2020/1212/20130518_142022_3D657EBD.jpg")
|
||||
//assert.False(t, result.IsZero())
|
||||
assert.Equal(t, "2013-05-18 14:20:22 +0000 UTC", result.String())
|
||||
})
|
||||
|
||||
|
@ -49,26 +49,26 @@ func TestTime(t *testing.T) {
|
|||
assert.Equal(t, "2019-05-21 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
|
||||
t.Run("2019-05-21", func(t *testing.T) {
|
||||
result := Time("2019-05-21")
|
||||
t.Run("/2019-05-21/file2314.JPG", func(t *testing.T) {
|
||||
result := Time("/2019-05-21/file2314.JPG")
|
||||
assert.False(t, result.IsZero())
|
||||
assert.Equal(t, "2019-05-21 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
|
||||
t.Run("2019.05.21", func(t *testing.T) {
|
||||
result := Time("2019.05.21")
|
||||
t.Run("/2019.05.21", func(t *testing.T) {
|
||||
result := Time("/2019.05.21")
|
||||
assert.True(t, result.IsZero())
|
||||
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
|
||||
t.Run("05.21.2019", func(t *testing.T) {
|
||||
result := Time("05.21.2019")
|
||||
t.Run("/05.21.2019", func(t *testing.T) {
|
||||
result := Time("/05.21.2019")
|
||||
assert.True(t, result.IsZero())
|
||||
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
|
||||
t.Run("21.05.2019", func(t *testing.T) {
|
||||
result := Time("21.05.2019")
|
||||
t.Run("/21.05.2019", func(t *testing.T) {
|
||||
result := Time("/21.05.2019")
|
||||
assert.True(t, result.IsZero())
|
||||
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
|
@ -87,6 +87,12 @@ func TestTime(t *testing.T) {
|
|||
|
||||
t.Run("2019/05/21", func(t *testing.T) {
|
||||
result := Time("2019/05/21")
|
||||
assert.True(t, result.IsZero())
|
||||
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
|
||||
t.Run("/2019/05/21/foo.txt", func(t *testing.T) {
|
||||
result := Time("/2019/05/21/foo.txt")
|
||||
assert.False(t, result.IsZero())
|
||||
assert.Equal(t, "2019-05-21 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
|
@ -126,6 +132,13 @@ func TestTime(t *testing.T) {
|
|||
assert.True(t, result.IsZero())
|
||||
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
|
||||
|
||||
t.Run("545452019/1/3/foo.jpg", func(t *testing.T) {
|
||||
result := Time("/2019/1/3/foo.jpg")
|
||||
assert.False(t, result.IsZero())
|
||||
assert.Equal(t, "2019-01-03 00:00:00 +0000 UTC", result.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestInt(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue