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

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

#include <duckdb.hpp>

Static Public Member Functions

static DUCKDB_API dtime_t FromString (const string &str, bool strict=false, optional_ptr< int32_t > nanos=nullptr)
 Convert a string in the format "hh:mm:ss" to a time object.
static DUCKDB_API dtime_t FromCString (const char *buf, idx_t len, bool strict=false, optional_ptr< int32_t > nanos=nullptr)
static DUCKDB_API bool TryConvertTime (const char *buf, idx_t len, idx_t &pos, dtime_t &result, bool strict=false, optional_ptr< int32_t > nanos=nullptr)
static DUCKDB_API bool TryConvertTimeTZ (const char *buf, idx_t len, idx_t &pos, dtime_tz_t &result, bool &has_offset, bool strict=false, optional_ptr< int32_t > nanos=nullptr)
static DUCKDB_API bool TryConvertInterval (const char *buf, idx_t len, idx_t &pos, dtime_t &result, bool strict=false, optional_ptr< int32_t > nanos=nullptr)
static DUCKDB_API string ToString (dtime_t time)
 Convert a time object to a string in the format "hh:mm:ss".
static DUCKDB_API string ToUTCOffset (int hour_offset, int minute_offset)
 Convert a UTC offset to ±HH[:MM].
static DUCKDB_API dtime_t FromTime (int32_t hour, int32_t minute, int32_t second, int32_t microseconds=0)
static DUCKDB_API int64_t ToNanoTime (int32_t hour, int32_t minute, int32_t second, int32_t nanoseconds=0)
static DUCKDB_API dtime_t NormalizeTimeTZ (dtime_tz_t timetz)
 Normalize a TIME_TZ by adding the offset to the time part and returning the TIME.
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.
static DUCKDB_API string ConversionError (const string &str)
static DUCKDB_API string ConversionError (string_t str)
static DUCKDB_API dtime_t FromTimeMs (int64_t time_ms)
static DUCKDB_API dtime_t FromTimeNs (int64_t time_ns)
static DUCKDB_API bool IsValidTime (int32_t hour, int32_t minute, int32_t second, int32_t microseconds)

Static Private Member Functions

static bool TryConvertInternal (const char *buf, idx_t len, idx_t &pos, dtime_t &result, bool strict, optional_ptr< int32_t > nanos=nullptr)

Detailed Description

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

Member Function Documentation

◆ FromString()

dtime_t duckdb::Time::FromString ( const string & str,
bool strict = false,
optional_ptr< int32_t > nanos = nullptr )
static

Convert a string in the format "hh:mm:ss" to a time object.

129311 {
129312 return Time::FromCString(str.c_str(), str.size(), strict, nanos);
129313}

◆ FromCString()

dtime_t duckdb::Time::FromCString ( const char * buf,
idx_t len,
bool strict = false,
optional_ptr< int32_t > nanos = nullptr )
static
129302 {
129303 dtime_t result;
129304 idx_t pos;
129305 if (!Time::TryConvertTime(buf, len, pos, result, strict, nanos)) {
129306 throw ConversionException(ConversionError(string(buf, len)));
129307 }
129308 return result;
129309}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ TryConvertTime()

bool duckdb::Time::TryConvertTime ( const char * buf,
idx_t len,
idx_t & pos,
dtime_t & result,
bool strict = false,
optional_ptr< int32_t > nanos = nullptr )
static
129211 {
129212 if (!Time::TryConvertInternal(buf, len, pos, result, strict, nanos)) {
129213 if (!strict) {
129214 // last chance, check if we can parse as timestamp
129215 timestamp_t timestamp;
129216 if (Timestamp::TryConvertTimestamp(buf, len, timestamp, false, nanos) == TimestampCastResult::SUCCESS) {
129217 if (!Timestamp::IsFinite(timestamp)) {
129218 return false;
129219 }
129220 result = Timestamp::GetTime(timestamp);
129221 return true;
129222 }
129223 }
129224 return false;
129225 }
129226 return result.micros <= Interval::MICROS_PER_DAY;
129227}
static bool IsFinite(timestamp_t timestamp)
True, if the timestamp is finite, else false.
Definition duckdb.hpp:4287
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
cv::GOpaque< int64_t > timestamp(G g)

