You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB

package regx
import (
"errors"
"strconv"
"time"
)
// Verify birthday
func (rr *Regx) CheckBirthdayCode(birthday string) (bool, error) {
year, _ := strconv.Atoi(birthday[:4])
month, _ := strconv.Atoi(birthday[4:6])
day, _ := strconv.Atoi(birthday[6:])
curYear, curMonth, curDay := time.Now().Date()
//Birth date greater than current date
if year < 1900 || year > curYear || month <= 0 || month > 12 || day <= 0 || day > 31 {
return false, errors.New("请检查生日部分的日期是否正确")
}
if year == curYear {
if month > int(curMonth) {
return false, errors.New("当前日期月份小于您身份证上的月份")
} else if month == int(curMonth) && day > curDay {
return false, errors.New("当前日期天数小于您身份证上的天数")
}
}
//出生日期在2月份
if 2 == month {
if rr.IsLeapYear(year) && day > 29 {
return false, errors.New("闰年2月只有29号")
} else if day > 28 {
return false, errors.New("非闰年2月只有28号")
}
} else if 4 == month || 6 == month || 9 == month || 11 == month {
if day > 30 {
return false, errors.New("小月只有30号")
}
}
return true, nil
}
// 判断是否为闰年
func (rr *Regx) IsLeapYear(year int) bool {
if year <= 0 {
return false
}
if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
return true
}
return false
}