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::Date Class Reference

The Date class is a static class that holds helper functions for the Date type. More...

#include <duckdb.hpp>

Collaboration diagram for duckdb::Date:

Static Public Member Functions

static DUCKDB_API date_t FromString (const string &str, bool strict=false)
 Convert a string in the format "YYYY-MM-DD" to a date object.
static DUCKDB_API date_t FromCString (const char *str, idx_t len, bool strict=false)
 Convert a string in the format "YYYY-MM-DD" to a date object.
static DUCKDB_API string ToString (date_t date)
 Convert a date object to a string in the format "YYYY-MM-DD".
static DUCKDB_API bool TryConvertDateSpecial (const char *buf, idx_t len, idx_t &pos, const DateSpecial &special)
static DUCKDB_API DateCastResult TryConvertDate (const char *buf, idx_t len, idx_t &pos, date_t &result, bool &special, bool strict=false)
static DUCKDB_API string Format (int32_t year, int32_t month, int32_t day)
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.
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.
static DUCKDB_API bool TryFromDate (int32_t year, int32_t month, int32_t day, date_t &result)
static DUCKDB_API bool IsLeapYear (int32_t year)
 Returns true if (year) is a leap year, and false otherwise.
static DUCKDB_API bool IsValid (int32_t year, int32_t month, int32_t day)
static bool IsFinite (date_t date)
 Returns true if the specified date is finite.
static DUCKDB_API int32_t MonthDays (int32_t year, int32_t month)
 The max number of days in a month of a given year.
static DUCKDB_API int64_t Epoch (date_t date)
 Extract the epoch from the date (seconds since 1970-01-01).
static DUCKDB_API int64_t EpochNanoseconds (date_t date)
 Extract the epoch from the date (nanoseconds since 1970-01-01).
static DUCKDB_API int64_t EpochMicroseconds (date_t date)
 Extract the epoch from the date (microseconds since 1970-01-01).
static DUCKDB_API int64_t EpochMilliseconds (date_t date)
 Extract the epoch from the date (milliseconds since 1970-01-01).
static DUCKDB_API date_t EpochToDate (int64_t epoch)
 Convert the epoch (seconds since 1970-01-01) to a date_t.
static DUCKDB_API int32_t EpochDays (date_t date)
 Extract the number of days since epoch (days since 1970-01-01).
static DUCKDB_API date_t EpochDaysToDate (int32_t epoch)
 Convert the epoch number of days to a date_t.
static DUCKDB_API int32_t ExtractYear (date_t date)
 Extract year of a date entry.
static DUCKDB_API int32_t ExtractMonth (date_t date)
 Extract month of a date entry.
static DUCKDB_API int32_t ExtractDay (date_t date)
 Extract day of a date entry.
static DUCKDB_API int32_t ExtractISODayOfTheWeek (date_t date)
 Extract the day of the week (1-7).
static DUCKDB_API int32_t ExtractDayOfTheYear (date_t date)
 Extract the day of the year.
static DUCKDB_API int64_t ExtractJulianDay (date_t date)
 Extract the day of the year.
static DUCKDB_API void ExtractISOYearWeek (date_t date, int32_t &year, int32_t &week)
static DUCKDB_API int32_t ExtractISOWeekNumber (date_t date)
static DUCKDB_API int32_t ExtractISOYearNumber (date_t date)
static DUCKDB_API int32_t ExtractWeekNumberRegular (date_t date, bool monday_first=true)
static DUCKDB_API date_t GetMondayOfCurrentWeek (date_t date)
 Returns the date of the monday of the current week.
static DUCKDB_API bool ParseDoubleDigit (const char *buf, idx_t len, idx_t &pos, int32_t &result)
 Helper function to parse two digits from a string (e.g. "30" -> 30, "03" -> 3, "3" -> 3).
static DUCKDB_API string FormatError (const string &str)
static DUCKDB_API string FormatError (string_t str)
static DUCKDB_API string RangeError (const string &str)
static DUCKDB_API string RangeError (string_t str)

Static Public Attributes

