diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..32fba11 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Deloz + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..42c7096 --- /dev/null +++ b/README.md @@ -0,0 +1,65 @@ +# 身份证号码验证器 + +这是一个用于验证和提取身份证号码信息的 Go 语言包。它提供了功能强大的方法来检查身份证号码的有效性,并从中获取出生日期和年龄。 + +## 功能 + +- 验证身份证号码的合法性 +- 提取身份证号码中的出生日期 +- 计算根据出生日期得到的年龄 + +## 使用方法 + +### 安装 + +使用 Go 模块管理,可以通过以下命令安装: + +```shell +go get -u github.com/deloz/idcardnumber +``` + +### 示例 + +```go +package main + +import ( + "fmt" + + "github.com/deloz/idcardnumber" +) + +func main() { + id := "320124198701011234" // 这里替换为要验证的身份证号码 + + // 验证身份证号码合法性 + err := idcardnumber.Validator(id) + if err != nil { + fmt.Println("Error:", err) + return + } + + // 创建一个新的身份证号码对象 + card := idcardnumber.New(id) + + // 获取出生日期 + birthday := card.Birthday() + fmt.Println("出生日期:", birthday) + + // 计算年龄 + age, err := card.Age() + if err != nil { + fmt.Println("Error:", err) + return + } + fmt.Println("年龄:", age) +} +``` + +## 贡献 + +欢迎贡献代码!如果您有任何建议或发现了 bug,请在 GitHub 上提交 issue 或者提出 pull request。 + +## 许可证 + +本项目基于 MIT 许可证。详情请参阅 [LICENSE](LICENSE) 文件。 \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c7b82eb --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/deloz/idcardnumber + +go 1.22.1 + +require github.com/bluesky335/IDCheck v0.0.0-20220430111809-304a8a8b679f diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..82aa1c4 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/bluesky335/IDCheck v0.0.0-20220430111809-304a8a8b679f h1:8QIyDoBLFumVy68k82pI4booLI2nR2fPG/VxqARGSmE= +github.com/bluesky335/IDCheck v0.0.0-20220430111809-304a8a8b679f/go.mod h1:PD4Jsv8cyK6DDv/UlGpnr4qmIq1+2ii5xSGgBfO1Sho= diff --git a/idcardnumber.go b/idcardnumber.go new file mode 100644 index 0000000..2bbd991 --- /dev/null +++ b/idcardnumber.go @@ -0,0 +1,90 @@ +package idcardnumber + +import ( + "errors" + "fmt" + "strconv" + "time" + + "github.com/bluesky335/IDCheck/IDNumber" +) + +type IDCardNumber struct { + number IDNumber.IDNumber +} + +func (i IDCardNumber) Valid() bool { + return i.number.IsValid() +} + +func (i IDCardNumber) Birthday() string { + b := i.number.GetBirthday() + + return fmt.Sprintf("%s-%s-%s", b.Year, b.Month, b.Day) +} + +func (i IDCardNumber) Age() (int, error) { + b := i.number.GetBirthday() + + year, err := strconv.Atoi(b.Year) + if err != nil { + return 0, errors.New("出生年份不正确") + } + + month, err := strconv.Atoi(b.Month) + if err != nil { + return 0, errors.New("出生月份不正确") + } + + day, err := strconv.Atoi(b.Day) + if err != nil { + return 0, errors.New("出生日期不正确") + } + + age := CalculateAgeFromBirthday(year, month, day) + + return age, nil +} + +// CalculateAgeFromBirthday 根据出生日期计算年龄 +func CalculateAgeFromBirthday(year, month, day int) int { + now := time.Now() + + age := now.Year() - year + + if now.Month() < time.Month(month) || (now.Month() == time.Month(month) && now.Day() < day) { + age-- + } + + return age +} + +// ParseAgeFromBirthday 根据出生日期计算年龄 +// birthdayLayout: 出生日期格式, 如: "2006-01-02" +// s: 出生日期 +func ParseAgeFromBirthday(birthdayLayout, s string) (int, error) { + t, err := time.Parse(birthdayLayout, s) + if err != nil { + return 0, errors.New("出生日期格式不正确") + } + + age := CalculateAgeFromBirthday(t.Year(), int(t.Month()), t.Day()) + + return age, nil +} + +func New(number string) *IDCardNumber { + return &IDCardNumber{ + number: IDNumber.New(number), + } +} + +// Validator 验证身份证号码 +// number: 身份证号码 +func Validator(number string) error { + if !New(number).Valid() { + return errors.New("身份证号码不合法") + } + + return nil +}