Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::StrpTimeFormat Struct Reference
Inheritance diagram for duckdb::StrpTimeFormat:
Collaboration diagram for duckdb::StrpTimeFormat:

Classes

struct  ParseResult
 Type-safe parsing argument. More...

Public Member Functions

bool operator!= (const StrpTimeFormat &other) const
DUCKDB_API bool Parse (string_t str, ParseResult &result, bool strict=false) const
 Parses a timestamp using the given specifier.
DUCKDB_API bool Parse (const char *data, size_t size, ParseResult &result, bool strict=false) const
DUCKDB_API bool TryParseDate (const char *data, size_t size, date_t &result) const
DUCKDB_API bool TryParseTimestamp (const char *data, size_t size, timestamp_t &result) const
DUCKDB_API bool TryParseTimestampNS (const char *data, size_t size, timestamp_ns_t &result) const
DUCKDB_API bool TryParseDate (string_t str, date_t &result, string &error_message) const
DUCKDB_API bool TryParseTime (string_t str, dtime_t &result, string &error_message) const
DUCKDB_API bool TryParseTimestamp (string_t str, timestamp_t &result, string &error_message) const
DUCKDB_API bool TryParseTimestampNS (string_t str, timestamp_ns_t &result, string &error_message) const
void Serialize (Serializer &serializer) const
Public Member Functions inherited from duckdb::StrTimeFormat
bool HasFormatSpecifier (StrTimeSpecifier s) const
DUCKDB_API bool Empty () const
 If the string format is empty.

Static Public Member Functions

static DUCKDB_API ParseResult Parse (const string &format, const string &text)
static DUCKDB_API bool TryParse (const string &format, const string &text, ParseResult &result)
static StrpTimeFormat Deserialize (Deserializer &deserializer)
Static Public Member Functions inherited from duckdb::StrTimeFormat
static DUCKDB_API string ParseFormatSpecifier (const string &format_string, StrTimeFormat &format)

Protected Member Functions

DUCKDB_API void AddFormatSpecifier (string preceding_literal, StrTimeSpecifier specifier) override
int NumericSpecifierWidth (StrTimeSpecifier specifier)
int32_t TryParseCollection (const char *data, idx_t &pos, idx_t size, const string_t collection[], idx_t collection_count) const
Protected Member Functions inherited from duckdb::StrTimeFormat
void AddLiteral (string literal)

Static Protected Member Functions

static string FormatStrpTimeError (const string &input, optional_idx position)

Private Member Functions

 StrpTimeFormat (const string &format_string)

Additional Inherited Members

Public Attributes inherited from duckdb::StrTimeFormat
string format_specifier
 The full format specifier, for error messages.
Protected Attributes inherited from duckdb::StrTimeFormat
vector< StrTimeSpecifier > specifiers
 The format specifiers.
vector< string > literals
 The literals that appear in between the format specifiers The following must hold: literals.size() = specifiers.size() + 1 Format is literals[0], specifiers[0], literals[1], ..., specifiers[n - 1], literals[n].
idx_t constant_size = 0
 The constant size that appears in the format string.
vector< int > numeric_width
 The max numeric width of the specifier (if it is parsed as a number), or -1 if it is not a number.

Constructor & Destructor Documentation

◆ StrpTimeFormat() [1/2]

duckdb::StrpTimeFormat::StrpTimeFormat ( )
225820 {
225821}

◆ StrpTimeFormat() [2/2]

duckdb::StrpTimeFormat::StrpTimeFormat ( const string & format_string)
explicitprivate
225823 {
225824 if (format_string.empty()) {
225825 return;
225826 }
225827 StrTimeFormat::ParseFormatSpecifier(format_string, *this);
225828}

Member Function Documentation

◆ operator!=()

bool duckdb::StrpTimeFormat::operator!= ( const StrpTimeFormat & other) const
inline
46187 {
46188 return format_specifier != other.format_specifier;
46189 }
string format_specifier
The full format specifier, for error messages.
Definition duckdb.hpp:46098

◆ Parse() [1/3]

StrpTimeFormat::ParseResult duckdb::StrpTimeFormat::Parse ( const string & format,
const string & text )
static
231613 {
231614 StrpTimeFormat format;
231615 format.format_specifier = format_string;
231616 string error = StrTimeFormat::ParseFormatSpecifier(format_string, format);
231617 if (!error.empty()) {
231618 throw InvalidInputException("Failed to parse format specifier %s: %s", format_string, error);
231619 }
231620 StrpTimeFormat::ParseResult result;
231621 if (!format.Parse(text, result)) {
231622 throw InvalidInputException("Failed to parse string \"%s\" with format specifier \"%s\"", text, format_string);
231623 }
231624 return result;
231625}
std::basic_string< Char > format(const S &format_str, Args &&... args)
Definition duckdb.cpp:16295

◆ TryParse()

