mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-06 15:37:01 +08:00

* 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>
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
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
|
||
}
|