在C++验证出生日期
Validating Date of birth in C++
我刚刚开始学习C++。在浏览本网站上的代码时,我遇到了一个验证用户输入日期的代码。但问题是它可以接受甚至未来的值,因此这个逻辑需要调整才能接受DOB。所以我决定使用"time()"函数获取当前时间,然后将其与输入的日期进行比较。首先,我在代码中添加了两行(在下面的代码中注释的那些),它们是
time(&tNow);
和
const struct tm *now = localtime(&tNow);
这是我的代码:
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;
// function expects the string in format dd/mm/yyyy:
bool extractDate(const std::string& s, int& d, int& m, int& y){
std::istringstream is(s);
char delimiter;
if (is >> d >> delimiter >> m >> delimiter >> y) {
struct tm t = {0};
t.tm_mday = d;
t.tm_mon = m - 1;
t.tm_year = y - 1900;
t.tm_isdst = -1;
// normalize:
time_t when = mktime(&t);
time_t tNow;
// time(&tNow);
const struct tm *norm = localtime(&when);
// const struct tm *now = localtime(&tNow); /* when I uncomment this line the code
// does not accept future date */
// validate (is the normalized date still the same?):
return (norm->tm_mday == d &&
norm->tm_mon == m - 1 &&
norm->tm_year == y - 1900);
}
return false;
}
int main() {
std::string s("11/01/2015");
int d,m,y;
if (extractDate(s, d, m, y))
std::cout << "date "
<< d << "/" << m << "/" << y
<< " is valid" << std::endl;
else
std::cout << "date is invalid" << std::endl;
}
当我取消注释const struct tm *now = localtime(&tNow);
时,代码会为任何未来的日期值提供正确的输出为"无效日期"......但为什么会这样。我得到了正确的输出,但我想知道为什么。
好的,所以问题是当你多次调用它时,localtime
会返回相同的缓冲区。您需要复制结果(或使用需要额外参数的localtime_r
,但它不是那么可移植)。
这是我的代码调试会话(带有未注释的部分):
(gdb) p norm
$1 = (const tm *) 0x7ffff75aca60 <_tmbuf>
(gdb) p now
$2 = (const tm *) 0x7ffff75aca60 <_tmbuf>
我解决它的方法是这样的:
const struct tm norm = *localtime(&when);
const struct tm now = *localtime(&tNow);
// validate (is the normalized date still the same?):
return (norm.tm_mday == d &&
norm.tm_mon == m - 1 &&
norm.tm_year == y - 1900);
同一主题还有其他几种变体,但这应该有效。
大多数localtime()
实现都使用内部本地静态struct tm
。 返回的指针是指向此单个实例的指针,因此,您的案例norm
和now
指向同一个struct tm
实例,第二个调用会修改它。
一些实现可能会使用线程本地存储,以便不同线程中的使用至少获得单独的struct tm
,但这不会影响这种情况,并且不是必需的行为。
大多数文档对此都很清楚,例如链接的参考资料说:
返回值指向一个内部对象,其有效性或值可能会被任何后续调用 gmtime 或 localtime 更改。
相关文章:
- 正在尝试了解输入验证循环
- 查询SQLite数据库中的日期
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何在C++中检查2D数组中负值的输入验证
- LibGit2 SSH身份验证失败
- 如何验证用户输入月份的日期?
- 从用户那里获取"yyyy/mm/dd, hh:mm"形式的日期输入并验证C++中的日期?
- 如何在当前时间,显示标头并将随机出生日期分配给X人数(C )时如何播种随机数生成器(C )
- C++ 编写一个程序,提示用户以数字形式输入一个人的出生日期(投掷/接球)
- 在 c++ 中验证日期格式
- 如何在MySQL或C 中以哈希格式验证日期
- 有没有办法关闭提升日期和时间验证
- C++中的日期验证和转换
- 日期时间验证截止到2012年7月25日15:08:23
- 在C++验证出生日期
- 如何在 C++ 中解析和验证 std::string 中的日期
- 使用一个用bool c++验证日期的程序
- 根据您的出生日期,您的年龄以月为单位
- 尝试验证日期时未定义的引用
- c++日期验证连续运行