bool duckdb::StrpTimeFormat::TryParse ( const string & format,
const string & text,
ParseResult & result )
static
231627 {
231628 StrpTimeFormat format;
231629 format.format_specifier = format_string;
231630 string error = StrTimeFormat::ParseFormatSpecifier(format_string, format);
231631 if (!error.empty()) {
231632 throw InvalidInputException("Failed to parse format specifier %s: %s", format_string, error);
231633 }
231634 return format.Parse(text, result);
231635}

◆ Parse() [2/3]

bool duckdb::StrpTimeFormat::Parse ( string_t str,
ParseResult & result,
bool strict = false ) const

Parses a timestamp using the given specifier.

231607 {
231608 auto data = str.GetData();
231609 idx_t size = str.GetSize();
231610 return Parse(data, size, result, strict);
231611}
GOpaque< Size > size(const GMat &src)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ Parse() [3/3]

bool duckdb::StrpTimeFormat::Parse ( const char * data,
size_t size,
ParseResult & result,
bool strict = false ) const
231010 {
231011 auto &result_data = result.data;
231012 auto &error_message = result.error_message;
231013 auto &error_position = result.error_position;
231014
231015 // initialize the result
231016 result_data[0] = 1900;
231017 result_data[1] = 1;
231018 result_data[2] = 1;
231019 result_data[3] = 0;
231020 result_data[4] = 0;
231021 result_data[5] = 0;
231022 result_data[6] = 0;
231023 result_data[7] = 0;
231024 // skip leading spaces
231025 while (StringUtil::CharacterIsSpace(*data)) {
231026 data++;
231027 size--;
231028 }
231029 // Check for specials
231030 // Precheck for alphas for performance.
231031 idx_t pos = 0;
231032 result.is_special = false;
231033 if (size > 4) {
231034 if (StringUtil::CharacterIsAlpha(*data)) {
231035 if (Date::TryConvertDateSpecial(data, size, pos, Date::PINF)) {
231036 result.is_special = true;
231037 result.special = date_t::infinity();
231038 } else if (Date::TryConvertDateSpecial(data, size, pos, Date::EPOCH)) {
231039 result.is_special = true;
231040 result.special = date_t::epoch();
231041 }
231042 } else if (*data == '-' && Date::TryConvertDateSpecial(data, size, pos, Date::NINF)) {
231043 result.is_special = true;
231044 result.special = date_t::ninfinity();
231045 }
231046 }
231047 if (result.is_special) {
231048 // skip trailing spaces
231049 while (pos < size && StringUtil::CharacterIsSpace(data[pos])) {
231050 pos++;
231051 }
231052 if (pos != size) {
231053 error_message = "Special timestamp did not match: trailing characters";
231054 error_position = pos;
231055 return false;
231056 }
231057 return true;
231058 }
231059
231060 TimeSpecifierAMOrPM ampm = TimeSpecifierAMOrPM::TIME_SPECIFIER_NONE;
231061
231062 // Year offset state (Year+W/j)
231063 auto offset_specifier = StrTimeSpecifier::WEEKDAY_DECIMAL;
231064 uint64_t weekno = 0;
231065 uint64_t weekday = 0;
231066 uint64_t yearday = 0;
231067 bool has_weekday = false;
231068
231069 // ISO state (%G/%V/%u)
231070 // Out of range values to detect multiple specifications
231071 uint64_t iso_year = 10000;
231072 uint64_t iso_week = 54;
231073 uint64_t iso_weekday = 8;
231074
231075 for (idx_t i = 0;; i++) {
231076 D_ASSERT(i < literals.size());
231077 // first compare the literal
231078 const auto &literal = literals[i];
231079 for (size_t l = 0; l < literal.size();) {
231080 // Match runs of spaces to runs of spaces.
231081 if (StringUtil::CharacterIsSpace(literal[l])) {
231082 if (!StringUtil::CharacterIsSpace(data[pos])) {
231083 error_message = "Space does not match, expected " + literals[i];
231084 error_position = pos;
231085 return false;
231086 }
231087 for (++pos; pos < size && StringUtil::CharacterIsSpace(data[pos]); ++pos) {
231088 continue;
231089 }
231090 for (++l; l < literal.size() && StringUtil::CharacterIsSpace(literal[l]); ++l) {
231091 continue;
231092 }
231093 continue;
231094 }
231095 // literal does not match
231096 if (data[pos++] != literal[l++]) {
231097 error_message = "Literal does not match, expected " + literal;
231098 error_position = pos;
231099 return false;
231100 }
231101 }
231102 if (i == specifiers.size()) {
231103 break;
231104 }
231105 // now parse the specifier
231106 if (numeric_width[i] > 0) {
231107 // numeric specifier: parse a number
231108 uint64_t number = 0;
231109 int digits = 0;
231110 size_t start_pos = pos;
231111 size_t end_pos = start_pos + UnsafeNumericCast<size_t>(numeric_width[i]);
231112 while (pos < size && pos < end_pos && StringUtil::CharacterIsDigit(data[pos])) {
231113 number = number * 10 + UnsafeNumericCast<uint64_t>(data[pos]) - '0';
231114 pos++;
231115 ++digits;
231116 }
231117 if (pos == start_pos) {
231118 // expected a number here
231119 error_message = "Expected a number";
231120 error_position = start_pos;
231121 return false;
231122 }
231123 switch (specifiers[i]) {
231124 case StrTimeSpecifier::DAY_OF_MONTH_PADDED:
231125 case StrTimeSpecifier::DAY_OF_MONTH:
231126 if (number < 1 || number > 31) {
231127 error_message = "Day out of range, expected a value between 1 and 31";
231128 error_position = start_pos;
231129 return false;
231130 }
231131 // day of the month
231132 result_data[2] = UnsafeNumericCast<int32_t>(number);
231133 offset_specifier = specifiers[i];
231134 break;
231135 case StrTimeSpecifier::MONTH_DECIMAL_PADDED:
231136 case StrTimeSpecifier::MONTH_DECIMAL:
231137 if (number < 1 || number > 12) {
231138 error_message = "Month out of range, expected a value between 1 and 12";
231139 error_position = start_pos;
231140 return false;
231141 }
231142 // month number
231143 result_data[1] = UnsafeNumericCast<int32_t>(number);
231144 offset_specifier = specifiers[i];
231145 break;
231146 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY_PADDED:
231147 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY:
231148 switch (offset_specifier) {
231149 case StrTimeSpecifier::YEAR_ISO:
231150 case StrTimeSpecifier::WEEK_NUMBER_ISO:
231151 // Override
231152 case StrTimeSpecifier::WEEKDAY_DECIMAL:
231153 // First offset specifier
231154 offset_specifier = specifiers[i];
231155 break;
231156 default:
231157 break;
231158 }
231159 // year without century..
231160 // Python uses 69 as a crossover point (i.e. >= 69 is 19.., < 69 is 20..)
231161 if (pos - start_pos < 2 && strict) {
231162 return false;
231163 }
231164 if (number >= 100) {
231165 // %y only supports numbers between [0..99]
231166 error_message = "Year without century out of range, expected a value between 0 and 99";
231167 error_position = start_pos;
231168 return false;
231169 }
231170 if (number >= 69) {
231171 result_data[0] = int32_t(1900 + number);
231172 } else {
231173 result_data[0] = int32_t(2000 + number);
231174 }
231175 break;
231176 case StrTimeSpecifier::YEAR_DECIMAL:
231177 switch (offset_specifier) {
231178 case StrTimeSpecifier::YEAR_ISO:
231179 case StrTimeSpecifier::WEEK_NUMBER_ISO:
231180 // Override
231181 case StrTimeSpecifier::WEEKDAY_DECIMAL:
231182 // First offset specifier
231183 offset_specifier = specifiers[i];
231184 break;
231185 default:
231186 break;
231187 }
231188 if (pos - start_pos < 2 && strict) {
231189 return false;
231190 }
231191 // year as full number
231192 result_data[0] = UnsafeNumericCast<int32_t>(number);
231193 break;
231194 case StrTimeSpecifier::YEAR_ISO:
231195 switch (offset_specifier) {
231196 // y/m/d overrides G/V/u but does not conflict
231197 case StrTimeSpecifier::DAY_OF_MONTH_PADDED:
231198 case StrTimeSpecifier::DAY_OF_MONTH:
231199 case StrTimeSpecifier::MONTH_DECIMAL_PADDED:
231200 case StrTimeSpecifier::MONTH_DECIMAL:
231201 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY_PADDED:
231202 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY:
231203 case StrTimeSpecifier::YEAR_DECIMAL:
231204 // Just validate, don't use
231205 break;
231206 case StrTimeSpecifier::WEEKDAY_DECIMAL:
231207 // First offset specifier
231208 offset_specifier = specifiers[i];
231209 break;
231210 case StrTimeSpecifier::YEAR_ISO:
231211 case StrTimeSpecifier::WEEK_NUMBER_ISO:
231212 // Already parsing ISO
231213 if (iso_year <= 9999) {
231214 error_message = "Multiple ISO year offsets specified";
231215 error_position = start_pos;
231216 return false;
231217 }
231218 break;
231219 default:
231220 error_message = "Incompatible ISO year offset specified";
231221 error_position = start_pos;
231222 return false;
231223 }
231224 if (number > 9999) {
231225 // %G only supports numbers between [0..9999]
231226 error_message = "ISO Year out of range, expected a value between 0000 and 9999";
231227 error_position = start_pos;
231228 return false;
231229 }
231230 iso_year = number;
231231 break;
231232 case StrTimeSpecifier::HOUR_24_PADDED:
231233 case StrTimeSpecifier::HOUR_24_DECIMAL:
231234 if (number >= 24) {
231235 error_message = "Hour out of range, expected a value between 0 and 23";
231236 error_position = start_pos;
231237 return false;
231238 }
231239 // hour as full number
231240 result_data[3] = UnsafeNumericCast<int32_t>(number);
231241 break;
231242 case StrTimeSpecifier::HOUR_12_PADDED:
231243 case StrTimeSpecifier::HOUR_12_DECIMAL:
231244 if (number < 1 || number > 12) {
231245 error_message = "Hour12 out of range, expected a value between 1 and 12";
231246 error_position = start_pos;
231247 return false;
231248 }
231249 // 12-hour number: start off by just storing the number
231250 result_data[3] = UnsafeNumericCast<int32_t>(number);
231251 break;
231252 case StrTimeSpecifier::MINUTE_PADDED:
231253 case StrTimeSpecifier::MINUTE_DECIMAL:
231254 if (number >= 60) {
231255 error_message = "Minutes out of range, expected a value between 0 and 59";
231256 error_position = start_pos;
231257 return false;
231258 }
231259 // minutes
231260 result_data[4] = UnsafeNumericCast<int32_t>(number);
231261 break;
231262 case StrTimeSpecifier::SECOND_PADDED:
231263 case StrTimeSpecifier::SECOND_DECIMAL:
231264 if (number >= 60) {
231265 error_message = "Seconds out of range, expected a value between 0 and 59";
231266 error_position = start_pos;
231267 return false;
231268 }
231269 // seconds
231270 result_data[5] = UnsafeNumericCast<int32_t>(number);
231271 break;
231272 case StrTimeSpecifier::NANOSECOND_PADDED:
231273 for (; digits < numeric_width[i]; ++digits) {
231274 number *= 10;
231275 }
231276 D_ASSERT(number < Interval::NANOS_PER_SEC); // enforced by the length of the number
231277 // nanoseconds
231278 result_data[6] = UnsafeNumericCast<int32_t>(number);
231279 break;
231280 case StrTimeSpecifier::MICROSECOND_PADDED:
231281 for (; digits < numeric_width[i]; ++digits) {
231282 number *= 10;
231283 }
231284 D_ASSERT(number < Interval::MICROS_PER_SEC); // enforced by the length of the number
231285 // nanoseconds
231286 result_data[6] = UnsafeNumericCast<int32_t>(number * Interval::NANOS_PER_MICRO);
231287 break;
231288 case StrTimeSpecifier::MILLISECOND_PADDED:
231289 for (; digits < numeric_width[i]; ++digits) {
231290 number *= 10;
231291 }
231292 D_ASSERT(number < Interval::MSECS_PER_SEC); // enforced by the length of the number
231293 // nanoseconds
231294 result_data[6] = UnsafeNumericCast<int32_t>(number * Interval::NANOS_PER_MSEC);
231295 break;
231296 case StrTimeSpecifier::WEEK_NUMBER_PADDED_SUN_FIRST:
231297 case StrTimeSpecifier::WEEK_NUMBER_PADDED_MON_FIRST:
231298 // m/d overrides WU/w but does not conflict
231299 switch (offset_specifier) {
231300 case StrTimeSpecifier::DAY_OF_MONTH_PADDED:
231301 case StrTimeSpecifier::DAY_OF_MONTH:
231302 case StrTimeSpecifier::MONTH_DECIMAL_PADDED:
231303 case StrTimeSpecifier::MONTH_DECIMAL:
231304 // Just validate, don't use
231305 break;
231306 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY_PADDED:
231307 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY:
231308 case StrTimeSpecifier::YEAR_DECIMAL:
231309 // Switch to offset parsing
231310 case StrTimeSpecifier::WEEKDAY_DECIMAL:
231311 // First offset specifier
231312 offset_specifier = specifiers[i];
231313 break;
231314 default:
231315 error_message = "Multiple week offsets specified";
231316 error_position = start_pos;
231317 return false;
231318 }
231319 if (number > 53) {
231320 error_message = "Week out of range, expected a value between 0 and 53";
231321 error_position = start_pos;
231322 return false;
231323 }
231324 weekno = number;
231325 break;
231326 case StrTimeSpecifier::WEEKDAY_DECIMAL:
231327 if (number > 6) {
231328 error_message = "Weekday out of range, expected a value between 0 and 6";
231329 error_position = start_pos;
231330 return false;
231331 }
231332 has_weekday = true;
231333 weekday = number;
231334 break;
231335 case StrTimeSpecifier::WEEK_NUMBER_ISO:
231336 switch (offset_specifier) {
231337 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY_PADDED:
231338 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY:
231339 case StrTimeSpecifier::YEAR_DECIMAL:
231340 error_message = "ISO week offsets are incompatible with non-ISO year specifiers. Use '%G' instead";
231341 error_position = start_pos;
231342 return false;
231343 case StrTimeSpecifier::DAY_OF_MONTH_PADDED:
231344 case StrTimeSpecifier::DAY_OF_MONTH:
231345 case StrTimeSpecifier::MONTH_DECIMAL_PADDED:
231346 case StrTimeSpecifier::MONTH_DECIMAL:
231347 // Just validate, don't use
231348 break;
231349 case StrTimeSpecifier::WEEKDAY_DECIMAL:
231350 // First offset specifier
231351 offset_specifier = specifiers[i];
231352 break;
231353 case StrTimeSpecifier::WEEK_NUMBER_ISO:
231354 case StrTimeSpecifier::YEAR_ISO:
231355 // Already parsing ISO
231356 if (iso_week <= 53) {
231357 error_message = "Multiple ISO week offsets specified";
231358 error_position = start_pos;
231359 return false;
231360 }
231361 break;
231362 default:
231363 error_message = "Incompatible ISO week offset specified";
231364 error_position = start_pos;
231365 return false;
231366 }
231367 if (number < 1 || number > 53) {
231368 error_message = "ISO week offset out of range, expected a value between 1 and 53";
231369 error_position = start_pos;
231370 return false;
231371 }
231372 iso_week = number;
231373 break;
231374 case StrTimeSpecifier::WEEKDAY_ISO:
231375 if (iso_weekday <= 7) {
231376 error_message = "Multiple ISO weekday offsets specified";
231377 error_position = start_pos;
231378 return false;
231379 }
231380 if (number < 1 || number > 7) {
231381 error_message = "ISO weekday offset out of range, expected a value between 1 and 7";
231382 error_position = start_pos;
231383 return false;
231384 }
231385 iso_weekday = number;
231386 break;
231387 case StrTimeSpecifier::DAY_OF_YEAR_PADDED:
231388 case StrTimeSpecifier::DAY_OF_YEAR_DECIMAL:
231389 // m/d overrides j but does not conflict
231390 switch (offset_specifier) {
231391 case StrTimeSpecifier::DAY_OF_MONTH_PADDED:
231392 case StrTimeSpecifier::DAY_OF_MONTH:
231393 case StrTimeSpecifier::MONTH_DECIMAL_PADDED:
231394 case StrTimeSpecifier::MONTH_DECIMAL:
231395 // Just validate, don't use
231396 break;
231397 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY_PADDED:
231398 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY:
231399 case StrTimeSpecifier::YEAR_DECIMAL:
231400 // Switch to offset parsing
231401 case StrTimeSpecifier::WEEKDAY_DECIMAL:
231402 // First offset specifier
231403 offset_specifier = specifiers[i];
231404 break;
231405 default:
231406 error_message = "Multiple year offsets specified";
231407 error_position = start_pos;
231408 return false;
231409 }
231410 if (number < 1 || number > 366) {
231411 error_message = "Year day out of range, expected a value between 1 and 366";
231412 error_position = start_pos;
231413 return false;
231414 }
231415 yearday = number;
231416 break;
231417 default:
231418 throw NotImplementedException("Unsupported specifier for strptime");
231419 }
231420 } else {
231421 switch (specifiers[i]) {
231422 case StrTimeSpecifier::AM_PM: {
231423 // parse the next 2 characters
231424 if (pos + 2 > size) {
231425 // no characters left to parse
231426 error_message = "Expected AM/PM";
231427 error_position = pos;
231428 return false;
231429 }
231430 char pa_char = char(std::tolower(data[pos]));
231431 char m_char = char(std::tolower(data[pos + 1]));
231432 if (m_char != 'm') {
231433 error_message = "Expected AM/PM";
231434 error_position = pos;
231435 return false;
231436 }
231437 if (pa_char == 'p') {
231438 ampm = TimeSpecifierAMOrPM::TIME_SPECIFIER_PM;
231439 } else if (pa_char == 'a') {
231440 ampm = TimeSpecifierAMOrPM::TIME_SPECIFIER_AM;
231441 } else {
231442 error_message = "Expected AM/PM";
231443 error_position = pos;
231444 return false;
231445 }
231446 pos += 2;
231447 break;
231448 }
231449 // we parse weekday names, but we don't use them as information
231450 case StrTimeSpecifier::ABBREVIATED_WEEKDAY_NAME:
231451 if (TryParseCollection(data, pos, size, Date::DAY_NAMES_ABBREVIATED, 7) < 0) {
231452 error_message = "Expected an abbreviated day name (Mon, Tue, Wed, Thu, Fri, Sat, Sun)";
231453 error_position = pos;
231454 return false;
231455 }
231456 break;
231457 case StrTimeSpecifier::FULL_WEEKDAY_NAME:
231458 if (TryParseCollection(data, pos, size, Date::DAY_NAMES, 7) < 0) {
231459 error_message = "Expected a full day name (Monday, Tuesday, etc...)";
231460 error_position = pos;
231461 return false;
231462 }
231463 break;
231464 case StrTimeSpecifier::ABBREVIATED_MONTH_NAME: {
231465 int32_t month = TryParseCollection(data, pos, size, Date::MONTH_NAMES_ABBREVIATED, 12);
231466 if (month < 0) {
231467 error_message = "Expected an abbreviated month name (Jan, Feb, Mar, etc..)";
231468 error_position = pos;
231469 return false;
231470 }
231471 result_data[1] = month + 1;
231472 break;
231473 }
231474 case StrTimeSpecifier::FULL_MONTH_NAME: {
231475 int32_t month = TryParseCollection(data, pos, size, Date::MONTH_NAMES, 12);
231476 if (month < 0) {
231477 error_message = "Expected a full month name (January, February, etc...)";
231478 error_position = pos;
231479 return false;
231480 }
231481 result_data[1] = month + 1;
231482 break;
231483 }
231484 case StrTimeSpecifier::UTC_OFFSET: {
231485 int hh, mm, ss;
231486 if (!Timestamp::TryParseUTCOffset(data, pos, size, hh, mm, ss)) {
231487 error_message = "Expected ±HH[MM] or -HH[:MM[:SS]]";
231488 error_position = pos;
231489 return false;
231490 }
231491 result_data[7] = (hh * Interval::MINS_PER_HOUR + mm) * Interval::SECS_PER_MINUTE + ss;
231492 break;
231493 }
231494 case StrTimeSpecifier::TZ_NAME: {
231495 // skip leading spaces
231496 while (pos < size && StringUtil::CharacterIsSpace(data[pos])) {
231497 pos++;
231498 }
231499 const auto tz_begin = data + pos;
231500 // stop when we encounter a non-tz character
231501 while (pos < size && Timestamp::CharacterIsTimeZone(data[pos])) {
231502 pos++;
231503 }
231504 const auto tz_end = data + pos;
231505 // Can't fully validate without a list - caller's responsibility.
231506 // But tz must not be empty.
231507 if (tz_end == tz_begin) {
231508 error_message = "Empty Time Zone name";
231509 error_position = UnsafeNumericCast<idx_t>(tz_begin - data);
231510 return false;
231511 }
231512 result.tz.assign(tz_begin, tz_end);
231513 break;
231514 }
231515 default:
231516 throw NotImplementedException("Unsupported specifier for strptime");
231517 }
231518 }
231519 }
231520 // skip trailing spaces
231521 while (pos < size && StringUtil::CharacterIsSpace(data[pos])) {
231522 pos++;
231523 }
231524 if (pos != size) {
231525 error_message = "Full specifier did not match: trailing characters";
231526 error_position = pos;
231527 return false;
231528 }
231529 if (ampm != TimeSpecifierAMOrPM::TIME_SPECIFIER_NONE) {
231530 if (result_data[3] > 12) {
231531 error_message =
231532 "Invalid hour: " + to_string(result_data[3]) + " AM/PM, expected an hour within the range [0..12]";
231533 return false;
231534 }
231535 // adjust the hours based on the AM or PM specifier
231536 if (ampm == TimeSpecifierAMOrPM::TIME_SPECIFIER_AM) {
231537 // AM: 12AM=0, 1AM=1, 2AM=2, ..., 11AM=11
231538 if (result_data[3] == 12) {
231539 result_data[3] = 0;
231540 }
231541 } else {
231542 // PM: 12PM=12, 1PM=13, 2PM=14, ..., 11PM=23
231543 if (result_data[3] != 12) {
231544 result_data[3] += 12;
231545 }
231546 }
231547 }
231548 switch (offset_specifier) {
231549 case StrTimeSpecifier::YEAR_ISO:
231550 case StrTimeSpecifier::WEEK_NUMBER_ISO: {
231551 // Default to 1900-01-01
231552 iso_year = (iso_year > 9999) ? 1900 : iso_year;
231553 iso_week = (iso_week > 53) ? 1 : iso_week;
231554 iso_weekday = (iso_weekday > 7) ? 1 : iso_weekday;
231555 // Gregorian and ISO agree on the year of January 4
231556 auto jan4 = Date::FromDate(UnsafeNumericCast<int32_t>(iso_year), 1, 4);
231557 // ISO Week 1 starts on the previous Monday
231558 auto week1 = Date::GetMondayOfCurrentWeek(jan4);
231559 // ISO Week N starts N-1 weeks later
231560 auto iso_date = week1 + UnsafeNumericCast<int32_t>((iso_week - 1) * 7 + (iso_weekday - 1));
231561 Date::Convert(iso_date, result_data[0], result_data[1], result_data[2]);
231562 break;
231563 }
231564 case StrTimeSpecifier::WEEK_NUMBER_PADDED_SUN_FIRST:
231565 case StrTimeSpecifier::WEEK_NUMBER_PADDED_MON_FIRST: {
231566 // Adjust weekday to be 0-based for the week type
231567 if (has_weekday) {
231568 weekday = (weekday + 7 -
231569 static_cast<uint64_t>(offset_specifier == StrTimeSpecifier::WEEK_NUMBER_PADDED_MON_FIRST)) %
231570 7;
231571 }
231572 // Get the start of week 1, move back 7 days and then weekno * 7 + weekday gives the date
231573 const auto jan1 = Date::FromDate(result_data[0], 1, 1);
231574 auto yeardate = Date::GetMondayOfCurrentWeek(jan1);
231575 yeardate -= int(offset_specifier == StrTimeSpecifier::WEEK_NUMBER_PADDED_SUN_FIRST);
231576 // Is there a week 0?
231577 yeardate -= 7 * int(yeardate >= jan1);
231578 yeardate += UnsafeNumericCast<int32_t>(weekno * 7 + weekday);
231579 Date::Convert(yeardate, result_data[0], result_data[1], result_data[2]);
231580 break;
231581 }
231582 case StrTimeSpecifier::DAY_OF_YEAR_PADDED:
231583 case StrTimeSpecifier::DAY_OF_YEAR_DECIMAL: {
231584 auto yeardate = Date::FromDate(result_data[0], 1, 1);
231585 yeardate += UnsafeNumericCast<int32_t>(yearday - 1);
231586 Date::Convert(yeardate, result_data[0], result_data[1], result_data[2]);
231587 break;
231588 }
231589 case StrTimeSpecifier::DAY_OF_MONTH_PADDED:
231590 case StrTimeSpecifier::DAY_OF_MONTH:
231591 case StrTimeSpecifier::MONTH_DECIMAL_PADDED:
231592 case StrTimeSpecifier::MONTH_DECIMAL:
231593 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY_PADDED:
231594 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY:
231595 case StrTimeSpecifier::YEAR_DECIMAL:
231596 // m/d overrides UWVwu/j
231597 break;
231598 default:
231599 D_ASSERT(offset_specifier == StrTimeSpecifier::WEEKDAY_DECIMAL);
231600 break;
231601 }
231602
231603 return true;
231604}
static DUCKDB_API date_t FromDate(int32_t year, int32_t month, int32_t day)
Create a Date object from a specified (year, month, day) combination.
Definition duckdb.cpp:117328
static DUCKDB_API date_t GetMondayOfCurrentWeek(date_t date)
Returns the date of the monday of the current week.
Definition duckdb.cpp:117775
static DUCKDB_API void Convert(date_t date, int32_t &out_year, int32_t &out_month, int32_t &out_day)
Extract the year, month and day from a given date object.
Definition duckdb.cpp:117272
static DUCKDB_API bool TryConvertDateSpecial(const char *buf, idx_t len, idx_t &pos, const DateSpecial &special)
Definition duckdb.cpp:117347
static bool CharacterIsTimeZone(char c)
Is the character a valid part of a time zone name?
Definition duckdb.hpp:4281
std::string to_string(const T &value)
Definition duckdb.cpp:19425
::uint64_t uint64_t
::int32_t int32_t
vector< StrTimeSpecifier > specifiers
The format specifiers.
Definition duckdb.hpp:46102
vector< string > literals
The literals that appear in between the format specifiers The following must hold: literals....
Definition duckdb.hpp:46106
vector< int > numeric_width
The max numeric width of the specifier (if it is parsed as a number), or -1 if it is not a number.
Definition duckdb.hpp:46110

