Commit 93a7f7d1 authored by Naoki Kambe's avatar Naoki Kambe

[trac929] add more strict check for date and time format (add reverse check)

parent 87e410c0
......@@ -103,8 +103,15 @@ check_format(ConstElementPtr value, ConstElementPtr format_name) {
BOOST_FOREACH (const format_types::value_type& f, time_formats) {
if (format_name->stringValue() == f.first) {
struct tm tm;
char buf[255] = "";
memset(&tm, 0, sizeof(tm));
// reverse check
return (strptime(value->stringValue().c_str(),
f.second.c_str(), &tm) != NULL);
f.second.c_str(), &tm) != NULL
&& strftime(buf, sizeof(buf),
f.second.c_str(), &tm) != 0
&& strcmp(value->stringValue().c_str(),
buf) == 0);
}
}
return (false);
......
......@@ -287,7 +287,7 @@ TEST(ModuleSpec, CheckFormat) {
item_default = "\"item_default\": \"2011-05-27\",";
item_format = "\"item_format\": \"date\"";
specs.push_back("," + item_default + item_format);
item_default = "\"item_default\": \"19:42:57Z\",";
item_default = "\"item_default\": \"19:42:57\",";
item_format = "\"item_format\": \"time\"";
specs.push_back("," + item_default + item_format);
......
......@@ -330,8 +330,10 @@ def _check_format(value, format_name):
for fmt in time_formats:
if format_name == fmt:
try:
time.strptime(value, time_formats[fmt])
return True
# reverse check
return value == time.strftime(
time_formats[fmt],
time.strptime(value, time_formats[fmt]))
except (ValueError, TypeError):
break
return False
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment