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 {
9namespace common {
10
11struct timestamp_t;
12struct date_t;
13
14enum class KUZU_API DatePartSpecifier : uint8_t {
15 YEAR,
28};
29
31 int32_t months = 0;
32 int32_t days = 0;
33 int64_t micros = 0;
34
36 interval_t(int32_t months_p, int32_t days_p, int64_t micros_p);
37
38 // comparator operators
39 bool operator==(const interval_t& rhs) const;
40 bool operator!=(const interval_t& rhs) const;
41
42 bool operator>(const interval_t& rhs) const;
43 bool operator<=(const interval_t& rhs) const;
44 bool operator<(const interval_t& rhs) const;
45 bool operator>=(const interval_t& rhs) const;
46
47 // arithmetic operators
48 interval_t operator+(const interval_t& rhs) const;
50 date_t operator+(const date_t& rhs) const;
51 interval_t operator-(const interval_t& rhs) const;
52
53 interval_t operator/(const uint64_t& rhs) const;
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/interval.hpp.
58// https://github.com/duckdb/duckdb/blob/master/src/common/types/interval.cpp.
59// When more functionality is needed, we should first consult these DuckDB links.
60// The Interval class is a static class that holds helper functions for the Interval type.
61class Interval {
62public:
63 static constexpr const int32_t MONTHS_PER_MILLENIUM = 12000;
64 static constexpr const int32_t MONTHS_PER_CENTURY = 1200;
65 static constexpr const int32_t MONTHS_PER_DECADE = 120;
66 static constexpr const int32_t MONTHS_PER_YEAR = 12;
67 static constexpr const int32_t MONTHS_PER_QUARTER = 3;
68 static constexpr const int32_t DAYS_PER_WEEK = 7;
70 static constexpr const int64_t DAYS_PER_MONTH = 30;
71 static constexpr const int64_t DAYS_PER_YEAR = 365;
72 static constexpr const int64_t MSECS_PER_SEC = 1000;
73 static constexpr const int32_t SECS_PER_MINUTE = 60;
74 static constexpr const int32_t MINS_PER_HOUR = 60;
75 static constexpr const int32_t HOURS_PER_DAY = 24;
76 static constexpr const int32_t SECS_PER_HOUR = SECS_PER_MINUTE * MINS_PER_HOUR;
77 static constexpr const int32_t SECS_PER_DAY = SECS_PER_HOUR * HOURS_PER_DAY;
78 static constexpr const int32_t SECS_PER_WEEK = SECS_PER_DAY * DAYS_PER_WEEK;
79
80 static constexpr const int64_t MICROS_PER_MSEC = 1000;
81 static constexpr const int64_t MICROS_PER_SEC = MICROS_PER_MSEC * MSECS_PER_SEC;
82 static constexpr const int64_t MICROS_PER_MINUTE = MICROS_PER_SEC * SECS_PER_MINUTE;
83 static constexpr const int64_t MICROS_PER_HOUR = MICROS_PER_MINUTE * MINS_PER_HOUR;
84 static constexpr const int64_t MICROS_PER_DAY = MICROS_PER_HOUR * HOURS_PER_DAY;
85 static constexpr const int64_t MICROS_PER_WEEK = MICROS_PER_DAY * DAYS_PER_WEEK;
86 static constexpr const int64_t MICROS_PER_MONTH = MICROS_PER_DAY * DAYS_PER_MONTH;
87
88 static constexpr const int64_t NANOS_PER_MICRO = 1000;
89 static constexpr const int64_t NANOS_PER_MSEC = NANOS_PER_MICRO * MICROS_PER_MSEC;
90 static constexpr const int64_t NANOS_PER_SEC = NANOS_PER_MSEC * MSECS_PER_SEC;
91 static constexpr const int64_t NANOS_PER_MINUTE = NANOS_PER_SEC * SECS_PER_MINUTE;
92 static constexpr const int64_t NANOS_PER_HOUR = NANOS_PER_MINUTE * MINS_PER_HOUR;
93 static constexpr const int64_t NANOS_PER_DAY = NANOS_PER_HOUR * HOURS_PER_DAY;
94 static constexpr const int64_t NANOS_PER_WEEK = NANOS_PER_DAY * DAYS_PER_WEEK;
95
96 KUZU_API static void addition(interval_t& result, uint64_t number, std::string specifierStr);
97 KUZU_API static interval_t fromCString(const char* str, uint64_t len);
98 KUZU_API static std::string toString(interval_t interval);
99 KUZU_API static bool greaterThan(const interval_t& left, const interval_t& right);
100 KUZU_API static void normalizeIntervalEntries(interval_t input, int64_t& months, int64_t& days,
101 int64_t& micros);
102 KUZU_API static void tryGetDatePartSpecifier(std::string specifier, DatePartSpecifier& result);
103 KUZU_API static int32_t getIntervalPart(DatePartSpecifier specifier, interval_t& timestamp);
104 KUZU_API static int64_t getMicro(const interval_t& val);
105 KUZU_API static int64_t getNanoseconds(const interval_t& val);
106};
107
108} // namespace common
109} // namespace kuzu
#define KUZU_API
Definition api.h:25
Definition interval_t.h:61
static constexpr const int32_t MONTHS_PER_CENTURY
Definition interval_t.h:64
static constexpr const int32_t SECS_PER_HOUR
Definition interval_t.h:76
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:81
static constexpr const int32_t DAYS_PER_WEEK
Definition interval_t.h:68
static constexpr const int64_t MICROS_PER_DAY
Definition interval_t.h:84
static constexpr const int32_t MINS_PER_HOUR
Definition interval_t.h:74
static KUZU_API int64_t getMicro(const interval_t &val)
static constexpr const int64_t MICROS_PER_MINUTE
Definition interval_t.h:82
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:70
static KUZU_API int64_t getNanoseconds(const interval_t &val)
static constexpr const int64_t NANOS_PER_WEEK
Definition interval_t.h:94
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:78
static constexpr const int64_t MICROS_PER_MONTH
Definition interval_t.h:86
static constexpr const int64_t MICROS_PER_WEEK
Definition interval_t.h:85
static constexpr const int32_t SECS_PER_MINUTE
Definition interval_t.h:73
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:83
static constexpr const int32_t MONTHS_PER_YEAR
Definition interval_t.h:66
static constexpr const int64_t NANOS_PER_MSEC
Definition interval_t.h:89
static constexpr const int32_t MONTHS_PER_MILLENIUM
Definition interval_t.h:63
static constexpr const int64_t MSECS_PER_SEC
Definition interval_t.h:72
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:65
static constexpr const int64_t NANOS_PER_SEC
Definition interval_t.h:90
static constexpr const int32_t SECS_PER_DAY
Definition interval_t.h:77
static constexpr const int64_t NANOS_PER_DAY
Definition interval_t.h:93
static constexpr const int64_t NANOS_PER_HOUR
Definition interval_t.h:92
static constexpr const int64_t MICROS_PER_MSEC
Definition interval_t.h:80
static constexpr const int64_t DAYS_PER_YEAR
Definition interval_t.h:71
static constexpr const int32_t MONTHS_PER_QUARTER
Definition interval_t.h:67
static constexpr const int64_t NANOS_PER_MICRO
Definition interval_t.h:88
static constexpr const int64_t NANOS_PER_MINUTE
Definition interval_t.h:91
static constexpr const int32_t HOURS_PER_DAY
Definition interval_t.h:75
enum KUZU_API DECADE
Definition interval_t.h:18
enum KUZU_API DAY
Definition interval_t.h:17
enum KUZU_API DatePartSpecifier
Definition interval_t.h:14
enum KUZU_API MILLENNIUM
Definition interval_t.h:20
enum KUZU_API MILLISECOND
Definition interval_t.h:23
enum KUZU_API MONTH
Definition interval_t.h:16
enum KUZU_API MICROSECOND
Definition interval_t.h:22
enum KUZU_API QUARTER
Definition interval_t.h:21
enum KUZU_API MINUTE
Definition interval_t.h:25
enum KUZU_API WEEK
Definition interval_t.h:27
enum KUZU_API CENTURY
Definition interval_t.h:19
enum KUZU_API HOUR
Definition interval_t.h:26
enum KUZU_API SECOND
Definition interval_t.h:24
Definition alter_type.h:5
Definition date_t.h:11
Definition interval_t.h:30
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