◆ TryParseDate() [1/2]

bool duckdb::StrpTimeFormat::TryParseDate ( const char * data,
size_t size,
date_t & result ) const
231765 {
231766 ParseResult parse_result;
231767 if (!Parse(data, size, parse_result)) {
231768 return false;
231769 }
231770 return parse_result.TryToDate(result);
231771}
Type-safe parsing argument.
Definition duckdb.hpp:46161

◆ TryParseTimestamp() [1/2]

bool duckdb::StrpTimeFormat::TryParseTimestamp ( const char * data,
size_t size,
timestamp_t & result ) const
231791 {
231792 ParseResult parse_result;
231793 if (!Parse(data, size, parse_result)) {
231794 return false;
231795 }
231796 return parse_result.TryToTimestamp(result);
231797}

◆ TryParseTimestampNS() [1/2]

bool duckdb::StrpTimeFormat::TryParseTimestampNS ( const char * data,
size_t size,
timestamp_ns_t & result ) const
231808 {
231809 ParseResult parse_result;
231810 if (!Parse(data, size, parse_result)) {
231811 return false;
231812 }
231813 return parse_result.TryToTimestampNS(result);
231814}

◆ TryParseDate() [2/2]

bool duckdb::StrpTimeFormat::TryParseDate ( string_t str,
date_t & result,
string & error_message ) const
231756 {
231757 ParseResult parse_result;
231758 if (!Parse(input, parse_result)) {
231759 error_message = parse_result.FormatError(input, format_specifier);
231760 return false;
231761 }
231762 return parse_result.TryToDate(result);
231763}

◆ TryParseTime()

bool duckdb::StrpTimeFormat::TryParseTime ( string_t str,
dtime_t & result,
string & error_message ) const
231773 {
231774 ParseResult parse_result;
231775 if (!Parse(input, parse_result)) {
231776 error_message = parse_result.FormatError(input, format_specifier);
231777 return false;
231778 }
231779 return parse_result.TryToTime(result);
231780}

◆ TryParseTimestamp() [2/2]

bool duckdb::StrpTimeFormat::TryParseTimestamp ( string_t str,
timestamp_t & result,
string & error_message ) const
231782 {
231783 ParseResult parse_result;
231784 if (!Parse(input, parse_result)) {
231785 error_message = parse_result.FormatError(input, format_specifier);
231786 return false;
231787 }
231788 return parse_result.TryToTimestamp(result);
231789}

◆ TryParseTimestampNS() [2/2]

bool duckdb::StrpTimeFormat::TryParseTimestampNS ( string_t str,
timestamp_ns_t & result,
string & error_message ) const
231799 {
231800 ParseResult parse_result;
231801 if (!Parse(input, parse_result)) {
231802 error_message = parse_result.FormatError(input, format_specifier);
231803 return false;
231804 }
231805 return parse_result.TryToTimestampNS(result);
231806}

