Kùzu C++ API
Loading...
Searching...
No Matches
interval_t.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5
6#include "api.h"
7
8namespace kuzu {
9
10namespace regex {
11class RE2;
12}
13
14namespace common {
15
16struct timestamp_t;
17struct date_t;
18
19enum class KUZU_API DatePartSpecifier : uint8_t {
20 YEAR,
33};
34
36 int32_t months = 0;
37 int32_t days = 0;
38 int64_t micros = 0;
39
41 interval_t(int32_t months_p, int32_t days_p, int64_t micros_p);
42
43 // comparator operators
44 bool operator==(const interval_t& rhs) const;
45 bool operator!=(const interval_t& rhs) const;
46
47 bool operator>(const interval_t& rhs) const;
48 bool operator<=(const interval_t& rhs) const;
49 bool operator<(const interval_t& rhs) const;
50 bool operator>=(const interval_t& rhs) const;
51
52 // arithmetic operators
53 interval_t operator+(const interval_t& rhs) const;
55 date_t operator+(const date_t& rhs) const;
56 interval_t operator-(const interval_t& rhs) const;
57
58 interval_t operator/(const uint64_t& rhs) const;
59};
60
61// Note: Aside from some minor changes, this implementation is copied from DuckDB's source code:
62// https://github.com/duckdb/duckdb/blob/master/src/include/duckdb/common/types/interval.hpp.
63// https://github.com/duckdb/duckdb/blob/master/src/common/types/interval.cpp.
64// When more functionality is needed, we should first consult these DuckDB links.
65// The Interval class is a static class that holds helper functions for the Interval type.
66class Interval {
67public:
68 static constexpr const int32_t MONTHS_PER_MILLENIUM = 12000;
69 static constexpr const int32_t MONTHS_PER_CENTURY = 1200;
70 static constexpr const int32_t MONTHS_PER_DECADE = 120;
71 static constexpr const int32_t MONTHS_PER_YEAR = 12;
72 static constexpr const int32_t MONTHS_PER_QUARTER = 3;
73 static constexpr const int32_t DAYS_PER_WEEK = 7;
75 static constexpr const int64_t DAYS_PER_MONTH = 30;
76 static constexpr const int64_t DAYS_PER_YEAR = 365;
77 static constexpr const int64_t MSECS_PER_SEC = 1000;
78 static constexpr const int32_t SECS_PER_MINUTE = 60;
79 static constexpr const int32_t MINS_PER_HOUR = 60;
80 static constexpr const int32_t HOURS_PER_DAY = 24;
81 static constexpr const int32_t SECS_PER_HOUR = SECS_PER_MINUTE * MINS_PER_HOUR;
82 static constexpr const int32_t SECS_PER_DAY = SECS_PER_HOUR * HOURS_PER_DAY;
83 static constexpr const int32_t SECS_PER_WEEK = SECS_PER_DAY * DAYS_PER_WEEK;
84
85 static constexpr const int64_t MICROS_PER_MSEC = 1000;
86 static constexpr const int64_t MICROS_PER_SEC = MICROS_PER_MSEC * MSECS_PER_SEC;
87 static constexpr const int64_t MICROS_PER_MINUTE = MICROS_PER_SEC * SECS_PER_MINUTE;
88 static constexpr const int64_t MICROS_PER_HOUR = MICROS_PER_MINUTE * MINS_PER_HOUR;
89 static constexpr const int64_t MICROS_PER_DAY = MICROS_PER_HOUR * HOURS_PER_DAY;
90 static constexpr const int64_t MICROS_PER_WEEK = MICROS_PER_DAY * DAYS_PER_WEEK;
91 static constexpr const int64_t MICROS_PER_MONTH = MICROS_PER_DAY * DAYS_PER_MONTH;
92
93 static constexpr const int64_t NANOS_PER_MICRO = 1000;
94 static constexpr const int64_t NANOS_PER_MSEC = NANOS_PER_MICRO * MICROS_PER_MSEC;
95 static constexpr const int64_t NANOS_PER_SEC = NANOS_PER_MSEC * MSECS_PER_SEC;
96 static constexpr const int64_t NANOS_PER_MINUTE = NANOS_PER_SEC * SECS_PER_MINUTE;
97 static constexpr const int64_t NANOS_PER_HOUR = NANOS_PER_MINUTE * MINS_PER_HOUR;
98 static constexpr const int64_t NANOS_PER_DAY = NANOS_PER_HOUR * HOURS_PER_DAY;
99 static constexpr const int64_t NANOS_PER_WEEK = NANOS_PER_DAY * DAYS_PER_WEEK;
100
101 KUZU_API static void addition(interval_t& result, uint64_t number, std::string specifierStr);
102 KUZU_API static interval_t fromCString(const char* str, uint64_t len);
103 KUZU_API static std::string toString(interval_t interval);
104 KUZU_API static bool greaterThan(const interval_t& left, const interval_t& right);
105 KUZU_API static void normalizeIntervalEntries(interval_t input, int64_t& months, int64_t& days,
106 int64_t& micros);
107 KUZU_API static void tryGetDatePartSpecifier(std::string specifier, DatePartSpecifier& result);
108 KUZU_API static int32_t getIntervalPart(DatePartSpecifier specifier, interval_t& timestamp);
109 KUZU_API static int64_t getMicro(const interval_t& val);
110 KUZU_API static int64_t getNanoseconds(const interval_t& val);
111 KUZU_API static const regex::RE2& regexPattern1();
112 KUZU_API static const regex::RE2& regexPattern2();
113};
114
115} // namespace common
116} // namespace kuzu
#define KUZU_API
Definition api.h:25
Definition interval_t.h:66
static constexpr const int32_t MONTHS_PER_CENTURY
Definition interval_t.h:69
static constexpr const int32_t SECS_PER_HOUR
Definition interval_t.h:81
static KUZU_API std::string toString(interval_t interval)
static KUZU_API int32_t getIntervalPart(DatePartSpecifier specifier, interval_t &timestamp)
static constexpr const int64_t MICROS_PER_SEC
Definition interval_t.h:86
static constexpr const int32_t DAYS_PER_WEEK
Definition interval_t.h:73
static constexpr const int64_t MICROS_PER_DAY
Definition interval_t.h:89
static constexpr const int32_t MINS_PER_HOUR
Definition interval_t.h:79
static KUZU_API int64_t getMicro(const interval_t &val)
static constexpr const int64_t MICROS_PER_MINUTE
Definition interval_t.h:87
static constexpr const int64_t DAYS_PER_MONTH
only used for interval comparison/ordering purposes, in which case a month counts as 30 days
Definition interval_t.h:75
static KUZU_API int64_t getNanoseconds(const interval_t &val)
static constexpr const int64_t NANOS_PER_WEEK
Definition interval_t.h:99
static KUZU_API interval_t fromCString(const char *str, uint64_t len)
static KUZU_API void normalizeIntervalEntries(interval_t input, int64_t &months, int64_t &days, int64_t &micros)
static constexpr const int32_t SECS_PER_WEEK
Definition interval_t.h:83
static KUZU_API const regex::RE2 & regexPattern1()
static constexpr const int64_t MICROS_PER_MONTH
Definition interval_t.h:91
static constexpr const int64_t MICROS_PER_WEEK
Definition interval_t.h:90
static constexpr const int32_t SECS_PER_MINUTE
Definition interval_t.h:78
static KUZU_API void tryGetDatePartSpecifier(std::string specifier, DatePartSpecifier &result)
static KUZU_API void addition(interval_t &result, uint64_t number, std::string specifierStr)
static constexpr const int64_t MICROS_PER_HOUR
Definition interval_t.h:88
static constexpr const int32_t MONTHS_PER_YEAR
Definition interval_t.h:71
static constexpr const int64_t NANOS_PER_MSEC
Definition interval_t.h:94
static constexpr const int32_t MONTHS_PER_MILLENIUM
Definition interval_t.h:68
static constexpr const int64_t MSECS_PER_SEC
Definition interval_t.h:77
static KUZU_API bool greaterThan(const interval_t &left, const interval_t &right)
static constexpr const int32_t MONTHS_PER_DECADE
Definition interval_t.h:70
static constexpr const int64_t NANOS_PER_SEC
Definition interval_t.h:95
static constexpr const int32_t SECS_PER_DAY
Definition interval_t.h:82
static constexpr const int64_t NANOS_PER_DAY
Definition interval_t.h:98
static constexpr const int64_t NANOS_PER_HOUR
Definition interval_t.h:97
static constexpr const int64_t MICROS_PER_MSEC
Definition interval_t.h:85
static constexpr const int64_t DAYS_PER_YEAR
Definition interval_t.h:76
static KUZU_API const regex::RE2 & regexPattern2()
static constexpr const int32_t MONTHS_PER_QUARTER
Definition interval_t.h:72
static constexpr const int64_t NANOS_PER_MICRO
Definition interval_t.h:93
static constexpr const int64_t NANOS_PER_MINUTE
Definition interval_t.h:96
static constexpr const int32_t HOURS_PER_DAY
Definition interval_t.h:80
enum KUZU_API DECADE
Definition interval_t.h:23
enum KUZU_API DAY
Definition interval_t.h:22
enum KUZU_API DatePartSpecifier
Definition interval_t.h:19
enum KUZU_API MILLENNIUM
Definition interval_t.h:25
enum KUZU_API MILLISECOND
Definition interval_t.h:28
enum KUZU_API MONTH
Definition interval_t.h:21
enum KUZU_API MICROSECOND
Definition interval_t.h:27
enum KUZU_API QUARTER
Definition interval_t.h:26
enum KUZU_API MINUTE
Definition interval_t.h:30
enum KUZU_API WEEK
Definition interval_t.h:32
enum KUZU_API CENTURY
Definition interval_t.h:24
enum KUZU_API HOUR
Definition interval_t.h:31
enum KUZU_API SECOND
Definition interval_t.h:29
Definition array_utils.h:7
Definition date_t.h:16
Definition interval_t.h:35
bool operator<(const interval_t &rhs) const
interval_t operator-(const interval_t &rhs) const
timestamp_t operator+(const timestamp_t &rhs) const
bool operator>(const interval_t &rhs) const
bool operator==(const interval_t &rhs) const
interval_t(int32_t months_p, int32_t days_p, int64_t micros_p)
bool operator!=(const interval_t &rhs) const
date_t operator+(const date_t &rhs) const
interval_t operator/(const uint64_t &rhs) const
interval_t operator+(const interval_t &rhs) const
bool operator<=(const interval_t &rhs) const
bool operator>=(const interval_t &rhs) const
Definition timestamp_t.h:10