◆ TryConvertTimeTZ()

bool duckdb::Time::TryConvertTimeTZ ( const char * buf,
idx_t len,
idx_t & pos,
dtime_tz_t & result,
bool & has_offset,
bool strict = false,
optional_ptr< int32_t > nanos = nullptr )
static
129230 {
129231 dtime_t time_part;
129232 has_offset = false;
129233 if (!Time::TryConvertInternal(buf, len, pos, time_part, false, nanos)) {
129234 if (!strict) {
129235 // last chance, check if we can parse as timestamp
129236 timestamp_t timestamp;
129237 if (Timestamp::TryConvertTimestamp(buf, len, timestamp, true, nanos) == TimestampCastResult::SUCCESS) {
129238 if (!Timestamp::IsFinite(timestamp)) {
129239 return false;
129240 }
129241 result = dtime_tz_t(Timestamp::GetTime(timestamp), 0);
129242 return true;
129243 }
129244 }
129245 return false;
129246 }
129247
129248 // skip optional whitespace before offset
129249 while (pos < len && StringUtil::CharacterIsSpace(buf[pos])) {
129250 pos++;
129251 }
129252
129253 // Get the ±HH[:MM[:SS]] part
129254 // (PG claims they don't support :SS but they do...)
129255 int hh = 0;
129256 int mm = 0;
129257 int ss = 0;
129258 has_offset = (pos < len);
129259 if (has_offset && !Timestamp::TryParseUTCOffset(buf, pos, len, hh, mm, ss)) {
129260 return false;
129261 }
129262
129263 // Offsets are in seconds in the open interval (-16:00:00, +16:00:00)
129264 int32_t offset = ((hh * Interval::MINS_PER_HOUR) + mm) * Interval::SECS_PER_MINUTE + ss;
129265
129266 if (offset < dtime_tz_t::MIN_OFFSET || offset > dtime_tz_t::MAX_OFFSET) {
129267 return false;
129268 }
129269
129270 // in strict mode, check remaining string for non-space characters
129271 if (strict) {
129272 // skip trailing spaces
129273 while (pos < len && StringUtil::CharacterIsSpace(buf[pos])) {
129274 pos++;
129275 }
129276 // check position. if end was not reached, non-space chars remaining
129277 if (pos < len) {
129278 return false;
129279 }
129280 }
129281
129282 result = dtime_tz_t(time_part, offset);
129283
129284 return true;
129285}
::int32_t int32_t

◆ TryConvertInterval()

bool duckdb::Time::TryConvertInterval ( const char * buf,
idx_t len,
idx_t & pos,
dtime_t & result,
bool strict = false,
optional_ptr< int32_t > nanos = nullptr )
static
129206 {
129207 return Time::TryConvertInternal(buf, len, pos, result, strict, nanos);
129208}

◆ ToString()

string duckdb::Time::ToString ( dtime_t time)
static

Convert a time object to a string in the format "hh:mm:ss".

129315 {
129316 int32_t time_units[4];
129317 Time::Convert(time, time_units[0], time_units[1], time_units[2], time_units[3]);
129318
129319 char micro_buffer[6];
129320 auto length = TimeToStringCast::Length(time_units, micro_buffer);
129321 auto buffer = make_unsafe_uniq_array_uninitialized<char>(length);
129322 TimeToStringCast::Format(buffer.get(), length, time_units, micro_buffer);
129323 return string(buffer.get(), length);
129324}
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

◆ ToUTCOffset()

string duckdb::Time::ToUTCOffset ( int hour_offset,
int minute_offset )
static

Convert a UTC offset to ±HH[:MM].

129326 {
129327 dtime_t time((hour_offset * Interval::MINS_PER_HOUR + minute_offset) * Interval::MICROS_PER_MINUTE);
129328
129329 char buffer[1 + 2 + 1 + 2];
129330 idx_t length = 0;
129331 buffer[length++] = (time.micros < 0 ? '-' : '+');
129332 time.micros = std::abs(time.micros);
129333
129334 int32_t time_units[4];
129335 Time::Convert(time, time_units[0], time_units[1], time_units[2], time_units[3]);
129336
129337 TimeToStringCast::FormatTwoDigits(buffer + length, time_units[0]);
129338 length += 2;
129339 if (time_units[1]) {
129340 buffer[length++] = ':';
129341 TimeToStringCast::FormatTwoDigits(buffer + length, time_units[1]);
129342 length += 2;
129343 }
129344
129345 return string(buffer, length);
129346}

