Kùzu C++ API
Loading...
Searching...
No Matches
timestamp_t.h
Go to the documentation of this file.
1#pragma once
2
3#include "date_t.h"
4#include "dtime_t.h"
5
6namespace kuzu {
7namespace common {
8
9// Type used to represent timestamps (value is in microseconds since 1970-01-01)
11 int64_t value = 0;
12
14 explicit timestamp_t(int64_t value_p);
15 timestamp_t& operator=(int64_t value_p);
16
17 // explicit conversion
18 explicit operator int64_t() const;
19
20 // Comparison operators with timestamp_t.
21 bool operator==(const timestamp_t& rhs) const;
22 bool operator!=(const timestamp_t& rhs) const;
23 bool operator<=(const timestamp_t& rhs) const;
24 bool operator<(const timestamp_t& rhs) const;
25 bool operator>(const timestamp_t& rhs) const;
26 bool operator>=(const timestamp_t& rhs) const;
27
28 // Comparison operators with date_t.
29 bool operator==(const date_t& rhs) const;
30 bool operator!=(const date_t& rhs) const;
31 bool operator<(const date_t& rhs) const;
32 bool operator<=(const date_t& rhs) const;
33 bool operator>(const date_t& rhs) const;
34 bool operator>=(const date_t& rhs) const;
35
36 // arithmetic operator
37 timestamp_t operator+(const interval_t& interval) const;
38 timestamp_t operator-(const interval_t& interval) const;
39
40 interval_t operator-(const timestamp_t& rhs) const;
41};
42
43struct timestamp_tz_t : public timestamp_t { // NO LINT
45};
46struct timestamp_ns_t : public timestamp_t { // NO LINT
48};
49struct timestamp_ms_t : public timestamp_t { // NO LINT
51};
52struct timestamp_sec_t : public timestamp_t { // NO LINT
54};
55
56// Note: Aside from some minor changes, this implementation is copied from DuckDB's source code:
57// https://github.com/duckdb/duckdb/blob/master/src/include/duckdb/common/types/timestamp.hpp.
58// https://github.com/duckdb/duckdb/blob/master/src/common/types/timestamp.cpp.
59// For example, instead of using their idx_t type to refer to indices, we directly use uint64_t,
60// which is the actual type of idx_t (so we say uint64_t len instead of idx_t len). When more
61// functionality is needed, we should first consult these DuckDB links.
62
63// The Timestamp class is a static class that holds helper functions for the Timestamp type.
64// timestamp/datetime uses 64 bits, high 32 bits for date and low 32 bits for time
65class Timestamp {
66public:
67 KUZU_API static timestamp_t fromCString(const char* str, uint64_t len);
68
69 // Convert a timestamp object to a std::string in the format "YYYY-MM-DD hh:mm:ss".
70 KUZU_API static std::string toString(timestamp_t timestamp);
71
73
75
76 // Create a Timestamp object from a specified (date, time) combination.
78
79 KUZU_API static bool tryConvertTimestamp(const char* str, uint64_t len, timestamp_t& result);
80
81 // Extract the date and time from a given timestamp object.
82 KUZU_API static void convert(timestamp_t timestamp, date_t& out_date, dtime_t& out_time);
83
84 // Create a Timestamp object from the specified epochMs.
86
87 // Create a Timestamp object from the specified epochMs.
89
90 // Create a Timestamp object from the specified epochSec.
92
93 // Create a Timestamp object from the specified epochNs.
95
96 KUZU_API static int32_t getTimestampPart(DatePartSpecifier specifier, timestamp_t& timestamp);
97
99
100 KUZU_API static int64_t getEpochNanoSeconds(const timestamp_t& timestamp);
101
102 KUZU_API static int64_t getEpochMilliSeconds(const timestamp_t& timestamp);
103
104 KUZU_API static int64_t getEpochSeconds(const timestamp_t& timestamp);
105
106 KUZU_API static bool tryParseUTCOffset(const char* str, uint64_t& pos, uint64_t len,
107 int& hour_offset, int& minute_offset);
108
109 static std::string getTimestampConversionExceptionMsg(const char* str, uint64_t len,
110 const std::string& typeID = "TIMESTAMP") {
111 return "Error occurred during parsing " + typeID + ". Given: \"" + std::string(str, len) +
112 "\". Expected format: (YYYY-MM-DD hh:mm:ss[.zzzzzz][+-TT[:tt]])";
113 }
114
116};
117
118} // namespace common
119} // namespace kuzu
#define KUZU_API
Definition api.h:25
Definition timestamp_t.h:65
static KUZU_API timestamp_t fromEpochMicroSeconds(int64_t epochMs)
static KUZU_API bool tryParseUTCOffset(const char *str, uint64_t &pos, uint64_t len, int &hour_offset, int &minute_offset)
static KUZU_API timestamp_t fromEpochSeconds(int64_t sec)
static KUZU_API timestamp_t trunc(DatePartSpecifier specifier, timestamp_t &date)
static std::string getTimestampConversionExceptionMsg(const char *str, uint64_t len, const std::string &typeID="TIMESTAMP")
Definition timestamp_t.h:109
static KUZU_API dtime_t getTime(timestamp_t timestamp)
static KUZU_API int64_t getEpochMilliSeconds(const timestamp_t &timestamp)
static KUZU_API int64_t getEpochNanoSeconds(const timestamp_t &timestamp)
static KUZU_API int64_t getEpochSeconds(const timestamp_t &timestamp)
static KUZU_API timestamp_t fromDateTime(date_t date, dtime_t time)
static KUZU_API int32_t getTimestampPart(DatePartSpecifier specifier, timestamp_t &timestamp)
static KUZU_API void convert(timestamp_t timestamp, date_t &out_date, dtime_t &out_time)
static KUZU_API timestamp_t fromCString(const char *str, uint64_t len)
static KUZU_API bool tryConvertTimestamp(const char *str, uint64_t len, timestamp_t &result)
static KUZU_API date_t getDate(timestamp_t timestamp)
static KUZU_API timestamp_t getCurrentTimestamp()
static KUZU_API timestamp_t fromEpochMilliSeconds(int64_t ms)
static KUZU_API timestamp_t fromEpochNanoSeconds(int64_t ns)
static KUZU_API std::string toString(timestamp_t timestamp)
enum KUZU_API DatePartSpecifier
Definition interval_t.h:19
Definition array_utils.h:7
Definition date_t.h:16
Definition dtime_t.h:12
Definition interval_t.h:35
Definition timestamp_t.h:49
Definition timestamp_t.h:46
Definition timestamp_t.h:52
Definition timestamp_t.h:10
bool operator<(const timestamp_t &rhs) const
timestamp_t operator-(const interval_t &interval) const
interval_t operator-(const timestamp_t &rhs) const
bool operator==(const timestamp_t &rhs) const
timestamp_t(int64_t value_p)
bool operator>(const timestamp_t &rhs) const
bool operator<=(const date_t &rhs) const
timestamp_t & operator=(int64_t value_p)
bool operator>=(const timestamp_t &rhs) const
bool operator!=(const timestamp_t &rhs) const
bool operator!=(const date_t &rhs) const
bool operator<(const date_t &rhs) const
timestamp_t operator+(const interval_t &interval) const
bool operator<=(const timestamp_t &rhs) const
bool operator>(const date_t &rhs) const
bool operator>=(const date_t &rhs) const
bool operator==(const date_t &rhs) const
Definition timestamp_t.h:43