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