FF4 - Duration object complete

- Replaces util.cpp:convertDuration
This commit is contained in:
Paul Beckingham 2009-05-16 21:50:04 -04:00
parent 7a03d819a0
commit fa76326aba
2 changed files with 105 additions and 5 deletions

View file

@ -25,11 +25,13 @@
//
////////////////////////////////////////////////////////////////////////////////
#include <vector>
#include "task.h"
#include "Duration.h"
////////////////////////////////////////////////////////////////////////////////
Duration::Duration ()
: mSeconds (0)
: mDays (0)
{
}
@ -37,7 +39,13 @@ Duration::Duration ()
Duration::Duration (const Duration& other)
{
throw std::string ("unimplemented Duration::Duration");
mSeconds = other.mSeconds;
mDays = other.mDays;
}
////////////////////////////////////////////////////////////////////////////////
Duration::Duration (const std::string& input)
{
parse (input);
}
////////////////////////////////////////////////////////////////////////////////
@ -46,12 +54,36 @@ Duration& Duration::operator= (const Duration& other)
throw std::string ("unimplemented Duration::operator=");
if (this != &other)
{
mSeconds = other.mSeconds;
mDays = other.mDays;
}
return *this;
}
////////////////////////////////////////////////////////////////////////////////
Duration::operator int ()
{
return (int) mDays;
}
////////////////////////////////////////////////////////////////////////////////
Duration::operator time_t ()
{
return mDays;
}
////////////////////////////////////////////////////////////////////////////////
bool Duration::operator< (const Duration& other)
{
return mDays < other.mDays;
}
////////////////////////////////////////////////////////////////////////////////
bool Duration::operator> (const Duration& other)
{
return mDays > other.mDays;
}
////////////////////////////////////////////////////////////////////////////////
Duration::~Duration ()
{
@ -60,7 +92,68 @@ Duration::~Duration ()
////////////////////////////////////////////////////////////////////////////////
void Duration::parse (const std::string& input)
{
throw std::string ("unimplemented Duration::parse");
std::string lower_input = lowerCase (input);
std::vector <std::string> supported;
supported.push_back ("daily");
supported.push_back ("day");
supported.push_back ("weekly");
supported.push_back ("weekdays");
supported.push_back ("sennight");
supported.push_back ("biweekly");
supported.push_back ("fortnight");
supported.push_back ("monthly");
supported.push_back ("bimonthly");
supported.push_back ("quarterly");
supported.push_back ("biannual");
supported.push_back ("biyearly");
supported.push_back ("annual");
supported.push_back ("semiannual");
supported.push_back ("yearly");
std::vector <std::string> matches;
if (autoComplete (lower_input, supported, matches) == 1)
{
std::string found = matches[0];
if (found == "daily" || found == "day") mDays = 1;
else if (found == "weekdays") mDays = 1;
else if (found == "weekly" || found == "sennight") mDays = 7;
else if (found == "biweekly" || found == "fortnight") mDays = 14;
else if (found == "monthly") mDays = 30;
else if (found == "bimonthly") mDays = 61;
else if (found == "quarterly") mDays = 91;
else if (found == "semiannual") mDays = 183;
else if (found == "yearly" || found == "annual") mDays = 365;
else if (found == "biannual" || found == "biyearly") mDays = 730;
}
// Support \d+ d|w|m|q|y
else
{
// Verify all digits followed by d, w, m, q, or y.
unsigned int length = lower_input.length ();
for (unsigned int i = 0; i < length; ++i)
{
if (! isdigit (lower_input[i]) &&
i == length - 1)
{
int number = ::atoi (lower_input.substr (0, i).c_str ());
switch (lower_input[length - 1])
{
case 'd': mDays = number * 1; break;
case 'w': mDays = number * 7; break;
case 'm': mDays = number * 30; break;
case 'q': mDays = number * 91; break;
case 'y': mDays = number * 365; break;
}
}
}
}
if (mDays == 0)
throw std::string ("The duration '") + input + "' was not recognized.";
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -28,19 +28,26 @@
#define INCLUDED_DURATION
#include <string>
#include <time.h>
class Duration
{
public:
Duration (); // Default constructor
Duration (const Duration&); // Copy constructor
Duration (const std::string&); // Parse
Duration& operator= (const Duration&); // Assignment operator
bool operator< (const Duration&);
bool operator> (const Duration&);
~Duration (); // Destructor
operator int ();
operator time_t ();
void parse (const std::string&);
private:
int mSeconds;
time_t mDays;
};
#endif