◆ Serialize()

void duckdb::StrpTimeFormat::Serialize ( Serializer & serializer) const
417874 {
417875 serializer.WritePropertyWithDefault<string>(100, "format_specifier", format_specifier);
417876}

◆ Deserialize()

StrpTimeFormat duckdb::StrpTimeFormat::Deserialize ( Deserializer & deserializer)
static
417878 {
417879 auto format_specifier = deserializer.ReadPropertyWithDefault<string>(100, "format_specifier");
417880 StrpTimeFormat result(format_specifier);
417881 return result;
417882}

◆ FormatStrpTimeError()

string duckdb::StrpTimeFormat::FormatStrpTimeError ( const string & input,
optional_idx position )
staticprotected
231641 {
231642 if (!position.IsValid()) {
231643 return string();
231644 }
231645 return input + "\n" + string(position.GetIndex(), ' ') + "^";
231646}

◆ AddFormatSpecifier()

void duckdb::StrpTimeFormat::AddFormatSpecifier ( string preceding_literal,
StrTimeSpecifier specifier )
overrideprotectedvirtual

Reimplemented from duckdb::StrTimeFormat.

230937 {
230938 numeric_width.push_back(NumericSpecifierWidth(specifier));
230939 StrTimeFormat::AddFormatSpecifier(std::move(preceding_literal), specifier);
230940}

