-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChinaIdNumber.cpp
More file actions
206 lines (178 loc) · 5.78 KB
/
Copy pathChinaIdNumber.cpp
File metadata and controls
206 lines (178 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// Created by smile on 2021/5/29.
//
#include "ChinaIdNumber.h"
#include <fstream>
#include <string>
#include <locale>
#include <codecvt>
#include <sstream>
#include <stdexcept>
#include <utility>
#include <ctime>
// 构造函数实现
ChinaIdNumber::ChinaIdNumber(std::array<char, 18> &id) {
this->ProvinceId = {id[0], id[1]};
this->CityId = {id[2], id[3]};
this->CountyId = {id[4], id[5]};
this->BirthYear = {id[6], id[7], id[8], id[9]};
this->BirthMonth = {id[10], id[11]};
this->BirthDay = {id[12], id[13]};
this->OrderCode = {id[14], id[15]};
this->GenderCode = {id[16]};
this->CheckCode = {id[17]};
this->isLegal = this->CheckCode.front() == CalcCheckCode();
}
ChinaIdNumber::ChinaIdNumber(std::string &idStr) {
if (idStr.size() != 18) {
throw std::length_error("id size must be 18");
}
std::array<char, 18> id{};
for (auto idx = 0; idx < 18; idx++) {
id[idx] = idStr[idx];
}
new(this) ChinaIdNumber(id);
}
// 静态成员实现,预分配空间大小
decltype(ChinaIdNumber::areaNameMap) ChinaIdNumber::areaNameMap(1<<18); // NOLINT
void ChinaIdNumber::InitAreaNameMap(const std::string& csvFilePath) {
std::ifstream fp(csvFilePath);
std::string line; // 每行 bytes源码
size_t row = 0;
if (not fp.is_open()) {
throw std::runtime_error(csvFilePath + " not Found!");
}
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
while (std::getline(fp, line)) {
size_t col = 0;
std::stringstream lineStream(line);
std::string itemStr;
std::wstring year;
std::string areaCode;
std::wstring areaName;
// 按照逗号分隔,并赋值给year, areaId, areaName
while (getline(lineStream, itemStr, ',')) {
if (not itemStr.empty() and itemStr.back() == '\r') {
itemStr.erase(itemStr.size() - 1);
}
if (col == 0) { // 标准年份
year = std::move(converter.from_bytes(itemStr));
} else if (col == 1) { // 地区码
areaCode = std::move(itemStr);
} else if (col == 2) {
areaName = std::move(converter.from_bytes(itemStr));
}
col++;
}
areaNameMap[areaCode] = {areaName, year};
row++;
}
}
std::wstring ChinaIdNumber::GetAreaInfo() {
std::wstring areaFullName, releaseYear;
auto areaCode = this->GetAreaCode();
decltype(areaNameMap)::iterator iter;
iter = areaNameMap.find(areaCode.substr(0,2) + "0000");
if (iter != areaNameMap.end()) {
areaFullName.append(iter->second.first);
releaseYear = iter->second.second;
}
iter = areaNameMap.find(areaCode.substr(0,4) + "00");
if (iter != areaNameMap.end()) {
areaFullName.append(iter->second.first);
releaseYear = iter->second.second;
}
iter = areaNameMap.find(areaCode);
if (iter != areaNameMap.end()) {
areaFullName.append(iter->second.first);
releaseYear = iter->second.second;
}
return areaFullName + L"(" + releaseYear + L"年标准)";
}
std::wstring ChinaIdNumber::GetBirthInfo() {
std::wstringstream wss;
for (const auto &Ch: this->BirthYear) {
wss << Ch;
}
wss << L"年";
for (const auto &Ch: this->BirthMonth) {
wss << Ch;
}
wss << L"月";
for (const auto &Ch: this->BirthDay) {
wss << Ch;
}
wss << L"日";
return wss.str();
}
std::wstring ChinaIdNumber::GetGenderInfo() {
if (((this->GenderCode.front() - '0') & 1) == 1) {
// 奇数代表
return L"男";
}
return L"女";
}
char ChinaIdNumber::CalcCheckCode() {
const std::array<int, 17> weights = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
int sum = 0;
for (auto idx = 0; idx < weights.size(); idx++) {
sum += weights[idx] * (this->getCharByIdx(idx) - '0');
}
char checkCode = char('0' + (12 - (sum % 11)) % 11);
return checkCode <= '9' ? checkCode : 'X';
}
char ChinaIdNumber::getCharByIdx(size_t idx) {
if (idx < 2) {
return this->ProvinceId[idx];
} else if (idx < 4) {
return this->CityId[idx - 2];
} else if (idx < 6) {
return this->CountyId[idx - 4];
} else if (idx < 10) {
return this->BirthYear[idx - 6];
} else if (idx < 12) {
return this->BirthMonth[idx - 10];
} else if (idx < 14) {
return this->BirthDay[idx - 12];
} else if (idx < 16) {
return this->OrderCode[idx - 14];
} else if (idx < 17) {
return this->GenderCode[idx - 16];
} else {
throw std::out_of_range("idx must be >= 0 and <= 16");
}
}
std::string ChinaIdNumber::GetAreaCode() {
std::stringstream ss;
for (const auto &item : this->ProvinceId) {
ss << item;
}
for (const auto &item : this->CityId) {
ss << item;
}
for (const auto &item : this->CountyId) {
ss << item;
}
return ss.str();
}
std::wstring ChinaIdNumber::GetAgeInfo() {
return std::to_wstring(this->calcAge()) + L"岁";
}
int ChinaIdNumber::calcAge() {
std::time_t t = std::time(nullptr); // get time now
std::tm *now = std::localtime(&t);
int birthYear = (BirthYear[0] - '0') * 1000
+ (BirthYear[1] - '0') * 100
+ (BirthYear[2] - '0') * 10
+ (BirthYear[3] - '0');
int birthMonth = (BirthMonth[0] - '0') * 10 + (BirthMonth[1] - '0');
int birthDay = (BirthDay[0] - '0') * 10 + (BirthDay[1] - '0');
// 当前年份:t.tm_year + 1900
// 当前月份:t.tm_mon + 1
// 当前日:t.tm_mday
auto age = now->tm_year + 1900 - birthYear;
if (now->tm_mon + 1 < birthMonth or (now->tm_mon + 1 == birthMonth and now->tm_mday < birthDay)) {
age--;
}
return age;
}