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
Timestamp Class Reference

The static Timestamp class holds helper functions for the timestamp types. More...

#include <duckdb.hpp>

Static Public Member Functions

static DUCKDB_API timestamp_t FromString (const string &str, bool use_offset)
 Convert a string in the format "YYYY-MM-DD hh:mm:ss[.f][-+TH[:tm]]" to a timestamp object.
static DUCKDB_API TimestampCastResult TryConvertTimestampTZ (const char *str, idx_t len, timestamp_t &result, const bool use_offset, bool &has_offset, string_t &tz, optional_ptr< int32_t > nanos=nullptr)
static DUCKDB_API TimestampCastResult TryConvertTimestamp (const char *str, idx_t len, timestamp_t &result, const bool use_offset, optional_ptr< int32_t > nanos=nullptr, bool strict=false)
 Strict Timestamp does not accept offsets.
static DUCKDB_API TimestampCastResult TryConvertTimestamp (const char *str, idx_t len, timestamp_ns_t &result)
static DUCKDB_API timestamp_t FromCString (const char *str, idx_t len, bool use_offset=false, optional_ptr< int32_t > nanos=nullptr)
static DUCKDB_API string ToString (timestamp_t timestamp)
 Convert a date object to a string in the format "YYYY-MM-DD hh:mm:ss".
static DUCKDB_API date_t GetDate (timestamp_t timestamp)
static DUCKDB_API dtime_t GetTime (timestamp_t timestamp)
static DUCKDB_API dtime_ns_t GetTimeNs (timestamp_ns_t timestamp)
static DUCKDB_API timestamp_t FromDatetime (date_t date, dtime_t time)
 Create a Timestamp object from a specified (date, time) combination.
static DUCKDB_API bool TryFromDatetime (date_t date, dtime_t time, timestamp_t &result)
static DUCKDB_API bool TryFromDatetime (date_t date, dtime_tz_t timetz, timestamp_t &result)
static DUCKDB_API bool TryFromTimestampNanos (timestamp_t ts, int32_t nanos, timestamp_ns_t &result)
 Scale up to ns.
static bool CharacterIsTimeZone (char c)
 Is the character a valid part of a time zone name?
static bool IsFinite (timestamp_t timestamp)
 True, if the timestamp is finite, else false.
static DUCKDB_API void Convert (timestamp_t date, date_t &out_date, dtime_t &out_time)
 Extract the date and time from a given timestamp object.
static DUCKDB_API void Convert (timestamp_ns_t date, date_t &out_date, dtime_t &out_time, int32_t &out_nanos)
 Extract the date and time from a given timestamp object.
static DUCKDB_API timestamp_t GetCurrentTimestamp ()
 Returns current timestamp.
static DUCKDB_API timestamp_t FromEpochSecondsPossiblyInfinite (int64_t s)
 Convert the epoch (in sec) to a timestamp.
static DUCKDB_API timestamp_t FromEpochSeconds (int64_t s)
static DUCKDB_API timestamp_t FromEpochMsPossiblyInfinite (int64_t ms)
 Convert the epoch (in ms) to a timestamp.
static DUCKDB_API timestamp_t FromEpochMs (int64_t ms)
static DUCKDB_API timestamp_t FromEpochMicroSeconds (int64_t micros)
 Convert the epoch (in microseconds) to a timestamp.
static DUCKDB_API timestamp_t FromEpochNanoSecondsPossiblyInfinite (int64_t nanos)
 Convert the epoch (in nanoseconds) to a timestamp.
static DUCKDB_API timestamp_t FromEpochNanoSeconds (int64_t nanos)
static DUCKDB_API timestamp_ns_t TimestampNsFromEpochMicros (int64_t micros)
 Construct ns timestamps from various epoch units.
static DUCKDB_API timestamp_ns_t TimestampNsFromEpochMillis (int64_t millis)
static DUCKDB_API bool TryGetEpochNanoSeconds (timestamp_t timestamp, int64_t &result)
 Try convert a timestamp to epoch (in nanoseconds).
static DUCKDB_API int64_t GetEpochSeconds (timestamp_t timestamp)
 Convert the epoch (in seconds) to a timestamp.
static DUCKDB_API int64_t GetEpochMs (timestamp_t timestamp)
 Convert the epoch (in ms) to a timestamp.
static DUCKDB_API int64_t GetEpochMicroSeconds (timestamp_t timestamp)
 Convert a timestamp to epoch (in microseconds).
static DUCKDB_API int64_t GetEpochNanoSeconds (timestamp_t timestamp)
 Convert a timestamp to epoch (in nanoseconds).
