ISO8601d: Added ::length and tests

This commit is contained in:
Paul Beckingham 2015-09-26 13:38:32 -04:00
parent 9962c14de2
commit f615db8a4c
3 changed files with 65 additions and 1 deletions

View file

@ -31,6 +31,7 @@
#include <ISO8601.h>
#include <Date.h>
#include <text.h>
#include <utf8.h>
#include <i18n.h>
#define DAY 86400
@ -815,6 +816,43 @@ int ISO8601d::dayOfWeek (const std::string& input)
return -1;
}
////////////////////////////////////////////////////////////////////////////////
int ISO8601d::length (const std::string& format)
{
int len = 0;
for (auto& i : format)
{
switch (i)
{
case 'm':
case 'M':
case 'd':
case 'D':
case 'y':
case 'v':
case 'V':
case 'h':
case 'H':
case 'n':
case 'N':
case 's':
case 'S': len += 2; break;
case 'b':
case 'j':
case 'J':
case 'a': len += 3; break;
case 'Y': len += 4; break;
case 'A':
case 'B': len += 10; break;
// Calculate the width, don't assume a single character width.
default: len += mk_wcwidth (i); break;
}
}
return len;
}
////////////////////////////////////////////////////////////////////////////////
void ISO8601p::clear ()
{

View file

@ -46,6 +46,8 @@ public:
static int dayOfWeek (const std::string&);
static int length (const std::string&);
private:
void clear ();
bool parse_date_time (Nibbler&);

View file

@ -71,7 +71,7 @@ void testParse (
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
UnitTest t (767);
UnitTest t (788);
ISO8601d iso;
std::string::size_type start = 0;
@ -227,6 +227,30 @@ int main (int argc, char** argv)
t.is (ISO8601d::dayOfWeek ("Thursday"), 4, "Thursday == 4");
t.is (ISO8601d::dayOfWeek ("Friday"), 5, "Friday == 5");
t.is (ISO8601d::dayOfWeek ("Saturday"), 6, "Saturday == 6");
// int ISO8601d::length (const std::string&);
t.is (ISO8601d::length ("m"), 2, "length 'm' --> 2");
t.is (ISO8601d::length ("M"), 2, "length 'M' --> 2");
t.is (ISO8601d::length ("d"), 2, "length 'd' --> 2");
t.is (ISO8601d::length ("D"), 2, "length 'D' --> 2");
t.is (ISO8601d::length ("y"), 2, "length 'y' --> 2");
t.is (ISO8601d::length ("Y"), 4, "length 'Y' --> 4");
t.is (ISO8601d::length ("a"), 3, "length 'a' --> 3");
t.is (ISO8601d::length ("A"), 10, "length 'A' --> 10");
t.is (ISO8601d::length ("b"), 3, "length 'b' --> 3");
t.is (ISO8601d::length ("B"), 10, "length 'B' --> 10");
t.is (ISO8601d::length ("v"), 2, "length 'v' --> 2");
t.is (ISO8601d::length ("V"), 2, "length 'V' --> 2");
t.is (ISO8601d::length ("h"), 2, "length 'h' --> 2");
t.is (ISO8601d::length ("H"), 2, "length 'H' --> 2");
t.is (ISO8601d::length ("n"), 2, "length 'n' --> 2");
t.is (ISO8601d::length ("N"), 2, "length 'N' --> 2");
t.is (ISO8601d::length ("s"), 2, "length 's' --> 2");
t.is (ISO8601d::length ("S"), 2, "length 'S' --> 2");
t.is (ISO8601d::length ("j"), 3, "length 'j' --> 3");
t.is (ISO8601d::length ("J"), 3, "length 'J' --> 3");
t.is (ISO8601d::length (" "), 1, "length ' ' --> 1");
}
catch (const std::string& e)