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

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

问题描述

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



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

解决方案

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



您还可以使用 Boost.Date_Time 解析:

  string inp(10/10/2010); 
字符串格式(%d /%m /%Y);
日期d
d = parser.parse_date(inp,format,svp);


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

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

解决方案

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.

You could also use Boost.Date_Time to parse it:

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

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

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