Enhancement

- Undefined variables are put to today in dates (if year and month is not
  specified, the current ones are considered). But if a global ones is
  specified, lower one are put to their default values (0 or 1).
This commit is contained in:
Louis-Claude Canon 2012-07-30 12:49:38 +02:00 committed by Paul Beckingham
parent d623a6f9af
commit ee0a16f663
3 changed files with 80 additions and 15 deletions

View file

@ -564,6 +564,22 @@ a D b Y H:N:S would do an output as "Mon 24 Jan 2011 11:19:42"
.RE
.RE
.RS
Undefined fields are put to their minimal valid values (1 for month and day and
0 for hour, minutes and seconds) when there is at least one more global date
field that is set. Otherwise, they are set to the corresponding values of
"now". For example:
.RE
.RS
.RS
.br
8/1/2012 with m/d/Y implies August 1, 2012 at midnight (inferred)
.br
8/1 20:40 with m/d H:N implies August 1, 2012 (inferred) at 20:40
.RE
.RE
.TP
.B weekstart=Sunday
Determines the day a week starts. Valid values are Sunday or Monday only. The

View file

@ -697,8 +697,8 @@ bool Nibbler::getDateISO (time_t& t)
// Convert to epoch.
struct tm tms = {0};
tms.tm_isdst = -1; // Requests that mktime determine summer time effect.
tms.tm_mday = day;
tms.tm_mon = month - 1;
tms.tm_mday = day;
tms.tm_year = year - 1900;
tms.tm_hour = hour;
tms.tm_min = minute;
@ -721,12 +721,12 @@ bool Nibbler::getDate (const std::string& format, time_t& t)
{
std::string::size_type i = _cursor;
int month = 0;
int day = 0;
int year = -1; // So we can check later.
int hour = 0;
int minute = 0;
int second = 0;
int month = -1; // So we can check later.
int day = -1;
int year = -1;
int hour = -1;
int minute = -1;
int second = -1;
for (unsigned int f = 0; f < format.length (); ++f)
{
@ -983,14 +983,42 @@ bool Nibbler::getDate (const std::string& format, time_t& t)
}
}
// Default the year to the current year, for formats that lack Y/y.
// By default, the most global date variables that are undefined are put to
// the current date (for instance, the year to the current year for formats
// that lack Y/y). If even 'second' is undefined, then the date is parsed as
// now.
if (year == -1)
{
time_t now = time (NULL);
struct tm* default_year = localtime (&now);
year = default_year->tm_year + 1900;
Date now = Date ();
year = now.year ();
if (month == -1)
{
month = now.month ();
if (day == -1)
{
day = now.day ();
if (hour == -1)
{
hour = now.hour ();
if (minute == -1)
{
minute = now.minute ();
if (second == -1)
second = now.second ();
}
}
}
}
}
// Put all remaining undefined date variables to their default values (0 or
// 1).
month = (month == -1) ? 1 : month;
day = (day == -1) ? 1 : day;
hour = (hour == -1) ? 0 : hour;
minute = (minute == -1) ? 0 : minute;
second = (second == -1) ? 0 : second;
// Convert to epoch.
struct tm tms = {0};
tms.tm_isdst = -1; // Requests that mktime determine summer time effect.

View file

@ -39,15 +39,15 @@ int main (int argc, char** argv)
{
#ifdef NIBBLER_FEATURE_DATE
#ifdef NIBBLER_FEATURE_REGEX
UnitTest t (382);
UnitTest t (396);
#else
UnitTest t (358);
UnitTest t (372);
#endif
#else
#ifdef NIBBLER_FEATURE_REGEX
UnitTest t (332);
UnitTest t (346);
#else
UnitTest t (308);
UnitTest t (322);
#endif
#endif
@ -581,6 +581,27 @@ int main (int argc, char** argv)
t.is (dt.hour (), 12, "ctor (std::string) -> h");
t.is (dt.minute (), 34, "ctor (std::string) -> N");
t.is (dt.second (), 56, "ctor (std::string) -> S");
n = Nibbler ("2010");
t.ok (n.getDate ("Y", ti), "Y ok");
dt = Date (ti);
t.is (dt.month (), 1, "ctor (std::string) -> m");
t.is (dt.day (), 1, "ctor (std::string) -> d");
t.is (dt.year (), 2010, "ctor (std::string) -> Y");
t.is (dt.hour (), 0, "ctor (std::string) -> h");
t.is (dt.minute (), 0, "ctor (std::string) -> N");
t.is (dt.second (), 0, "ctor (std::string) -> S");
n = Nibbler ("17:18:19");
t.ok (n.getDate ("H:N:S", ti), "H:N:S ok");
dt = Date (ti);
Date now = Date ();
t.is (dt.month (), now.month(), "ctor (std::string) -> m");
t.is (dt.day (), now.day(), "ctor (std::string) -> d");
t.is (dt.year (), now.year(), "ctor (std::string) -> Y");
t.is (dt.hour (), 17, "ctor (std::string) -> h");
t.is (dt.minute (), 18, "ctor (std::string) -> N");
t.is (dt.second (), 19, "ctor (std::string) -> S");
#endif
// bool getOneOf (const std::vector <std::string>&, std::string&);