// function get sad if input dateTime strings are not valid ISO 8601 format so try to fix them const std::string fixDateTimeString(const char* dateTime) { std::string date_str(dateTime); // if date portion is too short, give up and return empty string if ( date_str.length() < 8 ) return std::string(); if ( date_str.find("T") == std::string::npos ) { // grab time portion if it exists std::string time_part = date_str.substr(8); // if string does not contain 'T', add it in the right place date_str = date_str.substr(0,8); date_str += "T"; // add back in the time date_str += time_part; } // the length of a correctly formatted string const unsigned int correct_length = std::string("20130101T123456").length(); // pad with '0's as required while( date_str.length() < correct_length ) { date_str += "0"; }; return date_str; }
int coprintf (const char *format, ...) { va_list argptr; /* Argument list pointer */ int fmtsize = 0; /* Size of formatted line */ char *formatted = NULL, /* Formatted line */ *prefixed = NULL; /* Prefixed formatted line */ if (console_active) { formatted = mem_alloc (LINE_MAX + 1); if (!formatted) return (0); va_start (argptr, format); /* Start variable args processing */ vsnprintf (formatted, LINE_MAX, format, argptr); va_end (argptr); /* End variable args processing */ switch (console_mode) { case CONSOLE_DATETIME: xstrcpy_debug (); prefixed = xstrcpy (NULL, date_str (), " ", time_str (), ": ", formatted, NULL); break; case CONSOLE_TIME: xstrcpy_debug (); prefixed = xstrcpy (NULL, time_str (), ": ", formatted, NULL); break; } if (console_file) { file_write (console_file, prefixed? prefixed: formatted); fflush (console_file); } if (console_fct) (console_fct) (prefixed? prefixed: formatted); if (console_echo) { fprintf (stdout, "%s", prefixed? prefixed: formatted); fprintf (stdout, "\n"); fflush (stdout); } if (prefixed) { fmtsize = strlen (prefixed); mem_free (prefixed); } else fmtsize = strlen (formatted); mem_free (formatted); } return (fmtsize); }
/*------------------------------------------------------------------------ * create datetime string "YYYY-MM-DDThh:mm:ss.sss[Z|+-h[:m]]" */ char * ogc_datetime :: timestamp_str( OGC_TIME timebuf, int sec_digits) const { if ( timebuf != OGC_NULL ) { date_str(timebuf); strcat (timebuf, "T"); time_str(timebuf+11, sec_digits); char * t = timebuf + strlen(timebuf); tz_str(t); } return timebuf; }
int64_t NValue::parseTimestampString(const std::string &str) { // date_str std::string date_str(str); // This is the std:string API for "ltrim" and "rtrim". date_str.erase(date_str.begin(), std::find_if(date_str.begin(), date_str.end(), std::not1(std::ptr_fun<int, int>(std::isspace)))); date_str.erase(std::find_if(date_str.rbegin(), date_str.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), date_str.end()); std::size_t sep_pos; int year = 0; int month = 0; int day = 0; int hour = 0; int minute = 0; int second = 0; int micro = 1000000; // time_str std::string time_str; std::string number_string; const char * pch; switch (date_str.size()) { case 26: sep_pos = date_str.find(' '); if (sep_pos != 10) { throwTimestampFormatError(str); } time_str = date_str.substr(sep_pos + 1); // This is the std:string API for "ltrim" time_str.erase(time_str.begin(), std::find_if(time_str.begin(), time_str.end(), std::not1(std::ptr_fun<int, int>(std::isspace)))); if (time_str.length() != 15) { throwTimestampFormatError(str); } // tokenize time_str: HH:MM:SS.mmmmmm if (time_str.at(2) != ':' || time_str.at(5) != ':' || time_str.at(8) != '.') { throwTimestampFormatError(str); } // HH number_string = time_str.substr(0,2); pch = number_string.c_str(); if (pch[0] == '0') { hour = 0; } else if (pch[0] == '1') { hour = 10; } else if (pch[0] == '2') { hour = 20; } else { throwTimestampFormatError(str); } if (pch[1] > '9' || pch[1] < '0') { throwTimestampFormatError(str); } hour += pch[1] - '0'; if (hour > 23 || hour < 0) { throwTimestampFormatError(str); } // MM number_string = time_str.substr(3,2); pch = number_string.c_str(); if (pch[0] > '5' || pch[0] < '0') { throwTimestampFormatError(str); } minute = 10*(pch[0] - '0'); if (pch[1] > '9' || pch[1] < '0') { throwTimestampFormatError(str); } minute += pch[1] - '0'; if (minute > 59 || minute < 0) { throwTimestampFormatError(str); } // SS number_string = time_str.substr(6,2); pch = number_string.c_str(); if (pch[0] > '5' || pch[0] < '0') { throwTimestampFormatError(str); } second = 10*(pch[0] - '0'); if (pch[1] > '9' || pch[1] < '0') { throwTimestampFormatError(str); } second += pch[1] - '0'; if (second > 59 || second < 0) { throwTimestampFormatError(str); } // hack a '1' in the place if the decimal and use atoi to get a value that // MUST be between 1 and 2 million if all 6 digits of micros were included. number_string = time_str.substr(8,7); number_string.at(0) = '1'; pch = number_string.c_str(); micro = atoi(pch); if (micro >= 2000000 || micro < 1000000) { throwTimestampFormatError(str); } case 10: if (date_str.at(4) != '-' || date_str.at(7) != '-') { throwTimestampFormatError(str); } number_string = date_str.substr(0,4); pch = number_string.c_str(); // YYYY year = atoi(pch); // new years day 10000 is likely to cause problems. // There's a boost library limitation against years before 1400. if (year > 9999 || year < 1400) { throwTimestampFormatError(str); } // MM number_string = date_str.substr(5,2); pch = number_string.c_str(); if (pch[0] == '0') { month = 0; } else if (pch[0] == '1') { month = 10; } else { throwTimestampFormatError(str); } if (pch[1] > '9' || pch[1] < '0') { throwTimestampFormatError(str); } month += pch[1] - '0'; if (month > 12 || month < 1) { throwTimestampFormatError(str); } // DD number_string = date_str.substr(8,2); pch = number_string.c_str(); if (pch[0] == '0') { day = 0; } else if (pch[0] == '1') { day = 10; } else if (pch[0] == '2') { day = 20; } else if (pch[0] == '3') { day = 30; } else { throwTimestampFormatError(str); } if (pch[1] > '9' || pch[1] < '0') { throwTimestampFormatError(str); } day += pch[1] - '0'; if (day > 31 || day < 1) { throwTimestampFormatError(str); } break; default: throwTimestampFormatError(str); } int64_t result = 0; try { result = epoch_microseconds_from_components( (unsigned short int)year, (unsigned short int)month, (unsigned short int)day, hour, minute, second); } catch (const std::out_of_range& bad) { throwTimestampFormatError(str); } result += (micro - 1000000); return result; }