mirror of
https://github.com/nxshock/backuper.git
synced 2024-11-27 00:11:01 +05:00
Autodetect datetime format
This commit is contained in:
parent
dc3eb7d9ef
commit
3a37c29de4
23
main.go
23
main.go
@ -4,7 +4,6 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -56,25 +55,9 @@ func main() {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
var t time.Time
|
||||
switch len(os.Args[4]) {
|
||||
case len("02.01.2006"):
|
||||
t, err = time.Parse("02.01.2006 15:04", os.Args[4])
|
||||
if err != nil {
|
||||
config.fatalln("time parse error:", err)
|
||||
}
|
||||
case len("02.01.2006 15:04"):
|
||||
t, err = time.Parse("02.01.2006 15:04", os.Args[4])
|
||||
if err != nil {
|
||||
config.fatalln("time parse error:", err)
|
||||
}
|
||||
case len("02.01.2006 15:04:05"):
|
||||
t, err = time.Parse("02.01.2006 15:04:05", os.Args[4])
|
||||
if err != nil {
|
||||
config.fatalln("time parse error:", err)
|
||||
}
|
||||
default:
|
||||
config.fatalln(`wrong time format, must be ["DD.MM.YYYY", "DD.MM.YYYY hh:mm", "DD.MM.YYYY hh:mm:ss"]`)
|
||||
t, err := parseTime(os.Args[4])
|
||||
if err != nil {
|
||||
config.fatalln(err)
|
||||
}
|
||||
|
||||
plan, err := config.extractionPlan(os.Args[3], t)
|
||||
|
15
utils.go
15
utils.go
@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tidwall/match"
|
||||
)
|
||||
@ -71,3 +73,16 @@ func isFilePathMatchPatterns(patterns []string, fileName string) bool {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func parseTime(s string) (time.Time, error) {
|
||||
switch len(s) {
|
||||
case len("02.01.2006"):
|
||||
return time.ParseInLocation("02.01.2006", s, time.Local)
|
||||
case len("02.01.2006 15:04"):
|
||||
return time.ParseInLocation("02.01.2006 15:04", s, time.Local)
|
||||
case len("02.01.2006 15:04:05"):
|
||||
return time.ParseInLocation("02.01.2006 15:04:05", s, time.Local)
|
||||
}
|
||||
|
||||
return time.Time{}, errors.New("unknown time format")
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@ -10,3 +11,21 @@ func TestSizeToApproxHuman(t *testing.T) {
|
||||
assert.Equal(t, "1.0 KiB", sizeToApproxHuman(1024))
|
||||
assert.Equal(t, "1.1 KiB", sizeToApproxHuman(1126))
|
||||
}
|
||||
|
||||
func TestParseTime(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected time.Time
|
||||
}{
|
||||
{"02.01.2006", time.Date(2006, 01, 02, 0, 0, 0, 0, time.Local)},
|
||||
{"02.01.2006 15:04", time.Date(2006, 01, 02, 15, 4, 0, 0, time.Local)},
|
||||
{"02.01.2006 15:04:05", time.Date(2006, 01, 02, 15, 4, 5, 0, time.Local)},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
got, err := parseTime(test.input)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, test.expected, got)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user