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.

47 lines
1.2 KiB

package IdCardUtil
import (
"github.com/bluesky335/IDCheck/IdNumber"
"strconv"
"time"
)
var weight = [17]int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
var validValue = [11]byte{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}
// Check IdCard number valid.
func IsValidIdCardNo(IdCardNo *[]byte) bool {
var id = IdNumber.New(string(*IdCardNo))
return id.IsValid()
}
// Get information from IdCard number. Birthday, gender, province mask.
func GetIdCardNoInfo(idCard string) (isValid bool, birthday string, xbm string) {
IdCardNo := []byte(idCard)
xbm = "1"
if !IsValidIdCardNo(&IdCardNo) {
isValid = false
return
}
isValid = true
// Birthday information.
nYear, _ := strconv.Atoi(string(IdCardNo[6:10]))
nMonth, _ := strconv.Atoi(string(IdCardNo[10:12]))
nDay, _ := strconv.Atoi(string(IdCardNo[12:14]))
birthdayUnix := time.Date(nYear, time.Month(nMonth), nDay, 0, 0, 0, 0, time.Local).Unix()
//将Unix时间转为时间字符串
timeTemplate := "2006-01-02 15:04:05"
birthday = time.Unix(birthdayUnix, 0).Format(timeTemplate)
// Gender information.
genderMask, _ := strconv.Atoi(string(IdCardNo[16]))
if genderMask%2 == 0 {
xbm = "2"
} else {
xbm = "1"
}
return
}