Duration
- Allowed durations to be specified numerically as seconds, in string form.
This flexibility allows the removal of special-case handling that stores
recurrence periods in raw form ('monthly'), reducing code size. As a
consequence this means that recurrence may now be rendered according to
Duration::formatCompact, which is desirable.
- Added supporting unit tests.
This commit is contained in:
@@ -134,7 +134,13 @@ Duration::Duration (const std::string& input)
|
||||
: mSecs (0)
|
||||
, mNegative (false)
|
||||
{
|
||||
parse (input);
|
||||
if (digitsOnly (input))
|
||||
{
|
||||
mSecs = (time_t) strtol (input.c_str (), NULL, 10);
|
||||
mNegative = false;
|
||||
}
|
||||
else
|
||||
parse (input);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -342,6 +348,12 @@ bool Duration::valid (const std::string& input)
|
||||
std::string units;
|
||||
n.getUntilEOS (units);
|
||||
|
||||
// Non-trivial value with no units means the duration is specified in
|
||||
// seconds, and therefore a time_t. Consider it valid.
|
||||
if (value != 0.0 &&
|
||||
units == "")
|
||||
return true;
|
||||
|
||||
// Auto complete against all supported durations.
|
||||
std::vector <std::string> supported;
|
||||
for (unsigned int i = 0; i < NUM_DURATIONS; ++i)
|
||||
|
||||
@@ -48,7 +48,7 @@ int convertDuration (const std::string& input)
|
||||
|
||||
int main (int argc, char** argv)
|
||||
{
|
||||
UnitTest t (629);
|
||||
UnitTest t (631);
|
||||
|
||||
Duration d;
|
||||
|
||||
@@ -506,6 +506,8 @@ int main (int argc, char** argv)
|
||||
d = Duration (364*86400), t.is (d.formatCompact (), "11mo", "364*86400 -> 11mo");
|
||||
d = Duration (365*86400), t.is (d.formatCompact (), "1.0y", "365*86400 -> 1.0y");
|
||||
|
||||
d = Duration ("86400"), t.is (d.formatCompact (), "1d", "string '86400' -> 1d");
|
||||
|
||||
t.ok (d.valid ("daily"), "valid duration daily");
|
||||
t.ok (d.valid ("day"), "valid duration day");
|
||||
t.ok (d.valid ("weekly"), "valid duration weekly");
|
||||
@@ -630,6 +632,8 @@ int main (int argc, char** argv)
|
||||
|
||||
t.ok (d.valid ("-"), "valid duration -");
|
||||
|
||||
t.ok (d.valid ("86400"), "valid duration '86400'");
|
||||
|
||||
t.notok (d.valid ("woof"), "valid duration woof = fail");
|
||||
|
||||
t.is (convertDuration ("daily"), 1, "valid duration daily");
|
||||
|
||||
Reference in New Issue
Block a user