golang 时间格式化要指定默认的格式,如果不指定默认的格式就会出现错误,如果一个未知的时间格式需要做格式化,是比较棘手的。
下面是我在项目写的一个时间格式化函数,简单的支持几种格式
package util
import (
"strings"
"time"
)
func TimeParseLocal(in string) (out time.Time, err error) {
birStrLen := strings.Count(in, "") - 1
switch birStrLen {
case 10:
//2006-01-02
return TimeParse10(in)
case 19:
//2006-01-02 15:04:05
return TimeParse19(in)
default:
}
return
}
// yyyy-mm-dd | mm-dd-yyyy 格式
func TimeParse10(in string) (out time.Time, err error) {
chars := []rune(in)
fc := ""
for k, char := range chars {
if char == '-' || char == '/' || char == ':' || char == '.' {
fc = string(char)
}
switch {
case (k == 0) && ((char <= ('Z') && char >= ('A')) || (char <= ('z') && char >= ('a'))):
// 这个是 Jun-MM-YYYY 格式
case (k == 2) && (char == '-' || char == '/' || char == ':' || char == '.'):
return TimeParseMMDDYYYY(in, string(char))
case (k == 4) && (char == '-' || char == '/' || char == ':' || char == '.'):
return TimeParseYYYYMMDD(in, string(char))
}
}
return TimeParseYYYYMMDD(in, fc)
}
// yyyy-mm-dd hh:mm:ss | mm-dd-yyyy hh:mm:ss 格式
func TimeParse19(in string) (out time.Time, err error) {
chars := []rune(in)
fc := ""
for k, char := range chars {
if char == '-' || char == '/' || char == ':' || char == '.' {
fc = string(char)
}
switch {
case (k == 0) && ((char <= ('Z') && char >= ('A')) || (char <= ('z') && char >= ('a'))):
// 这个是 Jun-MM-YYYY 格式
case (k == 2) && (char == '-' || char == '/' || char == ':' || char == '.'):
return TimeParseMMDDYYYYHHMMSS(in, string(char))
case (k == 4) && (char == '-' || char == '/' || char == ':' || char == '.'):
return TimeParseYYYYMMDDHHMMSS(in, string(char))
}
}
return TimeParseYYYYMMDDHHMMSS(in, fc)
}
func TimeParseYYYYMMDD(in string, sub string) (out time.Time, err error) {
layout := "2006" + sub + "01" + sub + "02"
out, err = time.ParseInLocation(layout, in, time.Local)
if err != nil {
return
}
return
}
func TimeParseMMDDYYYY(in string, sub string) (out time.Time, err error) {
layout := "01" + sub + "02" + sub + "2006"
out, err = time.ParseInLocation(layout, in, time.Local)
if err != nil {
return
}
return
}
func TimeParseYYYYMMDDHHMMSS(in string, sub string) (out time.Time, err error) {
layout := "2006" + sub + "01" + sub + "02 15:04:05"
out, err = time.ParseInLocation(layout, in, time.Local)
if err != nil {
return
}
return
}
func TimeParseMMDDYYYYHHMMSS(in string, sub string) (out time.Time, err error) {
layout := "01" + sub + "02" + sub + "2006 15:04:05"
out, err = time.ParseInLocation(layout, in, time.Local)
if err != nil {
return
}
return
}
测试
package util
import (
"testing"
)
func TestTimeParseLocal(t *testing.T) {
t.Log(TimeParseLocal("2019/03/26"))
t.Log(TimeParseLocal("03/26/2019"))
t.Log(TimeParseLocal("2019/03/26 17:30:03"))
t.Log(TimeParseLocal("03/26/2019 17:30:03"))
}
有疑问加站长微信联系(非本文作者)