◆ FromTime()

dtime_t duckdb::Time::FromTime ( int32_t hour,
int32_t minute,
int32_t second,
int32_t microseconds = 0 )
static
129348 {
129349 int64_t result;
129350 result = hour; // hours
129351 result = result * Interval::MINS_PER_HOUR + minute; // hours -> minutes
129352 result = result * Interval::SECS_PER_MINUTE + second; // minutes -> seconds
129353 result = result * Interval::MICROS_PER_SEC + microseconds; // seconds -> microseconds
129354 return dtime_t(result);
129355}
::int64_t int64_t

◆ ToNanoTime()

int64_t duckdb::Time::ToNanoTime ( int32_t hour,
int32_t minute,
int32_t second,
int32_t nanoseconds = 0 )
static
129357 {
129358 int64_t result;
129359 result = hour; // hours
129360 result = result * Interval::MINS_PER_HOUR + minute; // hours -> minutes
129361 result = result * Interval::SECS_PER_MINUTE + second; // minutes -> seconds
129362 result = result * Interval::NANOS_PER_SEC + nanoseconds; // seconds -> nanoseconds
129363 return result;
129364}

◆ NormalizeTimeTZ()

dtime_t duckdb::Time::NormalizeTimeTZ ( dtime_tz_t timetz)
static

Normalize a TIME_TZ by adding the offset to the time part and returning the TIME.

129287 {
129288 date_t date(0);
129289 return Interval::Add(timetz.time(), {0, 0, -timetz.offset() * Interval::MICROS_PER_SEC}, date);
129290}
static date_t Add(date_t left, interval_t right)
Add an interval to a date.
Definition duckdb.cpp:123571

◆ Convert()

void duckdb::Time::Convert ( dtime_t time,
int32_t & out_hour,
int32_t & out_min,
int32_t & out_sec,
int32_t & out_micros )
static

Extract the time from a given timestamp object.

129382 {
129383 int64_t time = dtime.micros;
129384 hour = int32_t(time / Interval::MICROS_PER_HOUR);
129385 time -= int64_t(hour) * Interval::MICROS_PER_HOUR;
129386 min = int32_t(time / Interval::MICROS_PER_MINUTE);
129387 time -= int64_t(min) * Interval::MICROS_PER_MINUTE;
129388 sec = int32_t(time / Interval::MICROS_PER_SEC);
129389 time -= int64_t(sec) * Interval::MICROS_PER_SEC;
129390 micros = int32_t(time);
129391 D_ASSERT(Time::IsValidTime(hour, min, sec, micros));
129392}
static softfloat min()
Here is the caller graph for this function:

◆ ConversionError() [1/2]

string duckdb::Time::ConversionError ( const string & str)
static
129292 {
129293 return StringUtil::Format("time field value out of range: \"%s\", "
129294 "expected format is ([YYYY-MM-DD ]HH:MM:SS[.MS])",
129295 str);
129296}
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002

◆ ConversionError() [2/2]

string duckdb::Time::ConversionError ( string_t str)
static
129298 {
129299 return Time::ConversionError(str.GetString());
129300}

◆ FromTimeMs()

dtime_t duckdb::Time::FromTimeMs ( int64_t time_ms)
static
129394 {
129395 int64_t result;
129396 if (!TryMultiplyOperator::Operation(time_ms, Interval::MICROS_PER_MSEC, result)) {
129397 throw ConversionException("Could not convert Time(MS) to Time(US)");
129398 }
129399 return dtime_t(result);
129400}

◆ FromTimeNs()

dtime_t duckdb::Time::FromTimeNs ( int64_t time_ns)
static
129402 {
129403 return dtime_t(time_ns / Interval::NANOS_PER_MICRO);
129404}

◆ IsValidTime()