static DUCKDB_API int64_t GetEpochNanoSeconds (timestamp_ns_t timestamp)
static DUCKDB_API int64_t GetEpochRounded (timestamp_t timestamp, const int64_t power_of_ten)
 Convert a timestamp to a rounded epoch at a given resolution.
static DUCKDB_API double GetJulianDay (timestamp_t timestamp)
 Convert a timestamp to a Julian Day.
static DUCKDB_API TimestampComponents GetComponents (timestamp_t timestamp)
 Decompose a timestamp into its components.
static DUCKDB_API time_t ToTimeT (timestamp_t)
static DUCKDB_API timestamp_t FromTimeT (time_t)
static DUCKDB_API bool TryParseUTCOffset (const char *str, idx_t &pos, idx_t len, int &hh, int &mm, int &ss)
static DUCKDB_API string FormatError (const string &str)
static DUCKDB_API string FormatError (string_t str)
static DUCKDB_API string UnsupportedTimezoneError (const string &str)
static DUCKDB_API string UnsupportedTimezoneError (string_t str)
static DUCKDB_API string RangeError (const string &str)
static DUCKDB_API string RangeError (string_t str)

Static Public Attributes

static constexpr const int32_t MIN_YEAR = -290308
static constexpr const int32_t MIN_MONTH = 12
static constexpr const int32_t MIN_DAY = 22

Detailed Description

The static Timestamp class holds helper functions for the timestamp types.

Member Function Documentation

◆ FromString()

timestamp_t duckdb::Timestamp::FromString ( const string & str,
bool use_offset )
static

Convert a string in the format "YYYY-MM-DD hh:mm:ss[.f][-+TH[:tm]]" to a timestamp object.

129733 {
129734 return Timestamp::FromCString(str.c_str(), str.size(), use_offset);
129735}

◆ TryConvertTimestampTZ()

TimestampCastResult duckdb::Timestamp::TryConvertTimestampTZ ( const char * str,
idx_t len,
timestamp_t & result,
const bool use_offset,
bool & has_offset,
string_t & tz,
optional_ptr< int32_t > nanos = nullptr )
static

Convert a string where the offset can also be a time zone string: / [A_Za-z0-9/_]+/ If has_offset is true, then the result is an instant that was offset from UTC If the tz is not empty, the result is still an instant, but the parts can be extracted and applied to the TZ

129474 {
129475 idx_t pos;
129476 date_t date;
129477 dtime_t time;
129478 has_offset = false;
129479 switch (Date::TryConvertDate(str, len, pos, date, has_offset)) {
129480 case DateCastResult::ERROR_INCORRECT_FORMAT:
129481 return TimestampCastResult::ERROR_INCORRECT_FORMAT;
129482 case DateCastResult::ERROR_RANGE:
129483 return TimestampCastResult::ERROR_RANGE;
129484 default:
129485 break;
129486 }
129487 if (pos == len) {
129488 // no time: only a date or special
129489 if (date == date_t::infinity()) {
129490 result = timestamp_t::infinity();
129491 return TimestampCastResult::SUCCESS;
129492 } else if (date == date_t::ninfinity()) {
129493 result = timestamp_t::ninfinity();
129494 return TimestampCastResult::SUCCESS;
129495 }
129496 return Timestamp::TryFromDatetime(date, dtime_t(0), result) ? TimestampCastResult::SUCCESS
129497 : TimestampCastResult::ERROR_RANGE;
129498 }
129499 // try to parse a time field
129500 if (str[pos] == ' ' || str[pos] == 'T') {
129501 pos++;
129502 }
129503 idx_t time_pos = 0;
129504 // TryConvertTime may recursively call us, so we opt for a stricter
129505 // operation. Note that we can't pass strict== true here because we
129506 // want to process any suffix.
129507 if (!Time::TryConvertInterval(str + pos, len - pos, time_pos, time, false, nanos)) {
129508 return TimestampCastResult::ERROR_INCORRECT_FORMAT;
129509 }
129510 // We parsed an interval, so make sure it is in range.
129511 if (time.micros > Interval::MICROS_PER_DAY) {
129512 return TimestampCastResult::ERROR_RANGE;
129513 }
129514 pos += time_pos;
129515 if (!Timestamp::TryFromDatetime(date, time, result)) {
129516 return TimestampCastResult::ERROR_RANGE;
129517 }
129518 if (pos < len) {
129519 // skip a "Z" at the end (as per the ISO8601 specs)
129520 int hh, mm, ss;
129521 if (str[pos] == 'Z') {
129522 pos++;
129523 has_offset = true;
129524 } else if (Timestamp::TryParseUTCOffset(str, pos, len, hh, mm, ss)) {
129525 const int64_t delta =
129526 hh * Interval::MICROS_PER_HOUR + mm * Interval::MICROS_PER_MINUTE + ss * Interval::MICROS_PER_SEC;
129527 if (use_offset && !TrySubtractOperator::Operation(result.value, delta, result.value)) {
129528 return TimestampCastResult::ERROR_RANGE;
129529 }
129530 has_offset = true;
129531 } else {
129532 // Parse a time zone: / [A-Za-z0-9/_]+/
129533 if (str[pos++] != ' ') {
129534 return TimestampCastResult::ERROR_NON_UTC_TIMEZONE;
129535 }
129536 auto tz_name = str + pos;
129537 for (; pos < len && CharacterIsTimeZone(str[pos]); ++pos) {
129538 continue;
129539 }
129540 auto tz_len = str + pos - tz_name;
129541 if (tz_len) {
129542 tz = string_t(tz_name, UnsafeNumericCast<uint32_t>(tz_len));
129543 }
129544 // Note that the caller must reinterpret the instant we return to the given time zone
129545 }
129546
129547 // skip any spaces at the end
129548 while (pos < len && StringUtil::CharacterIsSpace(str[pos])) {
129549 pos++;
129550 }
129551 if (pos < len) {
129552 return TimestampCastResult::ERROR_INCORRECT_FORMAT;
129553 }
129554 }
129555 return TimestampCastResult::SUCCESS;
129556}
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
static bool CharacterIsTimeZone(char c)
Is the character a valid part of a time zone name?
Definition duckdb.hpp:4281
::int64_t int64_t
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ TryConvertTimestamp() [1/2]

TimestampCastResult duckdb::Timestamp::TryConvertTimestamp ( const char * str,
idx_t len,
timestamp_t & result,
const bool use_offset,
optional_ptr< int32_t > nanos = nullptr,
bool strict = false )
static

Strict Timestamp does not accept offsets.

129559 {
129560 string_t tz(nullptr, 0);
129561 bool has_offset = false;
129562 // We don't understand TZ without an extension, so fail if one was provided.
129563 auto success = TryConvertTimestampTZ(str, len, result, use_offset, has_offset, tz, nanos);
129564 if (success != TimestampCastResult::SUCCESS) {
129565 return success;
129566 }
129567 if (tz.GetSize() == 0) {
129568 // no timezone provided - success!
129569 if (strict && has_offset) {
129570 return TimestampCastResult::STRICT_UTC;
129571 }
129572 return TimestampCastResult::SUCCESS;
129573 }
129574 if (tz.GetSize() == 3) {
129575 // we can ONLY handle UTC without ICU being loaded
129576 auto tz_ptr = tz.GetData();
129577 if ((tz_ptr[0] == 'u' || tz_ptr[0] == 'U') && (tz_ptr[1] == 't' || tz_ptr[1] == 'T') &&
129578 (tz_ptr[2] == 'c' || tz_ptr[2] == 'C')) {
129579 if (strict && has_offset) {
129580 return TimestampCastResult::STRICT_UTC;
129581 }
129582 return TimestampCastResult::SUCCESS;
129583 }
129584 }
129585 return TimestampCastResult::ERROR_NON_UTC_TIMEZONE;
129586}
static DUCKDB_API TimestampCastResult TryConvertTimestampTZ(const char *str, idx_t len, timestamp_t &result, const bool use_offset, bool &has_offset, string_t &tz, optional_ptr< int32_t > nanos=nullptr)
Definition duckdb.cpp:129473

◆ TryConvertTimestamp() [2/2]

TimestampCastResult duckdb::Timestamp::TryConvertTimestamp ( const char * str,
idx_t len,
timestamp_ns_t & result )
static
129605 {
129606 int32_t nanos = 0;
129607 auto success = TryConvertTimestamp(str, len, result, true, &nanos);
129608 if (success != TimestampCastResult::SUCCESS) {
129609 return success;
129610 }
129611 if (!TryFromTimestampNanos(result, nanos, result)) {
129612 return TimestampCastResult::ERROR_INCORRECT_FORMAT;
129613 }
129614 return TimestampCastResult::SUCCESS;
129615}
static DUCKDB_API bool TryFromTimestampNanos(timestamp_t ts, int32_t nanos, timestamp_ns_t &result)
Scale up to ns.
Definition duckdb.cpp:129588
static DUCKDB_API TimestampCastResult TryConvertTimestamp(const char *str, idx_t len, timestamp_t &result, const bool use_offset, optional_ptr< int32_t > nanos=nullptr, bool strict=false)
Strict Timestamp does not accept offsets.
Definition duckdb.cpp:129558
::int32_t int32_t

