Files
go-easy-utils/validUtil/datetime.go
jeffery cea73b8e18 version 1.0.1 (#8)
* anyutil test

* fix:delete debug

* unit test utils

* unit test

* strUtil unit test

* fix;4, 6, 9, 11 Month determination error

* fix:The last digit of ID number is x, and the weighted sum comparison condition is wrong

* test

* test

* docs

* fix:docs

* idcard test

* test unit

* no message

* docs update

* 单元测试覆盖

---------

Co-authored-by: libin <libinjob@163.com>
Co-authored-by: 李斌 <libin1-hj@huafang.com>
2023-03-27 16:52:08 +08:00

78 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package validUtil
import (
"regexp"
"strconv"
"strings"
)
// IsTime 验证是否为时间格式HH:mm:ss
func IsTime(str string) bool {
reg := regexp.MustCompile(`^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$`)
return reg.MatchString(str)
}
// IsDate 验证是否为日期格式yyyy-MM-dd
func IsDate(str string) bool {
reg := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)
if !reg.MatchString(str) {
return false
}
parts := strings.Split(str, "-")
year, _ := strconv.Atoi(parts[0])
month, _ := strconv.Atoi(parts[1])
day, _ := strconv.Atoi(parts[2])
return isValidMonth(month, year) && isValidDay(day, month, year)
}
func isValidMonth(month, year int) bool {
switch month {
case 1, 3, 5, 7, 8, 10, 12:
return true
case 4, 6, 9, 11:
return true
case 2:
if year%4 == 0 && year%100 != 0 || year%400 == 0 {
return true
}
return false
}
return false
}
func isValidDay(day, month, year int) bool {
switch month {
case 1, 3, 5, 7, 8, 10, 12:
if day >= 1 && day <= 31 {
return true
}
case 4, 6, 9, 11:
if day >= 1 && day <= 30 {
return true
}
case 2:
if year%4 == 0 && year%100 != 0 || year%400 == 0 {
if day >= 1 && day <= 29 {
return true
}
} else {
if day >= 1 && day <= 28 {
return true
}
}
}
return false
}
// IsDateTime 验证是否为日期时间格式yyyy-MM-dd HH:mm:ss
func IsDateTime(str string) bool {
reg := regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`)
if !reg.MatchString(str) {
return false
}
if !IsDate(str[0:10]) || !IsTime(str[11:]) {
return false
}
return true
}