bool duckdb::Time::IsValidTime ( int32_t hour,
int32_t minute,
int32_t second,
int32_t microseconds )
static
129366 {
129367 if (hour < 0 || hour >= 24) {
129368 return (hour == 24) && (minute == 0) && (second == 0) && (microseconds == 0);
129369 }
129370 if (minute < 0 || minute >= 60) {
129371 return false;
129372 }
129373 if (second < 0 || second > 60) {
129374 return false;
129375 }
129376 if (microseconds < 0 || microseconds > 1000000) {
129377 return false;
129378 }
129379 return true;
129380}

◆ TryConvertInternal()

bool duckdb::Time::TryConvertInternal ( const char * buf,
idx_t len,
idx_t & pos,
dtime_t & result,
bool strict,
optional_ptr< int32_t > nanos = nullptr )
staticprivate
129092 {
129093 int32_t hour = -1, min = -1, sec = -1, micros = -1;
129094 pos = 0;
129095
129096 if (len == 0) {
129097 return false;
129098 }
129099
129100 int sep;
129101
129102 // skip leading spaces
129103 while (pos < len && StringUtil::CharacterIsSpace(buf[pos])) {
129104 pos++;
129105 }
129106
129107 if (pos >= len) {
129108 return false;
129109 }
129110
129111 if (!StringUtil::CharacterIsDigit(buf[pos])) {
129112 return false;
129113 }
129114
129115 // Allow up to 9 digit hours to support intervals
129116 hour = 0;
129117 for (int32_t digits = 9; pos < len && StringUtil::CharacterIsDigit(buf[pos]); ++pos) {
129118 if (digits-- > 0) {
129119 hour = hour * 10 + (buf[pos] - '0');
129120 } else {
129121 return false;
129122 }
129123 }
129124
129125 if (pos >= len) {
129126 return false;
129127 }
129128
129129 // fetch the separator
129130 sep = buf[pos++];
129131 if (sep != ':') {
129132 // invalid separator
129133 return false;
129134 }
129135 idx_t sep_pos = pos;
129136 if (pos == len && !strict) {
129137 min = 0;
129138 } else {
129139 if (!Date::ParseDoubleDigit(buf, len, pos, min)) {
129140 return false;
129141 }
129142 if (min < 0 || min >= 60) {
129143 return false;
129144 }
129145 }
129146
129147 if (pos > len) {
129148 return false;
129149 }
129150 if (pos == len && (!strict || sep_pos + 2 == pos)) {
129151 sec = 0;
129152 } else {
129153 if (buf[pos++] != sep) {
129154 return false;
129155 }
129156
129157 if (pos == len && !strict) {
129158 sec = 0;
129159 } else {
129160 if (!Date::ParseDoubleDigit(buf, len, pos, sec)) {
129161 return false;
129162 }
129163 if (sec < 0 || sec >= 60) {
129164 return false;
129165 }
129166 }
129167 }
129168
129169 micros = 0;
129170 if (pos < len && buf[pos] == '.') {
129171 pos++;
129172 // we expect some microseconds
129173 int32_t mult = 100000;
129174 if (nanos) {
129175 // do we expect nanoseconds?
129176 mult *= Interval::NANOS_PER_MICRO;
129177 }
129178 for (; pos < len && StringUtil::CharacterIsDigit(buf[pos]); pos++, mult /= 10) {
129179 if (mult > 0) {
129180 micros += (buf[pos] - '0') * mult;
129181 }
129182 }
129183 if (nanos) {
129184 *nanos = UnsafeNumericCast<int32_t>(micros % Interval::NANOS_PER_MICRO);
129185 micros /= Interval::NANOS_PER_MICRO;
129186 }
129187 }
129188
129189 // in strict mode, check remaining string for non-space characters
129190 if (strict) {
129191 // skip trailing spaces
129192 while (pos < len && StringUtil::CharacterIsSpace(buf[pos])) {
129193 pos++;
129194 }
129195 // check position. if end was not reached, non-space chars remaining
129196 if (pos < len) {
129197 return false;
129198 }
129199 }
129200
129201 result = Time::FromTime(hour, min, sec, micros);
129202 return true;
129203}
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

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