Kùzu C++ API
Loading...
Searching...
No Matches
date_t.h
Go to the documentation of this file.
1#pragma once
2
3#include "interval_t.h"
4
5namespace kuzu {
6namespace common {
7
8struct timestamp_t;
9
10// System representation of dates as the number of days since 1970-01-01.
12 int32_t days;
13
15 explicit date_t(int32_t days_p);
16
17 // Comparison operators with date_t.
18 bool operator==(const date_t& rhs) const;
19 bool operator!=(const date_t& rhs) const;
20 bool operator<=(const date_t& rhs) const;
21 bool operator<(const date_t& rhs) const;
22 bool operator>(const date_t& rhs) const;
23 bool operator>=(const date_t& rhs) const;
24
25 // Comparison operators with timestamp_t.
26 bool operator==(const timestamp_t& rhs) const;
27 bool operator!=(const timestamp_t& rhs) const;
28 bool operator<(const timestamp_t& rhs) const;
29 bool operator<=(const timestamp_t& rhs) const;
30 bool operator>(const timestamp_t& rhs) const;
31 bool operator>=(const timestamp_t& rhs) const;
32
33 // arithmetic operators
34 date_t operator+(const int32_t& day) const;
35 date_t operator-(const int32_t& day) const;
36
37 date_t operator+(const interval_t& interval) const;
38 date_t operator-(const interval_t& interval) const;
39
40 int64_t operator-(const date_t& rhs) const;
41};
42
43inline date_t operator+(int64_t i, const date_t date) {
44 return date + i;
45}
46
47// Note: Aside from some minor changes, this implementation is copied from DuckDB's source code:
48// https://github.com/duckdb/duckdb/blob/master/src/include/duckdb/common/types/date.hpp.
49// https://github.com/duckdb/duckdb/blob/master/src/common/types/date.cpp.
50// For example, instead of using their idx_t type to refer to indices, we directly use uint64_t,
51// which is the actual type of idx_t (so we say uint64_t len instead of idx_t len). When more
52// functionality is needed, we should first consult these DuckDB links.
53class Date {
54public:
55 KUZU_API static const int32_t NORMAL_DAYS[13];
56 KUZU_API static const int32_t CUMULATIVE_DAYS[13];
57 KUZU_API static const int32_t LEAP_DAYS[13];
58 KUZU_API static const int32_t CUMULATIVE_LEAP_DAYS[13];
59 KUZU_API static const int32_t CUMULATIVE_YEAR_DAYS[401];
60 KUZU_API static const int8_t MONTH_PER_DAY_OF_YEAR[365];
61 KUZU_API static const int8_t LEAP_MONTH_PER_DAY_OF_YEAR[366];
62
63 KUZU_API constexpr static const int32_t MIN_YEAR = -290307;
64 KUZU_API constexpr static const int32_t MAX_YEAR = 294247;
65 KUZU_API constexpr static const int32_t EPOCH_YEAR = 1970;
66
67 KUZU_API constexpr static const int32_t YEAR_INTERVAL = 400;
68 KUZU_API constexpr static const int32_t DAYS_PER_YEAR_INTERVAL = 146097;
69 constexpr static const char* BC_SUFFIX = " (BC)";
70
71 // Convert a string in the format "YYYY-MM-DD" to a date object
72 KUZU_API static date_t fromCString(const char* str, uint64_t len);
73 // Convert a date object to a string in the format "YYYY-MM-DD"
74 KUZU_API static std::string toString(date_t date);
75 // Try to convert text in a buffer to a date; returns true if parsing was successful
76 KUZU_API static bool tryConvertDate(const char* buf, uint64_t len, uint64_t& pos,
77 date_t& result);
78
79 // private:
80 // Returns true if (year) is a leap year, and false otherwise
81 KUZU_API static bool isLeapYear(int32_t year);
82 // Returns true if the specified (year, month, day) combination is a valid
83 // date
84 KUZU_API static bool isValid(int32_t year, int32_t month, int32_t day);
85 // Extract the year, month and day from a given date object
86 KUZU_API static void convert(date_t date, int32_t& out_year, int32_t& out_month,
87 int32_t& out_day);
88 // Create a Date object from a specified (year, month, day) combination
89 KUZU_API static date_t fromDate(int32_t year, int32_t month, int32_t day);
90
91 // Helper function to parse two digits from a string (e.g. "30" -> 30, "03" -> 3, "3" -> 3)
92 KUZU_API static bool parseDoubleDigit(const char* buf, uint64_t len, uint64_t& pos,
93 int32_t& result);
94
95 KUZU_API static int32_t monthDays(int32_t year, int32_t month);
96
97 KUZU_API static std::string getDayName(date_t& date);
98
99 KUZU_API static std::string getMonthName(date_t& date);
100
102
103 KUZU_API static int32_t getDatePart(DatePartSpecifier specifier, date_t& date);
104
105 KUZU_API static date_t trunc(DatePartSpecifier specifier, date_t& date);
106
107 KUZU_API static int64_t getEpochNanoSeconds(const date_t& date);
108
109private:
110 static void extractYearOffset(int32_t& n, int32_t& year, int32_t& year_offset);
111};
112
113} // namespace common
114} // namespace kuzu
#define KUZU_API
Definition api.h:25
Definition date_t.h:53
static KUZU_API int32_t monthDays(int32_t year, int32_t month)
static KUZU_API const int8_t MONTH_PER_DAY_OF_YEAR[365]
Definition date_t.h:60
static KUZU_API bool parseDoubleDigit(const char *buf, uint64_t len, uint64_t &pos, int32_t &result)
static KUZU_API const int32_t LEAP_DAYS[13]
Definition date_t.h:57
static KUZU_API std::string toString(date_t date)
static KUZU_API date_t trunc(DatePartSpecifier specifier, date_t &date)
static KUZU_API const int8_t LEAP_MONTH_PER_DAY_OF_YEAR[366]
Definition date_t.h:61
static KUZU_API bool isLeapYear(int32_t year)
KUZU_API static constexpr const int32_t YEAR_INTERVAL
Definition date_t.h:67
static KUZU_API std::string getMonthName(date_t &date)
static KUZU_API int64_t getEpochNanoSeconds(const date_t &date)
static KUZU_API bool isValid(int32_t year, int32_t month, int32_t day)
KUZU_API static constexpr const int32_t EPOCH_YEAR
Definition date_t.h:65
static KUZU_API void convert(date_t date, int32_t &out_year, int32_t &out_month, int32_t &out_day)
static KUZU_API const int32_t CUMULATIVE_YEAR_DAYS[401]
Definition date_t.h:59
static KUZU_API const int32_t CUMULATIVE_DAYS[13]
Definition date_t.h:56
static KUZU_API int32_t getDatePart(DatePartSpecifier specifier, date_t &date)
static KUZU_API const int32_t CUMULATIVE_LEAP_DAYS[13]
Definition date_t.h:58
static KUZU_API date_t fromCString(const char *str, uint64_t len)
static KUZU_API bool tryConvertDate(const char *buf, uint64_t len, uint64_t &pos, date_t &result)
static KUZU_API date_t getLastDay(date_t &date)
KUZU_API static constexpr const int32_t MAX_YEAR
Definition date_t.h:64
KUZU_API static constexpr const int32_t DAYS_PER_YEAR_INTERVAL
Definition date_t.h:68
static constexpr const char * BC_SUFFIX
Definition date_t.h:69
static KUZU_API const int32_t NORMAL_DAYS[13]
Definition date_t.h:55
static KUZU_API std::string getDayName(date_t &date)
KUZU_API static constexpr const int32_t MIN_YEAR
Definition date_t.h:63
static KUZU_API date_t fromDate(int32_t year, int32_t month, int32_t day)
date_t operator+(int64_t i, const date_t date)
Definition date_t.h:43
enum KUZU_API DatePartSpecifier
Definition interval_t.h:14
Definition alter_type.h:5
Definition date_t.h:11
bool operator>(const date_t &rhs) const
bool operator<(const date_t &rhs) const
bool operator==(const date_t &rhs) const
bool operator>(const timestamp_t &rhs) const
date_t operator-(const interval_t &interval) const
bool operator>=(const timestamp_t &rhs) const
bool operator==(const timestamp_t &rhs) const
bool operator>=(const date_t &rhs) const
date_t operator+(const interval_t &interval) const
date_t operator-(const int32_t &day) const
date_t(int32_t days_p)
bool operator<=(const timestamp_t &rhs) const
date_t operator+(const int32_t &day) const
bool operator<(const timestamp_t &rhs) const
bool operator!=(const timestamp_t &rhs) const
int32_t days
Definition date_t.h:12
int64_t operator-(const date_t &rhs) const
bool operator!=(const date_t &rhs) const
bool operator<=(const date_t &rhs) const
Definition interval_t.h:30
Definition timestamp_t.h:10