1 | #include "duckdb/main/capi/capi_internal.hpp" |
2 | #include "duckdb/common/types/date.hpp" |
3 | #include "duckdb/common/types/time.hpp" |
4 | #include "duckdb/common/types/timestamp.hpp" |
5 | |
6 | using duckdb::Date; |
7 | using duckdb::Time; |
8 | using duckdb::Timestamp; |
9 | |
10 | using duckdb::date_t; |
11 | using duckdb::dtime_t; |
12 | using duckdb::timestamp_t; |
13 | |
14 | duckdb_date_struct duckdb_from_date(duckdb_date date) { |
15 | int32_t year, month, day; |
16 | Date::Convert(date: date_t(date.days), out_year&: year, out_month&: month, out_day&: day); |
17 | |
18 | duckdb_date_struct result; |
19 | result.year = year; |
20 | result.month = month; |
21 | result.day = day; |
22 | return result; |
23 | } |
24 | |
25 | duckdb_date duckdb_to_date(duckdb_date_struct date) { |
26 | duckdb_date result; |
27 | result.days = Date::FromDate(year: date.year, month: date.month, day: date.day).days; |
28 | return result; |
29 | } |
30 | |
31 | duckdb_time_struct duckdb_from_time(duckdb_time time) { |
32 | int32_t hour, minute, second, micros; |
33 | Time::Convert(time: dtime_t(time.micros), out_hour&: hour, out_min&: minute, out_sec&: second, out_micros&: micros); |
34 | |
35 | duckdb_time_struct result; |
36 | result.hour = hour; |
37 | result.min = minute; |
38 | result.sec = second; |
39 | result.micros = micros; |
40 | return result; |
41 | } |
42 | |
43 | duckdb_time duckdb_to_time(duckdb_time_struct time) { |
44 | duckdb_time result; |
45 | result.micros = Time::FromTime(hour: time.hour, minute: time.min, second: time.sec, microseconds: time.micros).micros; |
46 | return result; |
47 | } |
48 | |
49 | duckdb_timestamp_struct duckdb_from_timestamp(duckdb_timestamp ts) { |
50 | date_t date; |
51 | dtime_t time; |
52 | Timestamp::Convert(date: timestamp_t(ts.micros), out_date&: date, out_time&: time); |
53 | |
54 | duckdb_date ddate; |
55 | ddate.days = date.days; |
56 | |
57 | duckdb_time dtime; |
58 | dtime.micros = time.micros; |
59 | |
60 | duckdb_timestamp_struct result; |
61 | result.date = duckdb_from_date(date: ddate); |
62 | result.time = duckdb_from_time(time: dtime); |
63 | return result; |
64 | } |
65 | |
66 | duckdb_timestamp duckdb_to_timestamp(duckdb_timestamp_struct ts) { |
67 | date_t date = date_t(duckdb_to_date(date: ts.date).days); |
68 | dtime_t time = dtime_t(duckdb_to_time(time: ts.time).micros); |
69 | |
70 | duckdb_timestamp result; |
71 | result.micros = Timestamp::FromDatetime(date, time).value; |
72 | return result; |
73 | } |
74 | |