我正在研究一个项目,我必须阅读一个日期以确保它是一个有效的日期。例如,2月29日只是闰年的有效日期,或者6月31日不是有效日期,因此计算机会根据输入信息输出该信息。我的问题是,我无法弄清楚如何解析字符串,以便用户可以输入“05/11/1996”作为日期(例如),然后将其放入单独的整数中。我正在考虑尝试使用while循环和字符串流做些事情,但我有点卡住了。如果有人能帮助我,我会很感激。如何在C++中解析和验证std :: string中的日期?
回答
如果格式就像你的榜样,你可以取出整数这样的:
int day, month, year;
sscanf(buffer, "%2d/%2d/%4d",
&month,
&day,
&year);
其中当然在缓冲区你有日期(“1996年5月11日”)
一个可能的解决方案也是基于strptime
,但是请注意,此功能仅验证日是否为从区间<1;31>
和一个月<1;12>
,即"30/02/2013"
是有效的还是:
#include <iostream>
#include <ctime>
int main() {
struct tm tm;
std::string s("32/02/2013");
if (strptime(s.c_str(), "%d/%m/%Y", &tm))
std::cout << "date is valid" << std::endl;
else
std::cout << "date is invalid" << std::endl;
}
但由于
strptime
并不总是可用的和额外的验证将是很好,这里是你能做什么:
- 提取日,月,年
- 填写
struct tm
- 正常化它
- 检查是否归日期仍然与检索日期,月份,年份相同
ie:
#include <iostream>
#include <sstream>
#include <ctime>
// 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);
const struct tm *norm = localtime(&when);
// the actual date would be:
// m = norm->tm_mon + 1;
// d = norm->tm_mday;
// y = norm->tm_year;
// e.g. 29/02/2013 would become 01/03/2013
// 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("29/02/2013");
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;
}
在这种情况下将输出date is invalid
因为正常化将检测29/02/2013
已被标准化为01/03/2013
。
很好的答案。当Google试图找到验证输入日期的方法时,Google搜索将我带到了这里。您的方法还处理闰年和其他边缘情况,这非常棒。 – DaV
有一个问题:如果你输入的年份低于1970年(epoch年),它将被归一化到1970年,并且该函数将失败 –
我宁愿使用升压日期时间:
#include <iostream>
#include <boost/date_time/local_time/local_time.hpp>
struct dateparser
{
dateparser(std::string fmt)
{
// set format
using namespace boost::local_time;
local_time_input_facet* input_facet = new local_time_input_facet();
input_facet->format(fmt.c_str());
ss.imbue(std::locale(ss.getloc(), input_facet));
}
bool operator()(std::string const& text)
{
ss.clear();
ss.str(text);
bool ok = ss >> pt;
if (ok)
{
auto tm = to_tm(pt);
year = tm.tm_year;
month = tm.tm_mon + 1; // for 1-based (1:jan, .. 12:dec)
day = tm.tm_mday;
}
return ok;
}
boost::posix_time::ptime pt;
unsigned year, month, day;
private:
std::stringstream ss;
};
int main(){
dateparser parser("%d/%m/%Y"); // not thread safe
// parse
for (auto&& txt : { "05/11/1996", "30/02/1983", "29/02/2000", "29/02/2001" })
{
if (parser(txt))
std::cout << txt << " -> " << parser.pt << " is the "
<< parser.day << "th of "
<< std::setw(2) << std::setfill('0') << parser.month
<< " in the year " << parser.year << "\n";
else
std::cout << txt << " is not a valid date\n";
}
}
输出:
05/11/1996 -> 1996-Nov-05 00:00:00 is the 5th of 11 in the year 96
30/02/1983 is not a valid date
29/02/2000 -> 2000-Feb-29 00:00:00 is the 29th of 02 in the year 100
29/02/2001 is not a valid date
我刚刚重构了我的答案以显示(a)如何有效地重新使用imbued流解析(b)它验证输入 – sehe
另一种选择是使用std::get_time
从<iomanip>
头(可自C++ 11以来)。它的一个很好的例子可以找到here。
- 1. JavaScript中的日期解析和验证
- 2. 在C#中解析和验证解析#
- 3. 如何解析std :: string中的rapidjson?
- 4. 解析日期与验证
- 5. 如何解析DWORD和LPBYTE为std :: string
- 6. C++中的日期验证和转换
- 7. 未在Eclipse/CDT 8.0中解析std :: string
- 8. 解析的std :: string与升压ptree中
- 9. 如何在JQuery中验证日期和日期?
- 10. C++ std :: string和string
- 11. 如何验证日历中的日期
- 12. 日期在C#中的XML解析
- 13. C++ - 从std :: string解析数字
- 14. 如何在iphone中验证日期选择器中的日期?
- 15. 在C++中验证日期格式
- 16. 在C#中的std :: string?
- 17. 如何在php中验证日期?
- 18. 如何在导轨中验证日期?
- 19. 如何在javascript中验证日期
- 20. 如何在Flash AS3中解析日期
- 21. 如何在C++中终止std :: string?
- 22. 解析和验证ruby中的任意日期格式(在rails上)
- 23. 解析和验证数字和日期时间输入
- 24. 如何在文本框中验证日期实施的日期?
- 25. 验证C#中的日期时间#
- 26. 目标c中的日期验证
- 27. 用于C中日期验证的库?
- 28. 如何验证jQuery中的datepicker日期
- 29. 如何在java中解析无法解析的日期
- 30. 在symfony2中验证日期
有没有一种方法可以使用字符串?我把它放到一些代码中,并将一个变量缓冲区设置为“05/20/13”,不幸的是它说在string和const char之间没有合适的转换。 – emufossum13
是的,如果你使用std :: string,那么buffer.c_str()就是你需要的。 – DrM
这是我如何设置变量decleration std :: string buffer = buffer。c_str(); buffer =“05/10/1996”; 是你在说什么? – emufossum13