C ++检查日期是否有效 [英] C++ check if a date is valid

查看:130
本文介绍了C ++检查日期是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么功能检查给定的日期是否有效?
我不想从头开始写任何东西。

is there any function to check if a given date is valid or not? I don't want to write anything from scratch.

例如。 32/10/2012无效
和10/10/2010有效

e.g. 32/10/2012 is not valid and 10/10/2010 is valid

推荐答案

如果您的字符串总是在这种格式下,最简单的做法是将字符串拆分为三个组件,填充 tm 结构,并将其传递给 mktime()。如果返回-1,那么它不是有效的日期。

If your string is always in that format the easiest thing to do would be to split the string into its three components, populate a tm structure and pass it to mktime(). If it returns -1 then it's not a valid date.

您也可以使用 Boost.Date_Time 来解析:

string inp("10/10/2010");
string format("%d/%m/%Y");
date d;
d = parser.parse_date(inp, format, svp);

这篇关于C ++检查日期是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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