◆ FromCString()

timestamp_t duckdb::Timestamp::FromCString ( const char * str,
idx_t len,
bool use_offset = false,
optional_ptr< int32_t > nanos = nullptr )
static
129645 {
129646 timestamp_t result;
129647 switch (Timestamp::TryConvertTimestamp(str, len, result, use_offset, nanos)) {
129648 case TimestampCastResult::SUCCESS:
129649 case TimestampCastResult::STRICT_UTC:
129650 break;
129651 case TimestampCastResult::ERROR_NON_UTC_TIMEZONE:
129652 throw ConversionException(UnsupportedTimezoneError(string(str, len)));
129653 case TimestampCastResult::ERROR_INCORRECT_FORMAT:
129654 throw ConversionException(FormatError(string(str, len)));
129655 case TimestampCastResult::ERROR_RANGE:
129656 throw ConversionException(RangeError(string(str, len)));
129657 }
129658 return result;
129659}

◆ ToString()

string duckdb::Timestamp::ToString ( timestamp_t timestamp)
static

Convert a date object to a string in the format "YYYY-MM-DD hh:mm:ss".

129737 {
129738 if (timestamp == timestamp_t::infinity()) {
129739 return Date::PINF.str;
129740 }
129741 if (timestamp == timestamp_t::ninfinity()) {
129742 return Date::NINF.str;
129743 }
129744
129745 date_t date;
129746 dtime_t time;
129747 Timestamp::Convert(timestamp, date, time);
129748 return Date::ToString(date) + " " + Time::ToString(time);
129749}
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
static DUCKDB_API string ToString(dtime_t time)
Convert a time object to a string in the format "hh:mm:ss".
Definition duckdb.cpp:129315
static DUCKDB_API void Convert(timestamp_t date, date_t &out_date, dtime_t &out_time)
Extract the date and time from a given timestamp object.
Definition duckdb.cpp:129812

◆ GetDate()

date_t duckdb::Timestamp::GetDate ( timestamp_t timestamp)
static
129751 {
129752 if (DUCKDB_UNLIKELY(timestamp == timestamp_t::infinity())) {
129753 return date_t::infinity();
129754 }
129755 if (DUCKDB_UNLIKELY(timestamp == timestamp_t::ninfinity())) {
129756 return date_t::ninfinity();
129757 }
129758 return date_t(UnsafeNumericCast<int32_t>((timestamp.value + (timestamp.value < 0)) / Interval::MICROS_PER_DAY -
129759 (timestamp.value < 0)));
129760}
cv::GOpaque< int64_t > timestamp(G g)

◆ GetTime()

dtime_t duckdb::Timestamp::GetTime ( timestamp_t timestamp)
static
129762 {
129763 if (!IsFinite(timestamp)) {
129764 throw ConversionException("Can't get TIME of infinite TIMESTAMP");
129765 }
129766 date_t date = Timestamp::GetDate(timestamp);
129767 return dtime_t(timestamp.value - (int64_t(date.days) * int64_t(Interval::MICROS_PER_DAY)));
129768}
static bool IsFinite(timestamp_t timestamp)
True, if the timestamp is finite, else false.
Definition duckdb.hpp:4287

◆ GetTimeNs()

dtime_ns_t duckdb::Timestamp::GetTimeNs ( timestamp_ns_t timestamp)
static
129770 {
129771 if (!IsFinite(input)) {
129772 throw ConversionException("Can't get TIME_NS of infinite TIMESTAMP");
129773 }
129774 date_t date = Timestamp::GetDate(Timestamp::FromEpochNanoSeconds(input.value));
129775 int64_t nanos;
129776 if (!TryMultiplyOperator::Operation<int64_t, int64_t, int64_t>(date.days, Interval::NANOS_PER_DAY, nanos)) {
129777 throw ConversionException("Overflow extracting TIME_NS of TIMESTAMP");
129778 }
129779 return dtime_ns_t(input.value - nanos);
129780}

◆ FromDatetime()

Create a Timestamp object from a specified (date, time) combination.

129804 {
129805 timestamp_t result;
129806 if (!TryFromDatetime(date, time, result)) {
129807 throw ConversionException("Date and time not in timestamp range");
129808 }
129809 return result;
129810}
Here is the caller graph for this function:

◆ TryFromDatetime() [1/2]