◆ NumericSpecifierWidth()

int duckdb::StrpTimeFormat::NumericSpecifierWidth ( StrTimeSpecifier specifier)
protected
230942 {
230943 switch (specifier) {
230944 case StrTimeSpecifier::WEEKDAY_DECIMAL:
230945 case StrTimeSpecifier::WEEKDAY_ISO:
230946 return 1;
230947 case StrTimeSpecifier::DAY_OF_MONTH_PADDED:
230948 case StrTimeSpecifier::DAY_OF_MONTH:
230949 case StrTimeSpecifier::MONTH_DECIMAL_PADDED:
230950 case StrTimeSpecifier::MONTH_DECIMAL:
230951 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY_PADDED:
230952 case StrTimeSpecifier::YEAR_WITHOUT_CENTURY:
230953 case StrTimeSpecifier::HOUR_24_PADDED:
230954 case StrTimeSpecifier::HOUR_24_DECIMAL:
230955 case StrTimeSpecifier::HOUR_12_PADDED:
230956 case StrTimeSpecifier::HOUR_12_DECIMAL:
230957 case StrTimeSpecifier::MINUTE_PADDED:
230958 case StrTimeSpecifier::MINUTE_DECIMAL:
230959 case StrTimeSpecifier::SECOND_PADDED:
230960 case StrTimeSpecifier::SECOND_DECIMAL:
230961 case StrTimeSpecifier::WEEK_NUMBER_PADDED_SUN_FIRST:
230962 case StrTimeSpecifier::WEEK_NUMBER_PADDED_MON_FIRST:
230963 case StrTimeSpecifier::WEEK_NUMBER_ISO:
230964 return 2;
230965 case StrTimeSpecifier::MILLISECOND_PADDED:
230966 case StrTimeSpecifier::DAY_OF_YEAR_PADDED:
230967 case StrTimeSpecifier::DAY_OF_YEAR_DECIMAL:
230968 return 3;
230969 case StrTimeSpecifier::YEAR_DECIMAL:
230970 case StrTimeSpecifier::YEAR_ISO:
230971 return 4;
230972 case StrTimeSpecifier::MICROSECOND_PADDED:
230973 return 6;
230974 case StrTimeSpecifier::NANOSECOND_PADDED:
230975 return 9;
230976 default:
230977 return -1;
230978 }
230979}

◆ TryParseCollection()

int32_t duckdb::StrpTimeFormat::TryParseCollection ( const char * data,
idx_t & pos,
idx_t size,
const string_t collection[],
idx_t collection_count ) const
protected
230984 {
230985 for (idx_t c = 0; c < collection_count; c++) {
230986 auto &entry = collection[c];
230987 auto entry_data = entry.GetData();
230988 auto entry_size = entry.GetSize();
230989 // check if this entry matches
230990 if (pos + entry_size > size) {
230991 // too big: can't match
230992 continue;
230993 }
230994 // compare the characters
230995 idx_t i;
230996 for (i = 0; i < entry_size; i++) {
230997 if (std::tolower(entry_data[i]) != std::tolower(data[pos + i])) {
230998 break;
230999 }
231000 }
231001 if (i == entry_size) {
231002 // full match
231003 pos += entry_size;
231004 return UnsafeNumericCast<int32_t>(c);
231005 }
231006 }
231007 return -1;
231008}

The documentation for this struct was generated from the following files: