如何告诉我的结构体tm是否在无效状态? [英] How Can I Tell if My struct tm Has Been Left in an Invalid State?

查看:337
本文介绍了如何告诉我的结构体tm是否在无效状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于无效输入的问题, 格式无效。例如给出以下代码:

This is a question about invalid input, not invalid formatting. For example given the following code:

tm bar;

foo >> get_time(&bar, "%Y-%m-%d");

cout >> bar.tm_year >> bar.tm_mon >> bar.tm_mday >> endl;

这很好,如果我定义: stringstream foo(2001-02- 28 non-leap year); 如果我有一个无效的格式,如: stringstream foo(bad format 2001-02-28 non-leap year );

This is fine if I define: stringstream foo("2001-02-28 non-leap year"); And has a clear error if I have invalid format such as: stringstream foo("bad format 2001-02-28 non-leap year");

但我不知道如何检测我的输入是否无效,例如:

But I don't know how to detect if my input was invalid for example:

stringstream foo("2001-02-30 non-leap year");

在这种情况下, bar 虽然没有错。有没有什么我可以听,这会提醒我输入无效。

In this case bar can be read as though nothing was wrong. Is there something I can listen for which will alert me that the input was invalid?

推荐答案

http://stackoverflow.com/users/214671/matteo-italia\"> Matteo Italia 的答案,除了写你自己的格里高利验证器,你唯一可靠的复选是 mktime 。使用 get_time 获得的 tm 无法验证,因为字段可能且可能不会填写。考虑以下2个无效示例:

As is mentioned by Matteo Italia's answer, apart from writing your own Gregorian validator, your only reliable double check is mktime. tms obtained with get_time cannot be validated, as fields may and may not be filled out. Consider the following 2 invalid examples:

这样做: istringstream(2001-02-30)> get_time(& bar,%Y-%m-%d); 导致当运行完 / code>:

Doing this: istringstream("2001-02-30") >> get_time(&bar, "%Y-%m-%d"); results in a result that would change when run thorough mktime:


bar.tm_mday :30

bar.tm_mon :1

bar.tm_year :100

bar.tm_mday : 30
bar.tm_mon : 1
bar.tm_year : 100

执行此操作时: istringstream(2001-13-30)> get_time(& bar,%Y-%m-%d); 导致在运行 mktime时不会更改的结果

While doing this: istringstream("2001-13-30") >> get_time(&bar, "%Y-%m-%d"); results in a result that would not change when run through mktime:


bar.tm_mday :30

bar.tm_mon :0

bar.tm_year :100


bar.tm_mday : 30
bar.tm_mon : 0
bar.tm_year : 100

为了使 mktime 更改所有无效日期,我们需要读取日期在不使用 get_time

In order to have all invalid dates altered by mktime we need to read the date in without the use of get_time:

int year;
int month;
int day;
istringstream foo("2000-13-30");

foo >> year;
foo.ignore();
foo >> month;
foo.ignore();
foo >> day;

tm bar = { 0, 0, 0, day, month - 1, year - 1900 };

此时, bar 中的任何错误通过 mktime 更正,因此我们的实际验证步骤将是检查非 - time_t(-1)返回和比较修改的 bar 回到原始值:
if(time_t(-1) != mktime(& bar)&& bar.tm_mday == day&& bar.tm_mon == month - 1&& bar.tm_year == year - 1900) 如果此条件为真,则 bar 的输入有效。

At this point any error in bar may be corrected by mktime so our actual validation step will be to check for a non-time_t(-1) return and compare the modified bar back to the original values: if(time_t(-1) != mktime(&bar) && bar.tm_mday == day && bar.tm_mon == month - 1 && bar.tm_year == year - 1900) if this condition is true then the input to bar was valid.

可以在不写入格雷戈里验证器的情况下验证 tm ,但可以 可以验证 tm 已读入 get_time

In conclusion, it is possible to validate a tm without writing a Gregorian validator, but it is not possible to validate a tm that has been read in by get_time.

这篇关于如何告诉我的结构体tm是否在无效状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