bool duckdb::Timestamp::TryFromDatetime ( date_t date,
dtime_t time,
timestamp_t & result )
static
129782 {
129783 if (!TryMultiplyOperator::Operation<int64_t, int64_t, int64_t>(date.days, Interval::MICROS_PER_DAY, result.value)) {
129784 return false;
129785 }
129786 if (!TryAddOperator::Operation<int64_t, int64_t, int64_t>(result.value, time.micros, result.value)) {
129787 return false;
129788 }
129789 return Timestamp::IsFinite(result);
129790}

◆ TryFromDatetime() [2/2]

bool duckdb::Timestamp::TryFromDatetime ( date_t date,
dtime_tz_t timetz,
timestamp_t & result )
static
129792 {
129793 if (!TryFromDatetime(date, timetz.time(), result)) {
129794 return false;
129795 }
129796 // Offset is in seconds
129797 const auto offset = int64_t(timetz.offset() * Interval::MICROS_PER_SEC);
129798 if (!TryAddOperator::Operation(result.value, -offset, result.value)) {
129799 return false;
129800 }
129801 return Timestamp::IsFinite(result);
129802}

◆ TryFromTimestampNanos()

Scale up to ns.

129588 {
129589 if (!IsFinite(input)) {
129590 result.value = input.value;
129591 return true;
129592 }
129593 // Scale to ns
129594 if (!TryMultiplyOperator::Operation(input.value, Interval::NANOS_PER_MICRO, result.value)) {
129595 return false;
129596 }
129597
129598 if (!TryAddOperator::Operation(result.value, int64_t(nanos), result.value)) {
129599 return false;
129600 }
129601
129602 return IsFinite(result);
129603}

◆ CharacterIsTimeZone()

bool duckdb::Timestamp::CharacterIsTimeZone ( char c)
inlinestatic

Is the character a valid part of a time zone name?

4281 {
4282 return StringUtil::CharacterIsAlpha(c) || StringUtil::CharacterIsDigit(c) || c == '_' || c == '/' || c == '+' ||
4283 c == '-';
4284 }

◆ IsFinite()

bool duckdb::Timestamp::IsFinite ( timestamp_t timestamp)
inlinestatic

True, if the timestamp is finite, else false.

4287 {
4288 return timestamp != timestamp_t::infinity() && timestamp != timestamp_t::ninfinity();
4289 }
Here is the caller graph for this function:

◆ Convert() [1/2]

void duckdb::Timestamp::Convert ( timestamp_t date,
date_t & out_date,
dtime_t & out_time )
static

Extract the date and time from a given timestamp object.

129812 {
129813 out_date = GetDate(timestamp);
129814 int64_t days_micros;
129815 if (!TryMultiplyOperator::Operation<int64_t, int64_t, int64_t>(out_date.days, Interval::MICROS_PER_DAY,
129816 days_micros)) {
129817 throw ConversionException("Date out of range in timestamp conversion");
129818 }
129819 out_time = dtime_t(timestamp.value - days_micros);
129820 D_ASSERT(timestamp == Timestamp::FromDatetime(out_date, out_time));
129821}
static DUCKDB_API timestamp_t FromDatetime(date_t date, dtime_t time)
Create a Timestamp object from a specified (date, time) combination.
Definition duckdb.cpp:129804
Here is the caller graph for this function:

◆ Convert() [2/2]

void duckdb::Timestamp::Convert ( timestamp_ns_t date,
date_t & out_date,
dtime_t & out_time,
int32_t & out_nanos )
static

Extract the date and time from a given timestamp object.

129823 {
129824 timestamp_t ms(TemporalRound(input.value, Interval::NANOS_PER_MICRO));
129825 out_date = Timestamp::GetDate(ms);
129826 int64_t days_nanos;
129827 if (!TryMultiplyOperator::Operation<int64_t, int64_t, int64_t>(out_date.days, Interval::NANOS_PER_DAY,
129828 days_nanos)) {
129829 throw ConversionException("Date out of range in timestamp_ns conversion");
129830 }
129831
129832 out_time = dtime_t((input.value - days_nanos) / Interval::NANOS_PER_MICRO);
129833 out_nanos = UnsafeNumericCast<int32_t>((input.value - days_nanos) % Interval::NANOS_PER_MICRO);
129834}

◆ GetCurrentTimestamp()

Returns current timestamp.

129836 {
129837 auto now = system_clock::now();
129838 auto epoch_micros = duration_cast<microseconds>(now.time_since_epoch()).count();
129839 return FromEpochMicroSeconds(epoch_micros);
129840}
static DUCKDB_API timestamp_t FromEpochMicroSeconds(int64_t micros)
Convert the epoch (in microseconds) to a timestamp.
Definition duckdb.cpp:129868

