调整时间参数转换
This commit is contained in:
parent
c4d3792edf
commit
b79fdaf437
|
|
@ -67,14 +67,14 @@ func (j Job) GetFileName(params map[string]interface{}) string {
|
||||||
field := string(b[1 : len(b)-1])
|
field := string(b[1 : len(b)-1])
|
||||||
|
|
||||||
if val, ok := params[field]; ok {
|
if val, ok := params[field]; ok {
|
||||||
return []byte(toString(val, false))
|
return []byte(toString(val, "20060102"))
|
||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
})
|
})
|
||||||
|
|
||||||
//安全命名
|
//安全命名
|
||||||
path, name := filepath.Split(string(fileName))
|
path, name := filepath.Split(string(fileName))
|
||||||
name, err := filenamify.Filenamify(name, filenamify.Options{Replacement: ""})
|
name, err := filenamify.Filenamify(name, filenamify.Options{Replacement: "-"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("不安全的文件名:%s", err.Error())
|
log.Printf("不安全的文件名:%s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
@ -105,14 +105,22 @@ func (t Task) GetSql(params map[string]interface{}) string {
|
||||||
return sql
|
return sql
|
||||||
}
|
}
|
||||||
|
|
||||||
func toString(parm interface{}, timestamp bool) string {
|
func toString(parm interface{}, timestamp interface{}) string {
|
||||||
switch p := parm.(type) {
|
switch p := parm.(type) {
|
||||||
case time.Time:
|
case time.Time:
|
||||||
if timestamp {
|
switch t := timestamp.(type) {
|
||||||
return strconv.FormatInt(p.Unix(), 10)
|
case bool:
|
||||||
} else {
|
if t {
|
||||||
return p.Format("2006-01-02 15:04:05")
|
return strconv.FormatInt(p.Unix(), 10)
|
||||||
|
} else {
|
||||||
|
return p.Format("2006-01-02 15:04:05")
|
||||||
|
}
|
||||||
|
case string:
|
||||||
|
return p.Format(t)
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
case string:
|
case string:
|
||||||
return p
|
return p
|
||||||
case int:
|
case int:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJob_GetFileName(t *testing.T) {
|
||||||
|
t.Run("normal", func(t *testing.T) {
|
||||||
|
|
||||||
|
j := &Job{
|
||||||
|
File: "/var/www/aa-{begin}-{end}-{task}.xls",
|
||||||
|
}
|
||||||
|
|
||||||
|
name := j.GetFileName(map[string]interface{}{
|
||||||
|
"begin": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
"end": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
"task": 1,
|
||||||
|
})
|
||||||
|
assert.Equal(t, "/var/www/aa-20230101-20230101-1.xls", name)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("deletion", func(t *testing.T) {
|
||||||
|
|
||||||
|
j := &Job{
|
||||||
|
File: "/var/www/aa-{begin}-{end}-{task}*.xls",
|
||||||
|
}
|
||||||
|
|
||||||
|
name := j.GetFileName(map[string]interface{}{
|
||||||
|
"begin": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
"end": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
})
|
||||||
|
assert.Equal(t, "/var/www/aa-20230101-20230101-{task}-.xls", name)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("err", func(t *testing.T) {
|
||||||
|
|
||||||
|
j := &Job{
|
||||||
|
File: "/var/www/aa-{begin}-{end}-{task}*.xls",
|
||||||
|
}
|
||||||
|
|
||||||
|
name := j.GetFileName(map[string]interface{}{
|
||||||
|
"begin": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
"end": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
})
|
||||||
|
assert.Equal(t, "/var/www/aa-20230101-20230101-{task}-.xls", name)
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue