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.
43 lines
1.0 KiB
43 lines
1.0 KiB
package IdCardUtil
|
|
|
|
import (
|
|
"github.com/bluesky335/IDCheck/IdNumber"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// 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
|
|
}
|