static const DateSpecial PINF = {"infinity", 3}
static const DateSpecial NINF = {"-infinity", 4}
static const DateSpecial EPOCH = {"epoch", 5}
static const string_t MONTH_NAMES [12]
static const string_t MONTH_NAMES_ABBREVIATED [12]
static const string_t DAY_NAMES [7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
static const string_t DAY_NAMES_ABBREVIATED [7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
static const int32_t NORMAL_DAYS [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
static const int32_t CUMULATIVE_DAYS [13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}
static const int32_t LEAP_DAYS [13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
static const int32_t CUMULATIVE_LEAP_DAYS [13] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
static const int32_t CUMULATIVE_YEAR_DAYS [401]
static const int8_t MONTH_PER_DAY_OF_YEAR [365]
static const int8_t LEAP_MONTH_PER_DAY_OF_YEAR [366]
static constexpr const int32_t DATE_MIN_YEAR = -5877641
static constexpr const int32_t DATE_MIN_MONTH = 6
static constexpr const int32_t DATE_MIN_DAY = 25
static constexpr const int32_t DATE_MAX_YEAR = 5881580
static constexpr const int32_t DATE_MAX_MONTH = 7
static constexpr const int32_t DATE_MAX_DAY = 10
static constexpr const int32_t EPOCH_YEAR = 1970
static constexpr const int32_t YEAR_INTERVAL = 400
static constexpr const int32_t DAYS_PER_YEAR_INTERVAL = 146097

Static Private Member Functions

static void ExtractYearOffset (int32_t &n, int32_t &year, int32_t &year_offset)

Detailed Description

The Date class is a static class that holds helper functions for the Date type.

Member Function Documentation

◆ FromString()

date_t duckdb::Date::FromString ( const string & str,
bool strict = false )
static

Convert a string in the format "YYYY-MM-DD" to a date object.

117521 {
117522 return Date::FromCString(str.c_str(), str.size(), strict);
117523}
static DUCKDB_API date_t FromCString(const char *str, idx_t len, bool strict=false)
Convert a string in the format "YYYY-MM-DD" to a date object.
Definition duckdb.cpp:117506
Here is the call graph for this function:

◆ FromCString()

date_t duckdb::Date::FromCString ( const char * str,
idx_t len,
bool strict = false )
static

Convert a string in the format "YYYY-MM-DD" to a date object.

117506 {
117507 date_t result;
117508 idx_t pos;
117509 bool special = false;
117510 switch (TryConvertDate(buf, len, pos, result, special, strict)) {
117511 case DateCastResult::ERROR_INCORRECT_FORMAT:
117512 throw ConversionException(FormatError(string(buf, len)));
117513 case DateCastResult::ERROR_RANGE:
117514 throw ConversionException(RangeError(string(buf, len)));
117515 case DateCastResult::SUCCESS:
117516 break;
117517 }
117518 return result;
117519}
static DUCKDB_API DateCastResult TryConvertDate(const char *buf, idx_t len, idx_t &pos, date_t &result, bool &special, bool strict=false)
Definition duckdb.cpp:117363
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ToString()

string duckdb::Date::ToString ( date_t date)
static

Convert a date object to a string in the format "YYYY-MM-DD".

117525 {
117526 // PG displays temporal infinities in lowercase,
117527 // but numerics in Titlecase.
117528 if (date == date_t::infinity()) {
117529 return PINF.str;
117530 } else if (date == date_t::ninfinity()) {
117531 return NINF.str;
117532 }
117533 int32_t date_units[3];
117534 idx_t year_length;
117535 bool add_bc;
117536 Date::Convert(date, date_units[0], date_units[1], date_units[2]);
117537
117538 auto length = DateToStringCast::Length(date_units, year_length, add_bc);
117539 auto buffer = make_unsafe_uniq_array_uninitialized<char>(length);
117540 DateToStringCast::Format(buffer.get(), date_units, year_length, add_bc);
117541 return string(buffer.get(), length);
117542}
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
::int32_t int32_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TryConvertDateSpecial()

bool duckdb::Date::TryConvertDateSpecial ( const char * buf,
idx_t len,
idx_t & pos,
const DateSpecial & special )
static

Try to convert the string as a give "special" date (e.g, PINF, ...) Returns true if it was successful and updates the scan pos.

117347 {
117348 auto p = pos;
117349 auto special = s.str;
117350 for (; p < len && *special; ++p) {
117351 const auto s = *special++;
117352 if (!s || StringUtil::CharacterToLower(buf[p]) != s) {
117353 return false;
117354 }
117355 }
117356 if (*special && (p - pos) != s.abbr) {
117357 return false;
117358 }
117359 pos = p;
117360 return true;
117361}
Here is the caller graph for this function:

◆ TryConvertDate()

DateCastResult duckdb::Date::TryConvertDate ( const char * buf,
idx_t len,
idx_t & pos,
date_t & result,
bool & special,
bool strict = false )
static

Try to convert text in a buffer to a date; returns true if parsing was successful If the date was a "special" value, the special flag will be set.

117364 {
117365 special = false;
117366 pos = 0;
117367 if (len == 0) {
117368 return DateCastResult::ERROR_INCORRECT_FORMAT;
117369 }
117370
117371 int32_t day = 0;
117372 int32_t month = -1;
117373 int32_t year = 0;
117374 bool yearneg = false;
117375 int sep;
117376
117377 // skip leading spaces
117378 while (pos < len && StringUtil::CharacterIsSpace(buf[pos])) {
117379 pos++;
117380 }
117381
117382 if (pos >= len) {
117383 return DateCastResult::ERROR_INCORRECT_FORMAT;
117384 }
117385 if (buf[pos] == '-') {
117386 yearneg = true;
117387 pos++;
117388 if (pos >= len) {
117389 return DateCastResult::ERROR_INCORRECT_FORMAT;
117390 }
117391 }
117392 if (!StringUtil::CharacterIsDigit(buf[pos])) {
117393 // Check for special values
117394 if (TryConvertDateSpecial(buf, len, pos, PINF)) {
117395 result = yearneg ? date_t::ninfinity() : date_t::infinity();
117396 } else if (TryConvertDateSpecial(buf, len, pos, EPOCH)) {
117397 result = date_t::epoch();
117398 } else {
117399 return DateCastResult::ERROR_INCORRECT_FORMAT;
117400 }
117401 // skip trailing spaces - parsing must be strict here
117402 while (pos < len && StringUtil::CharacterIsSpace(buf[pos])) {
117403 pos++;
117404 }
117405 special = true;
117406 return (pos == len) ? DateCastResult::SUCCESS : DateCastResult::ERROR_INCORRECT_FORMAT;
117407 }
117408 // first parse the year
117409 idx_t year_length = 0;
117410 for (; pos < len && StringUtil::CharacterIsDigit(buf[pos]); pos++) {
117411 if (year >= 100000000) {
117412 return DateCastResult::ERROR_RANGE;
117413 }
117414 year = (buf[pos] - '0') + year * 10;
117415 year_length++;
117416 }
117417 if (year_length < 2 && strict) {
117418 return DateCastResult::ERROR_INCORRECT_FORMAT;
117419 }
117420 if (yearneg) {
117421 year = -year;
117422 }
117423
117424 if (pos >= len) {
117425 return DateCastResult::ERROR_INCORRECT_FORMAT;
117426 }
117427
117428 // fetch the separator
117429 sep = buf[pos++];
117430 if (sep != ' ' && sep != '-' && sep != '/' && sep != '\\') {
117431 // invalid separator
117432 return DateCastResult::ERROR_INCORRECT_FORMAT;
117433 }
117434
117435 // parse the month
117436 if (!Date::ParseDoubleDigit(buf, len, pos, month)) {
117437 return DateCastResult::ERROR_INCORRECT_FORMAT;
117438 }
117439
117440 if (pos >= len) {
117441 return DateCastResult::ERROR_INCORRECT_FORMAT;
117442 }
117443
117444 if (buf[pos++] != sep) {
117445 return DateCastResult::ERROR_INCORRECT_FORMAT;
117446 }
117447
117448 if (pos >= len) {
117449 return DateCastResult::ERROR_INCORRECT_FORMAT;
117450 }
117451
117452 // now parse the day
117453 if (!Date::ParseDoubleDigit(buf, len, pos, day)) {
117454 return DateCastResult::ERROR_INCORRECT_FORMAT;
117455 }
117456
117457 // check for an optional trailing " (BC)""
117458 if (len - pos >= 5 && StringUtil::CharacterIsSpace(buf[pos]) && buf[pos + 1] == '(' &&
117459 StringUtil::CharacterToLower(buf[pos + 2]) == 'b' && StringUtil::CharacterToLower(buf[pos + 3]) == 'c' &&
117460 buf[pos + 4] == ')') {
117461 if (yearneg || year == 0) {
117462 return DateCastResult::ERROR_INCORRECT_FORMAT;
117463 }
117464 year = -year + 1;
117465 pos += 5;
117466 }
117467
117468 // in strict mode, check remaining string for non-space characters
117469 if (strict) {
117470 // skip trailing spaces
117471 while (pos < len && StringUtil::CharacterIsSpace(buf[pos])) {
117472 pos++;
117473 }
117474 // check position. if end was not reached, non-space chars remaining
117475 if (pos < len) {
117476 return DateCastResult::ERROR_INCORRECT_FORMAT;
117477 }
117478 } else {
117479 // in non-strict mode, check for any direct trailing digits
117480 if (pos < len && StringUtil::CharacterIsDigit(buf[pos])) {
117481 return DateCastResult::ERROR_INCORRECT_FORMAT;
117482 }
117483 }
117484
117485 return Date::TryFromDate(year, month, day, result) ? DateCastResult::SUCCESS : DateCastResult::ERROR_RANGE;
117486}
static DUCKDB_API bool ParseDoubleDigit(const char *buf, idx_t len, idx_t &pos, int32_t &result)
Helper function to parse two digits from a string (e.g. "30" -> 30, "03" -> 3, "3" -> 3).
Definition duckdb.cpp:117336
static DUCKDB_API bool TryConvertDateSpecial(const char *buf, idx_t len, idx_t &pos, const DateSpecial &special)
Definition duckdb.cpp:117347
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Format()

string duckdb::Date::Format ( int32_t year,
int32_t month,
int32_t day )
static

Create a string "YYYY-MM-DD" from a specified (year, month, day) combination

117544 {
117545 return ToString(Date::FromDate(year, month, day));
117546}
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 string ToString(date_t date)
Convert a date object to a string in the format "YYYY-MM-DD".
Definition duckdb.cpp:117525
Here is the call graph for this function:

◆ Convert()

void duckdb::Date::Convert ( date_t date,
int32_t & out_year,
int32_t & out_month,
int32_t & out_day )
static

Extract the year, month and day from a given date object.

117272 {
117273 auto n = d.days;
117274 int32_t year_offset;
117275 Date::ExtractYearOffset(n, year, year_offset);
117276
117277 day = n - Date::CUMULATIVE_YEAR_DAYS[year_offset];
117278 D_ASSERT(day >= 0 && day <= 365);
117279
117280 bool is_leap_year = (Date::CUMULATIVE_YEAR_DAYS[year_offset + 1] - Date::CUMULATIVE_YEAR_DAYS[year_offset]) == 366;
117281 if (is_leap_year) {
117282 month = Date::LEAP_MONTH_PER_DAY_OF_YEAR[day];
117283 day -= Date::CUMULATIVE_LEAP_DAYS[month - 1];
117284 } else {
117285 month = Date::MONTH_PER_DAY_OF_YEAR[day];
117286 day -= Date::CUMULATIVE_DAYS[month - 1];
117287 }
117288 day++;
117289 D_ASSERT(day > 0 && day <= (is_leap_year ? Date::LEAP_DAYS[month] : Date::NORMAL_DAYS[month]));
117290 D_ASSERT(month > 0 && month <= 12);
117291}
Here is the caller graph for this function:

◆ FromDate()

date_t duckdb::Date::FromDate ( int32_t year,
int32_t month,
int32_t day )
static

Create a Date object from a specified (year, month, day) combination.

117328 {
117329 date_t result;
117330 if (!Date::TryFromDate(year, month, day, result)) {
117331 throw ConversionException("Date out of range: %d-%d-%d", year, month, day);
117332 }
117333 return result;
117334}
Here is the caller graph for this function:

◆ TryFromDate()

bool duckdb::Date::TryFromDate ( int32_t year,
int32_t month,
int32_t day,
date_t & result )
static
117293 {
117294 int32_t n = 0;
117295 if (!Date::IsValid(year, month, day)) {
117296 return false;
117297 }
117298 n += Date::IsLeapYear(year) ? Date::CUMULATIVE_LEAP_DAYS[month - 1] : Date::CUMULATIVE_DAYS[month - 1];
117299 n += day - 1;
117300 if (year < 1970) {
117301 int32_t diff_from_base = 1970 - year;
117302 int32_t year_index = 400 - (diff_from_base % 400);
117303 int32_t fractions = diff_from_base / 400;
117304 n += Date::CUMULATIVE_YEAR_DAYS[year_index];
117305 n -= Date::DAYS_PER_YEAR_INTERVAL;
117306 n -= fractions * Date::DAYS_PER_YEAR_INTERVAL;
117307 } else if (year >= 2370) {
117308 int32_t diff_from_base = year - 2370;
117309 int32_t year_index = diff_from_base % 400;
117310 int32_t fractions = diff_from_base / 400;
117311 n += Date::CUMULATIVE_YEAR_DAYS[year_index];
117312 n += Date::DAYS_PER_YEAR_INTERVAL;
117313 n += fractions * Date::DAYS_PER_YEAR_INTERVAL;
117314 } else {
117315 n += Date::CUMULATIVE_YEAR_DAYS[year - 1970];
117316 }
117317#ifdef DEBUG
117318 int32_t y, m, d;
117319 Date::Convert(date_t(n), y, m, d);
117320 D_ASSERT(year == y);
117321 D_ASSERT(month == m);
117322 D_ASSERT(day == d);
117323#endif
117324 result = date_t(n);
117325 return true;
117326}
static DUCKDB_API bool IsLeapYear(int32_t year)
Returns true if (year) is a leap year, and false otherwise.
Definition duckdb.cpp:117548
static DUCKDB_API bool IsValid(int32_t year, int32_t month, int32_t day)
Definition duckdb.cpp:117552

◆ IsLeapYear()

bool duckdb::Date::IsLeapYear ( int32_t year)
static

Returns true if (year) is a leap year, and false otherwise.

117548 {
117549 return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
117550}
Here is the caller graph for this function:

◆ IsValid()

bool duckdb::Date::IsValid ( int32_t year,
int32_t month,
int32_t day )
static

Returns true if the specified (year, month, day) combination is a valid date

117552 {
117553 if (month < 1 || month > 12) {
117554 return false;
117555 }
117556 if (day < 1) {
117557 return false;
117558 }
117559 if (year <= DATE_MIN_YEAR) {
117560 if (year < DATE_MIN_YEAR) {
117561 return false;
117562 } else if (year == DATE_MIN_YEAR) {
117563 if (month < DATE_MIN_MONTH || (month == DATE_MIN_MONTH && day < DATE_MIN_DAY)) {
117564 return false;
117565 }
117566 }
117567 }
117568 if (year >= DATE_MAX_YEAR) {
117569 if (year > DATE_MAX_YEAR) {
117570 return false;
117571 } else if (year == DATE_MAX_YEAR) {
117572 if (month > DATE_MAX_MONTH || (month == DATE_MAX_MONTH && day > DATE_MAX_DAY)) {
117573 return false;
117574 }
117575 }
117576 }
117577 return Date::IsLeapYear(year) ? day <= Date::LEAP_DAYS[month] : day <= Date::NORMAL_DAYS[month];
117578}
Here is the call graph for this function:

◆ IsFinite()

bool duckdb::Date::IsFinite ( date_t date)
inlinestatic

Returns true if the specified date is finite.

5078 {
5079 return date != date_t::infinity() && date != date_t::ninfinity();
5080 }
Here is the caller graph for this function:

◆ MonthDays()

int32_t duckdb::Date::MonthDays ( int32_t year,
int32_t month )
static

The max number of days in a month of a given year.

117580 {
117581 D_ASSERT(month >= 1 && month <= 12);
117582 return Date::IsLeapYear(year) ? Date::LEAP_DAYS[month] : Date::NORMAL_DAYS[month];
117583}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Epoch()

int64_t duckdb::Date::Epoch ( date_t date)
static

Extract the epoch from the date (seconds since 1970-01-01).

117597 {
117598 return ((int64_t)date.days) * Interval::SECS_PER_DAY;
117599}

◆ EpochNanoseconds()

int64_t duckdb::Date::EpochNanoseconds ( date_t date)
static

Extract the epoch from the date (nanoseconds since 1970-01-01).

117601 {
117602 int64_t result;
117603 if (!TryMultiplyOperator::Operation<int64_t, int64_t, int64_t>(date.days, Interval::MICROS_PER_DAY * 1000,
117604 result)) {
117605 throw ConversionException("Could not convert DATE (%s) to nanoseconds", Date::ToString(date));
117606 }
117607 return result;
117608}
::int64_t int64_t
Here is the call graph for this function:

◆ EpochMicroseconds()

int64_t duckdb::Date::EpochMicroseconds ( date_t date)
static

Extract the epoch from the date (microseconds since 1970-01-01).

117610 {
117611 int64_t result;
117612 if (!TryMultiplyOperator::Operation<int64_t, int64_t, int64_t>(date.days, Interval::MICROS_PER_DAY, result)) {
117613 throw ConversionException("Could not convert DATE (%s) to microseconds", Date::ToString(date));
117614 }
117615 return result;
117616}
Here is the call graph for this function:

◆ EpochMilliseconds()

int64_t duckdb::Date::EpochMilliseconds ( date_t date)
static

Extract the epoch from the date (milliseconds since 1970-01-01).

117618 {
117619 int64_t result;
117620 const auto MILLIS_PER_DAY = Interval::MICROS_PER_DAY / Interval::MICROS_PER_MSEC;
117621 if (!TryMultiplyOperator::Operation<int64_t, int64_t, int64_t>(date.days, MILLIS_PER_DAY, result)) {
117622 throw ConversionException("Could not convert DATE (%s) to milliseconds", Date::ToString(date));
117623 }
117624 return result;
117625}
Here is the call graph for this function:

◆ EpochToDate()

date_t duckdb::Date::EpochToDate ( int64_t epoch)
static

Convert the epoch (seconds since 1970-01-01) to a date_t.

117593 {
117594 return date_t(UnsafeNumericCast<int32_t>(epoch / Interval::SECS_PER_DAY));
117595}

◆ EpochDays()

int32_t duckdb::Date::EpochDays ( date_t date)
static

Extract the number of days since epoch (days since 1970-01-01).

117589 {
117590 return date.days;
117591}

◆ EpochDaysToDate()

date_t duckdb::Date::EpochDaysToDate ( int32_t epoch)
static

Convert the epoch number of days to a date_t.

117585 {
117586 return (date_t)epoch;
117587}

◆ ExtractYear()

int32_t duckdb::Date::ExtractYear ( date_t date)
static

Extract year of a date entry.

117627 {
117628 int32_t year, year_offset;
117629 Date::ExtractYearOffset(d.days, year, year_offset);
117630 return year;
117631}

◆ ExtractMonth()

int32_t duckdb::Date::ExtractMonth ( date_t date)
static

Extract month of a date entry.

117633 {
117634 int32_t out_year, out_month, out_day;
117635 Date::Convert(date, out_year, out_month, out_day);
117636 return out_month;
117637}
Here is the call graph for this function:

◆ ExtractDay()

int32_t duckdb::Date::ExtractDay ( date_t date)
static

Extract day of a date entry.

117639 {
117640 int32_t out_year, out_month, out_day;
117641 Date::Convert(date, out_year, out_month, out_day);
117642 return out_day;
117643}
Here is the call graph for this function:

◆ ExtractISODayOfTheWeek()

int32_t duckdb::Date::ExtractISODayOfTheWeek ( date_t date)
static

Extract the day of the week (1-7).

117657 {
117658 // date of 0 is 1970-01-01, which was a Thursday (4)
117659 // -7 = 4
117660 // -6 = 5
117661 // -5 = 6
117662 // -4 = 7
117663 // -3 = 1
117664 // -2 = 2
117665 // -1 = 3
117666 // 0 = 4
117667 // 1 = 5
117668 // 2 = 6
117669 // 3 = 7
117670 // 4 = 1
117671 // 5 = 2
117672 // 6 = 3
117673 // 7 = 4
117674 if (date.days < 0) {
117675 // negative date: start off at 4 and cycle downwards
117676 return UnsafeNumericCast<int32_t>((7 - ((-int64_t(date.days) + 3) % 7)));
117677 } else {
117678 // positive date: start off at 4 and cycle upwards
117679 return UnsafeNumericCast<int32_t>(((int64_t(date.days) + 3) % 7) + 1);
117680 }
117681}
Here is the caller graph for this function:

◆ ExtractDayOfTheYear()

int32_t duckdb::Date::ExtractDayOfTheYear ( date_t date)
static

Extract the day of the year.

117645 {
117646 int32_t year, year_offset;
117647 Date::ExtractYearOffset(date.days, year, year_offset);
117648 return date.days - Date::CUMULATIVE_YEAR_DAYS[year_offset] + 1;
117649}

◆ ExtractJulianDay()

int64_t duckdb::Date::ExtractJulianDay ( date_t date)
static

Extract the day of the year.

117651 {
117652 // Julian Day 0 is (-4713, 11, 24) in the proleptic Gregorian calendar.
117653 static const int64_t JULIAN_EPOCH = -2440588;
117654 return date.days - JULIAN_EPOCH;
117655}
Here is the caller graph for this function:

◆ ExtractISOYearWeek()

void duckdb::Date::ExtractISOYearWeek ( date_t date,
int32_t & year,
int32_t & week )
static

Extract the ISO week number ISO weeks start on Monday and the first week of a year contains January 4 of that year. In the ISO week-numbering system, it is possible for early-January dates to be part of the 52nd or 53rd week of the previous year.

117726 {
117727 week = GetISOYearWeek(date, year);
117728}

◆ ExtractISOWeekNumber()

int32_t duckdb::Date::ExtractISOWeekNumber ( date_t date)
static
117730 {
117731 int32_t year, week;
117732 ExtractISOYearWeek(date, year, week);
117733 return week;
117734}
static DUCKDB_API void ExtractISOYearWeek(date_t date, int32_t &year, int32_t &week)
Definition duckdb.cpp:117726

◆ ExtractISOYearNumber()

int32_t duckdb::Date::ExtractISOYearNumber ( date_t date)
static
117736 {
117737 int32_t year, week;
117738 ExtractISOYearWeek(date, year, week);
117739 return year;
117740}

◆ ExtractWeekNumberRegular()

int32_t duckdb::Date::ExtractWeekNumberRegular ( date_t date,
bool monday_first = true )
static

Extract the week number as Python handles it. Either Monday or Sunday is the first day of the week, and any date before the first Monday/Sunday returns week 0 This is a bit more consistent because week numbers in a year are always incrementing

117742 {
117743 int32_t year, month, day;
117744 Date::Convert(date, year, month, day);
117745 month -= 1;
117746 day -= 1;
117747 // get the day of the year
117748 auto day_of_the_year =
117749 (Date::IsLeapYear(year) ? Date::CUMULATIVE_LEAP_DAYS[month] : Date::CUMULATIVE_DAYS[month]) + day;
117750 // now figure out the first monday or sunday of the year
117751 // what day is January 1st?
117752 auto day_of_jan_first = Date::ExtractISODayOfTheWeek(Date::FromDate(year, 1, 1));
117753 // monday = 1, sunday = 7
117754 int32_t first_week_start;
117755 if (monday_first) {
117756 // have to find next "1"
117757 if (day_of_jan_first == 1) {
117758 // jan 1 is monday: starts immediately
117759 first_week_start = 0;
117760 } else {
117761 // jan 1 is not monday: count days until next monday
117762 first_week_start = 8 - day_of_jan_first;
117763 }
117764 } else {
117765 first_week_start = 7 - day_of_jan_first;
117766 }
117767 if (day_of_the_year < first_week_start) {
117768 // day occurs before first week starts: week 0
117769 return 0;
117770 }
117771 return ((day_of_the_year - first_week_start) / 7) + 1;
117772}
static DUCKDB_API int32_t ExtractISODayOfTheWeek(date_t date)
Extract the day of the week (1-7).
Definition duckdb.cpp:117657
Here is the call graph for this function:

◆ GetMondayOfCurrentWeek()

date_t duckdb::Date::GetMondayOfCurrentWeek ( date_t date)
static

Returns the date of the monday of the current week.

117775 {
117777 return date - (dotw - 1);
117778}
Here is the call graph for this function:

◆ ParseDoubleDigit()

bool duckdb::Date::ParseDoubleDigit ( const char * buf,
idx_t len,
idx_t & pos,
int32_t & result )
static

Helper function to parse two digits from a string (e.g. "30" -> 30, "03" -> 3, "3" -> 3).

117336 {
117337 if (pos < len && StringUtil::CharacterIsDigit(buf[pos])) {
117338 result = buf[pos++] - '0';
117339 if (pos < len && StringUtil::CharacterIsDigit(buf[pos])) {
117340 result = (buf[pos++] - '0') + result * 10;
117341 }
117342 return true;
117343 }
117344 return false;
117345}
Here is the caller graph for this function:

◆ FormatError() [1/2]

string duckdb::Date::FormatError ( const string & str)
static
117488 {
117489 return StringUtil::Format("invalid date field format: \"%s\", "
117490 "expected format is (YYYY-MM-DD)",
117491 str);
117492}
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002

◆ FormatError() [2/2]

string duckdb::Date::FormatError ( string_t str)
static
117502 {
117503 return FormatError(str.GetString());
117504}

◆ RangeError() [1/2]

string duckdb::Date::RangeError ( const string & str)
static
117494 {
117495 return StringUtil::Format("date field value out of range: \"%s\"", str);
117496}

◆ RangeError() [2/2]

string duckdb::Date::RangeError ( string_t str)
static
117498 {
117499 return RangeError(str.GetString());
117500}

◆ ExtractYearOffset()

void duckdb::Date::ExtractYearOffset ( int32_t & n,
int32_t & year,
int32_t & year_offset )
staticprivate
117247 {
117248 year = Date::EPOCH_YEAR;
117249 // first we normalize n to be in the year range [1970, 2370]
117250 // since leap years repeat every 400 years, we can safely normalize just by "shifting" the CumulativeYearDays array
117251 while (n < 0) {
117252 n += Date::DAYS_PER_YEAR_INTERVAL;
117253 year -= Date::YEAR_INTERVAL;
117254 }
117255 while (n >= Date::DAYS_PER_YEAR_INTERVAL) {
117256 n -= Date::DAYS_PER_YEAR_INTERVAL;
117257 year += Date::YEAR_INTERVAL;
117258 }
117259 // interpolation search
117260 // we can find an upper bound of the year by assuming each year has 365 days
117261 year_offset = n / 365;
117262 // because of leap years we might be off by a little bit: compensate by decrementing the year offset until we find
117263 // our year
117264 while (n < Date::CUMULATIVE_YEAR_DAYS[year_offset]) {
117265 year_offset--;
117266 D_ASSERT(year_offset >= 0);
117267 }
117268 year += year_offset;
117269 D_ASSERT(n >= Date::CUMULATIVE_YEAR_DAYS[year_offset]);
117270}

Member Data Documentation

◆ MONTH_NAMES

const string_t duckdb::Date::MONTH_NAMES
static
Initial value:
= {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"}

◆ MONTH_NAMES_ABBREVIATED

const string_t duckdb::Date::MONTH_NAMES_ABBREVIATED
static
Initial value:
= {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}

◆ CUMULATIVE_YEAR_DAYS

const int32_t duckdb::Date::CUMULATIVE_YEAR_DAYS
static
Initial value:
= {
0, 365, 730, 1096, 1461, 1826, 2191, 2557, 2922, 3287, 3652, 4018, 4383, 4748,
5113, 5479, 5844, 6209, 6574, 6940, 7305, 7670, 8035, 8401, 8766, 9131, 9496, 9862,
10227, 10592, 10957, 11323, 11688, 12053, 12418, 12784, 13149, 13514, 13879, 14245, 14610, 14975,
15340, 15706, 16071, 16436, 16801, 17167, 17532, 17897, 18262, 18628, 18993, 19358, 19723, 20089,
20454, 20819, 21184, 21550, 21915, 22280, 22645, 23011, 23376, 23741, 24106, 24472, 24837, 25202,
25567, 25933, 26298, 26663, 27028, 27394, 27759, 28124, 28489, 28855, 29220, 29585, 29950, 30316,
30681, 31046, 31411, 31777, 32142, 32507, 32872, 33238, 33603, 33968, 34333, 34699, 35064, 35429,
35794, 36160, 36525, 36890, 37255, 37621, 37986, 38351, 38716, 39082, 39447, 39812, 40177, 40543,
40908, 41273, 41638, 42004, 42369, 42734, 43099, 43465, 43830, 44195, 44560, 44926, 45291, 45656,
46021, 46387, 46752, 47117, 47482, 47847, 48212, 48577, 48942, 49308, 49673, 50038, 50403, 50769,
51134, 51499, 51864, 52230, 52595, 52960, 53325, 53691, 54056, 54421, 54786, 55152, 55517, 55882,
56247, 56613, 56978, 57343, 57708, 58074, 58439, 58804, 59169, 59535, 59900, 60265, 60630, 60996,
61361, 61726, 62091, 62457, 62822, 63187, 63552, 63918, 64283, 64648, 65013, 65379, 65744, 66109,
66474, 66840, 67205, 67570, 67935, 68301, 68666, 69031, 69396, 69762, 70127, 70492, 70857, 71223,
71588, 71953, 72318, 72684, 73049, 73414, 73779, 74145, 74510, 74875, 75240, 75606, 75971, 76336,
76701, 77067, 77432, 77797, 78162, 78528, 78893, 79258, 79623, 79989, 80354, 80719, 81084, 81450,
81815, 82180, 82545, 82911, 83276, 83641, 84006, 84371, 84736, 85101, 85466, 85832, 86197, 86562,
86927, 87293, 87658, 88023, 88388, 88754, 89119, 89484, 89849, 90215, 90580, 90945, 91310, 91676,
92041, 92406, 92771, 93137, 93502, 93867, 94232, 94598, 94963, 95328, 95693, 96059, 96424, 96789,
97154, 97520, 97885, 98250, 98615, 98981, 99346, 99711, 100076, 100442, 100807, 101172, 101537, 101903,
102268, 102633, 102998, 103364, 103729, 104094, 104459, 104825, 105190, 105555, 105920, 106286, 106651, 107016,
107381, 107747, 108112, 108477, 108842, 109208, 109573, 109938, 110303, 110669, 111034, 111399, 111764, 112130,
112495, 112860, 113225, 113591, 113956, 114321, 114686, 115052, 115417, 115782, 116147, 116513, 116878, 117243,
117608, 117974, 118339, 118704, 119069, 119435, 119800, 120165, 120530, 120895, 121260, 121625, 121990, 122356,
122721, 123086, 123451, 123817, 124182, 124547, 124912, 125278, 125643, 126008, 126373, 126739, 127104, 127469,
127834, 128200, 128565, 128930, 129295, 129661, 130026, 130391, 130756, 131122, 131487, 131852, 132217, 132583,
132948, 133313, 133678, 134044, 134409, 134774, 135139, 135505, 135870, 136235, 136600, 136966, 137331, 137696,
138061, 138427, 138792, 139157, 139522, 139888, 140253, 140618, 140983, 141349, 141714, 142079, 142444, 142810,
143175, 143540, 143905, 144271, 144636, 145001, 145366, 145732, 146097}

◆ MONTH_PER_DAY_OF_YEAR

const int8_t duckdb::Date::MONTH_PER_DAY_OF_YEAR
static
Initial value:
= {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12}

◆ LEAP_MONTH_PER_DAY_OF_YEAR

const int8_t duckdb::Date::LEAP_MONTH_PER_DAY_OF_YEAR
static
Initial value:
= {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12}

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