◆ FromEpochSecondsPossiblyInfinite()

Convert the epoch (in sec) to a timestamp.

129842 {
129843 int64_t result;
129844 if (!TryMultiplyOperator::Operation(sec, Interval::MICROS_PER_SEC, result)) {
129845 throw ConversionException("Could not convert Timestamp(S) to Timestamp(US)");
129846 }
129847 return timestamp_t(result);
129848}

◆ FromEpochSeconds()

timestamp_t duckdb::Timestamp::FromEpochSeconds ( int64_t s)
static
129850 {
129851 D_ASSERT(Timestamp::IsFinite(timestamp_t(sec)));
129853}
static DUCKDB_API timestamp_t FromEpochSecondsPossiblyInfinite(int64_t s)
Convert the epoch (in sec) to a timestamp.
Definition duckdb.cpp:129842

◆ FromEpochMsPossiblyInfinite()

Convert the epoch (in ms) to a timestamp.

129855 {
129856 int64_t result;
129857 if (!TryMultiplyOperator::Operation(ms, Interval::MICROS_PER_MSEC, result)) {
129858 throw ConversionException("Could not convert Timestamp(MS) to Timestamp(US)");
129859 }
129860 return timestamp_t(result);
129861}

◆ FromEpochMs()

timestamp_t duckdb::Timestamp::FromEpochMs ( int64_t ms)
static
129863 {
129864 D_ASSERT(Timestamp::IsFinite(timestamp_t(ms)));
129865 return FromEpochMsPossiblyInfinite(ms);
129866}
static DUCKDB_API timestamp_t FromEpochMsPossiblyInfinite(int64_t ms)
Convert the epoch (in ms) to a timestamp.
Definition duckdb.cpp:129855

◆ FromEpochMicroSeconds()

Convert the epoch (in microseconds) to a timestamp.

129868 {
129869 return timestamp_t(micros);
129870}

◆ FromEpochNanoSecondsPossiblyInfinite()

Convert the epoch (in nanoseconds) to a timestamp.

129872 {
129873 return timestamp_t(ns / Interval::NANOS_PER_MICRO);
129874}

◆ FromEpochNanoSeconds()

timestamp_t duckdb::Timestamp::FromEpochNanoSeconds ( int64_t nanos)
static
129876 {
129877 D_ASSERT(Timestamp::IsFinite(timestamp_t(ns)));
129879}
static DUCKDB_API timestamp_t FromEpochNanoSecondsPossiblyInfinite(int64_t nanos)
Convert the epoch (in nanoseconds) to a timestamp.
Definition duckdb.cpp:129872

◆ TimestampNsFromEpochMicros()

Construct ns timestamps from various epoch units.

129890 {
129891 D_ASSERT(Timestamp::IsFinite(timestamp_t(micros)));
129892 timestamp_ns_t result;
129893 if (!TryMultiplyOperator::Operation(micros, Interval::NANOS_PER_MSEC, result.value)) {
129894 throw ConversionException("Could not convert Timestamp(MS) to Timestamp(NS)");
129895 }
129896 return result;
129897}

◆ TimestampNsFromEpochMillis()

timestamp_ns_t duckdb::Timestamp::TimestampNsFromEpochMillis ( int64_t millis)
static
129881 {
129882 D_ASSERT(Timestamp::IsFinite(timestamp_t(millis)));
129883 timestamp_ns_t result;
129884 if (!TryMultiplyOperator::Operation(millis, Interval::NANOS_PER_MICRO, result.value)) {
129885 throw ConversionException("Could not convert Timestamp(US) to Timestamp(NS)");
129886 }
129887 return result;
129888}

◆ TryGetEpochNanoSeconds()

bool duckdb::Timestamp::TryGetEpochNanoSeconds ( timestamp_t timestamp,
int64_t & result )
static

Try convert a timestamp to epoch (in nanoseconds).

129913 {
129914 D_ASSERT(Timestamp::IsFinite(timestamp));
129915 if (!TryMultiplyOperator::Operation(timestamp.value, Interval::NANOS_PER_MICRO, result)) {
129916 return false;
129917 }
129918 return true;
129919}

◆ GetEpochSeconds()

Convert the epoch (in seconds) to a timestamp.

129899 {
129900 D_ASSERT(Timestamp::IsFinite(timestamp));
129901 return timestamp.value / Interval::MICROS_PER_SEC;
129902}

◆ GetEpochMs()

Convert the epoch (in ms) to a timestamp.

129904 {
129905 D_ASSERT(Timestamp::IsFinite(timestamp));
129906 return timestamp.value / Interval::MICROS_PER_MSEC;
129907}

◆ GetEpochMicroSeconds()

Convert a timestamp to epoch (in microseconds).

129909 {
129910 return timestamp.value;
129911}

◆ GetEpochNanoSeconds() [1/2]

Convert a timestamp to epoch (in nanoseconds).

129921 {
129922 int64_t result;
129923 D_ASSERT(Timestamp::IsFinite(timestamp));
129924 if (!TryGetEpochNanoSeconds(timestamp, result)) {
129925 throw ConversionException("Could not convert Timestamp(US) to Timestamp(NS)");
129926 }
129927 return result;
129928}
static DUCKDB_API bool TryGetEpochNanoSeconds(timestamp_t timestamp, int64_t &result)
Try convert a timestamp to epoch (in nanoseconds).
Definition duckdb.cpp:129913

◆ GetEpochNanoSeconds() [2/2]

129930 {
129931 D_ASSERT(Timestamp::IsFinite(timestamp));
129932 return timestamp.value;
129933}

◆ GetEpochRounded()

int64_t duckdb::Timestamp::GetEpochRounded ( timestamp_t timestamp,
const int64_t power_of_ten )
static

Convert a timestamp to a rounded epoch at a given resolution.

129935 {
129936 D_ASSERT(Timestamp::IsFinite(input));
129937 // Round away from the epoch.
129938 // Scale first so we don't overflow.
129939 const auto scaling = power_of_ten / 2;
129940 input.value /= scaling;
129941 if (input.value < 0) {
129942 --input.value;
129943 } else {
129944 ++input.value;
129945 }
129946 input.value /= 2;
129947 return input.value;
129948}

◆ GetJulianDay()

double duckdb::Timestamp::GetJulianDay ( timestamp_t timestamp)
static

Convert a timestamp to a Julian Day.

129950 {
129951 double result = double(Timestamp::GetTime(timestamp).micros);
129952 result /= Interval::MICROS_PER_DAY;
129953 result += double(Date::ExtractJulianDay(Timestamp::GetDate(timestamp)));
129954 return result;
129955}
static DUCKDB_API int64_t ExtractJulianDay(date_t date)
Extract the day of the year.
Definition duckdb.cpp:117651

◆ GetComponents()

TimestampComponents duckdb::Timestamp::GetComponents ( timestamp_t timestamp)
static

Decompose a timestamp into its components.

129957 {
129958 date_t date;
129959 dtime_t time;
129960
129961 Convert(timestamp, date, time);
129962
129963 TimestampComponents result;
129964 Date::Convert(date, result.year, result.month, result.day);
129965 Time::Convert(time, result.hour, result.minute, result.second, result.microsecond);
129966 return result;
129967}
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 void Convert(dtime_t time, int32_t &out_hour, int32_t &out_min, int32_t &out_sec, int32_t &out_micros)
Extract the time from a given timestamp object.
Definition duckdb.cpp:129382

◆ ToTimeT()

time_t duckdb::Timestamp::ToTimeT ( timestamp_t timestamp)
static
129969 {
129970 auto components = Timestamp::GetComponents(timestamp);
129971 struct tm tm {};
129972 tm.tm_year = components.year - 1900;
129973 tm.tm_mon = components.month - 1;
129974 tm.tm_mday = components.day;
129975 tm.tm_hour = components.hour;
129976 tm.tm_min = components.minute;
129977 tm.tm_sec = components.second;
129978 tm.tm_isdst = 0;
129979 return mktime(&tm);
129980}
static DUCKDB_API TimestampComponents GetComponents(timestamp_t timestamp)
Decompose a timestamp into its components.
Definition duckdb.cpp:129957

◆ FromTimeT()

timestamp_t duckdb::Timestamp::FromTimeT ( time_t time)
static
129982 {
129983#ifdef DUCKDB_WINDOWS
129984 auto tm = localtime(&time);
129985#else
129986 struct tm tm_storage {};
129987 auto tm = localtime_r(&time, &tm_storage);
129988#endif
129989 if (!tm) {
129990 throw InternalException("FromTimeT failed: null pointer returned");
129991 }
129992
129993 int32_t year = tm->tm_year + 1900;
129994 int32_t month = tm->tm_mon + 1;
129995 int32_t day = tm->tm_mday;
129996 int32_t hour = tm->tm_hour;
129997 int32_t min = tm->tm_min;
129998 int32_t sec = tm->tm_sec;
129999
130000 auto dt = Date::FromDate(year, month, day);
130001 auto t = Time::FromTime(hour, min, sec, 0);
130002 return FromDatetime(dt, t);
130003}
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 softfloat min()

◆ TryParseUTCOffset()

bool duckdb::Timestamp::TryParseUTCOffset ( const char * str,
idx_t & pos,
idx_t len,
int & hh,
int & mm,
int & ss )
static
129661 {
129662 mm = 0;
129663 ss = 0;
129664 idx_t curpos = pos;
129665 // parse the next 3 characters
129666 if (curpos + 3 > len) {
129667 // no characters left to parse
129668 return false;
129669 }
129670 char sign_char = str[curpos];
129671 if (sign_char != '+' && sign_char != '-') {
129672 // expected either + or -
129673 return false;
129674 }
129675 curpos++;
129676 if (!StringUtil::CharacterIsDigit(str[curpos]) || !StringUtil::CharacterIsDigit(str[curpos + 1])) {
129677 // expected +HH or -HH
129678 return false;
129679 }
129680 hh = (str[curpos] - '0') * 10 + (str[curpos + 1] - '0');
129681 if (sign_char == '-') {
129682 hh = -hh;
129683 }
129684 curpos += 2;
129685
129686 // optional minute specifier: expected either "MM" or ":MM"
129687 if (curpos >= len) {
129688 // done, nothing left
129689 pos = curpos;
129690 return true;
129691 }
129692 const bool colons_used = (str[curpos] == ':');
129693 if (colons_used) {
129694 curpos++;
129695 }
129696 if (curpos + 2 > len || !StringUtil::CharacterIsDigit(str[curpos]) ||
129697 !StringUtil::CharacterIsDigit(str[curpos + 1])) {
129698 // no MM specifier
129699 pos = curpos;
129700 return true;
129701 }
129702 // we have an MM specifier: parse it
129703 mm = (str[curpos] - '0') * 10 + (str[curpos + 1] - '0');
129704 if (sign_char == '-') {
129705 mm = -mm;
129706 }
129707 curpos += 2;
129708
129709 // optional seconds specifier: must be ":SS"
129710 if (curpos >= len || !colons_used || (str[curpos] != ':')) {
129711 // done, nothing left
129712 pos = curpos;
129713 return true;
129714 }
129715 // Skip colon and read seconds
129716 curpos++;
129717 if (curpos + 2 > len || !StringUtil::CharacterIsDigit(str[curpos]) ||
129718 !StringUtil::CharacterIsDigit(str[curpos + 1])) {
129719 // no SS specifier
129720 pos = curpos;
129721 return true;
129722 }
129723 // we have an SS specifier: parse it
129724 ss = (str[curpos] - '0') * 10 + (str[curpos + 1] - '0');
129725 if (sign_char == '-') {
129726 ss = -ss;
129727 }
129728 pos = curpos + 2;
129729
129730 return true;
129731}

◆ FormatError() [1/2]

string duckdb::Timestamp::FormatError ( const string & str)
static
129617 {
129618 return StringUtil::Format("invalid timestamp field format: \"%s\", "
129619 "expected format is (YYYY-MM-DD HH:MM:SS[.US][±HH[:MM[:SS]]| ZONE])",
129620 str);
129621}
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002

◆ FormatError() [2/2]

string duckdb::Timestamp::FormatError ( string_t str)
static
129633 {
129634 return Timestamp::FormatError(str.GetString());
129635}

◆ UnsupportedTimezoneError() [1/2]

string duckdb::Timestamp::UnsupportedTimezoneError ( const string & str)
static
129623 {
129624 return StringUtil::Format("timestamp field value \"%s\" has a timestamp that is not UTC.\nUse the TIMESTAMPTZ type "
129625 "with the ICU extension loaded to handle non-UTC timestamps.",
129626 str);
129627}

◆ UnsupportedTimezoneError() [2/2]

string duckdb::Timestamp::UnsupportedTimezoneError ( string_t str)
static
129637 {
129638 return Timestamp::UnsupportedTimezoneError(str.GetString());
129639}

◆ RangeError() [1/2]

string duckdb::Timestamp::RangeError ( const string & str)
static
129629 {
129630 return StringUtil::Format("timestamp field value out of range: \"%s\"", str);
129631}

◆ RangeError() [2/2]

string duckdb::Timestamp::RangeError ( string_t str)
static
129641 {
129642 return Timestamp::RangeError(str.GetString());
129643}

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