WSL/SLF GitLab Repository

Commit 21b3f1de authored by Mathias Bavay's avatar Mathias Bavay
Browse files

The "NOW±xxx" time specification has been improved (more flexible) and the...

The "NOW±xxx" time specification has been improved (more flexible) and the documentation expanded. Otherwise, code cleanup (code factorization).
parent 03c9a527
......@@ -425,74 +425,76 @@ bool convertString(Date& t, const std::string& str, const double& time_zone, std
{
std::string s(str);
trim(s); //delete trailing and leading whitespaces and tabs
stripComments(s);
//special case: NOW or NOW±xxx (offset in seconds)
(void)f;
int year;
unsigned int month, day, hour, minute;
double second;
char rest[32] = "";
const char *c_str = s.c_str();
//special case: NOW or NOW±xxx (offset in seconds or hh:mm)
if (s.substr(0, 3)=="NOW") {
t.setFromSys();
t.setTimeZone(time_zone);
if (s.size()>3) {
const size_t beg = s.find_first_of(NUM);
if (beg==npos)
throw InvalidFormatException("Invalid date specification '"+s+"'", AT);
size_t end = s.find_first_not_of(NUM, beg+1);
if (end!=npos)
unsigned int secs;
bool status = true;
if (sscanf(c_str, "NOW+%u:%u%31s", &hour, &minute, rest) >= 2) {
t += (hour*60.+minute)/(60.*24.);
} else if (sscanf(c_str, "NOW-%u:%u%31s", &hour, &minute, rest) >= 2) {
t -= (hour*60.+minute)/(60.*24.);
} else if (sscanf(c_str, "NOW+%u%31s", &secs, rest) >= 1) {
t += (secs)/(3600.*24.);
} else if (sscanf(c_str, "NOW-%u%31s", &secs, rest) >= 1) {
t -= (secs)/(3600.*24.);
} else
status = false;
if (status==false || strlen(rest)>0)
throw InvalidFormatException("Invalid date specification '"+s+"'", AT);
const int offset = atoi( s.substr(beg-1, std::string::npos).c_str());
t += static_cast<double>(offset)/(3600.*24.);
}
return true;
}
(void)f;
int year;
unsigned int month, day, hour, minute;
double second;
char rest[32] = "";
const char *c_str = s.c_str();
if (sscanf(c_str, "%d-%u-%u %u:%u:%lg%31s", &year, &month, &day, &hour, &minute, &second, rest) >= 6) {
std::string timezone_iso(rest);
stripComments(timezone_iso);
const std::string timezone_iso(rest);
const double tz = (timezone_iso.empty())? time_zone : Date::parseTimeZone(timezone_iso);
if (tz==nodata) return false;
t.setDate(year, month, day, hour, minute, second, tz);
return true;
} else if (sscanf(c_str, "%d-%u-%uT%u:%u:%lg%31s", &year, &month, &day, &hour, &minute, &second, rest) >= 6) { //ISO
std::string timezone_iso(rest);
stripComments(timezone_iso);
const std::string timezone_iso(rest);
const double tz = (timezone_iso.empty())? time_zone : Date::parseTimeZone(timezone_iso);
if (tz==nodata) return false;
t.setDate(year, month, day, hour, minute, second, tz);
return true;
} else if (sscanf(c_str, "%d-%u-%u %u:%u%31s", &year, &month, &day, &hour, &minute, rest) >= 5) {
std::string timezone_iso(rest);
stripComments(timezone_iso);
const std::string timezone_iso(rest);
const double tz = (timezone_iso.empty())? time_zone : Date::parseTimeZone(timezone_iso);
if (tz==nodata) return false;
t.setDate(year, month, day, hour, minute, static_cast<unsigned>(0), tz);
return true;
} else if (sscanf(c_str, "%d-%u-%uT%u:%u%31s", &year, &month, &day, &hour, &minute, rest) >= 5) {
std::string timezone_iso(rest);
stripComments(timezone_iso);
const std::string timezone_iso(rest);
const double tz = (timezone_iso.empty())? time_zone : Date::parseTimeZone(timezone_iso);
if (tz==nodata) return false;
t.setDate(year, month, day, hour, minute, static_cast<unsigned>(0), tz);
return true;
} else if (sscanf(c_str, "%d-%u-%u%31s", &year, &month, &day, rest) >= 3) {
std::string timezone_iso(rest);
stripComments(timezone_iso);
const std::string timezone_iso(rest);
const double tz = (timezone_iso.empty())? time_zone : Date::parseTimeZone(timezone_iso);
if (tz==nodata) return false;
t.setDate(year, month, day, static_cast<unsigned>(0), static_cast<unsigned>(0), static_cast<unsigned>(0), tz);
return true;
} else if (sscanf(c_str, "%u:%u%31s", &hour, &minute, rest) >= 2) {
std::string timezone_iso(rest);
stripComments(timezone_iso);
const std::string timezone_iso(rest);
const double tz = (timezone_iso.empty())? time_zone : Date::parseTimeZone(timezone_iso);
if (tz==nodata) return false;
t.setDate( (static_cast<double>(hour))/24. + (static_cast<double>(minute))/24./60. , tz);
......
......@@ -52,22 +52,22 @@ namespace mio {
* When parsing a string to extract a date, various string representations are supported (the timezone term {TZ} is optional \em if a
* fallback timezone is available, most probably as Input::TIME_ZONE in the configuration file):
* - <A HREF="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</A> variations:
* - YYYY-MM-DDThh:mm:ss{TZ}
* - YYYY-MM-DDThh:mm{TZ}
* - YYYY-MM-DDThh:mm:ss{TZ}, for example 2017-02-02T12:35:00 in the fallback time zone
* - YYYY-MM-DDThh:mm{TZ}, for example 2017-02-02T12:35+01:00
* - similar to ISO but without the 'T' marker (some systems wrongfuly reject it):
* - YYYY-MM-DD hh:mm:ss{TZ}
* - YYYY-MM-DD hh:mm{TZ}
* - YYYY-MM-DD hh:mm:ss{TZ}, for example 2017-02-02 11:35:00Z
* - YYYY-MM-DD hh:mm{TZ}, for example 2017-02-02 12:35+01
* - simplified:
* - YYYY-MM-DD{TZ}
* - hh:mm{TZ}
* - YYYY-MM-DD{TZ}, for example 2017-02-01
* - hh:mm{TZ}, for example 15:12+01:30
* - numeric:
* - YYYYMMDDHHmmss{TZ}
* - YYYYMMDDHHmm{TZ}
* - YYYYMMDDHH{TZ}
* - relative, with keyword (in this case a fallback time zone must be available from somewhere else):
* - NOW
* - NOW+{offset} (the offset is in seconds)
* - NOW-{offset} (the offset is in seconds)
* - NOW±{offset} (the offset is in seconds), for example NOW+3600
* - NOW±hh:mm (the offset is in hours and minutes), for example NOW-01:30
*
* The timezone information {TZ} is as laid out in Date::parseTimeZone.
*
......
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