5#include "../SqlColumnTypeDefinitions.hpp"
26 using native_type = std::chrono::system_clock::time_point;
27 using duration_type = std::chrono::system_clock::duration;
32 return SqlDateTime { std::chrono::system_clock::now() };
35#if !defined(LIGHTWEIGHT_CXX26_REFLECTION)
39 return SqlDateTime { std::chrono::system_clock::time_point { std::chrono::utc_clock::now().time_since_epoch() } };
50 constexpr std::weak_ordering operator<=>(
SqlDateTime const& other) const noexcept
54 constexpr bool operator==(SqlDateTime
const& other)
const noexcept
56 return (*this <=> other) == std::weak_ordering::equivalent;
59 constexpr bool operator!=(SqlDateTime
const& other)
const noexcept
61 return !(*
this == other);
65 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDateTime(std::chrono::year_month_day ymd,
66 std::chrono::hh_mm_ss<duration_type> time)
noexcept:
68 .year = (SQLSMALLINT) (
int) ymd.year(),
69 .month = (SQLUSMALLINT) (
unsigned) ymd.month(),
70 .day = (SQLUSMALLINT) (
unsigned) ymd.day(),
71 .hour = (SQLUSMALLINT) time.hours().count(),
72 .minute = (SQLUSMALLINT) time.minutes().count(),
73 .second = (SQLUSMALLINT) time.seconds().count(),
75 (SQLUINTEGER) (std::chrono::duration_cast<std::chrono::nanoseconds>(time.to_duration()).count() / 100) * 100,
82 std::chrono::year
year,
83 std::chrono::month
month,
85 std::chrono::hours
hour,
86 std::chrono::minutes
minute,
87 std::chrono::seconds
second,
88 std::chrono::nanoseconds
nanosecond = std::chrono::nanoseconds(0)) noexcept:
90 .year = (SQLSMALLINT) (
int)
year,
91 .month = (SQLUSMALLINT) (
unsigned)
month,
92 .day = (SQLUSMALLINT) (
unsigned)
day,
93 .hour = (SQLUSMALLINT)
hour.count(),
94 .minute = (SQLUSMALLINT)
minute.count(),
95 .second = (SQLUSMALLINT)
second.count(),
96 .fraction = (SQLUINTEGER) (
nanosecond.count() / 100) * 100,
102 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDateTime(std::chrono::system_clock::time_point
value)
noexcept:
103 sqlValue { ConvertToSqlValue(
value) }
110 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::year
year() const noexcept
112 return std::chrono::year(
static_cast<int>(sqlValue.year));
116 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::month
month() const noexcept
118 return std::chrono::month(
static_cast<unsigned>(sqlValue.month));
122 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::day
day() const noexcept
124 return std::chrono::day(
static_cast<unsigned>(sqlValue.day));
128 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::hours
hour() const noexcept
130 return std::chrono::hours(
static_cast<unsigned>(sqlValue.hour));
134 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::minutes
minute() const noexcept
136 return std::chrono::minutes(
static_cast<unsigned>(sqlValue.minute));
140 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::seconds
second() const noexcept
142 return std::chrono::seconds(
static_cast<unsigned>(sqlValue.second));
146 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::nanoseconds
nanosecond() const noexcept
148 return std::chrono::nanoseconds(
static_cast<unsigned>(sqlValue.fraction));
153 LIGHTWEIGHT_FORCE_INLINE
constexpr operator native_type() const noexcept
159 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
SqlDate Date() const noexcept
165 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
SqlTime Time() const noexcept
168 std::chrono::minutes {
minute() },
169 std::chrono::seconds {
second() },
170 std::chrono::microseconds { std::chrono::duration_cast<std::chrono::microseconds>(
nanosecond()) } };
173 static LIGHTWEIGHT_FORCE_INLINE SQL_TIMESTAMP_STRUCT
constexpr ConvertToSqlValue(native_type
value)
noexcept
175 using namespace std::chrono;
176 auto const totalDays = floor<days>(
value);
177 auto const ymd = year_month_day { totalDays };
179 hh_mm_ss<duration_type> { std::chrono::duration_cast<duration_type>(floor<nanoseconds>(
value - totalDays)) };
180 return ConvertToSqlValue(ymd, hms);
183 static LIGHTWEIGHT_FORCE_INLINE SQL_TIMESTAMP_STRUCT
constexpr ConvertToSqlValue(
184 std::chrono::year_month_day ymd, std::chrono::hh_mm_ss<duration_type> hms)
noexcept
188 return SQL_TIMESTAMP_STRUCT {
189 .year = (SQLSMALLINT) (
int) ymd.year(),
190 .month = (SQLUSMALLINT) (
unsigned) ymd.month(),
191 .day = (SQLUSMALLINT) (
unsigned) ymd.day(),
192 .hour = (SQLUSMALLINT) hms.hours().count(),
193 .minute = (SQLUSMALLINT) hms.minutes().count(),
194 .second = (SQLUSMALLINT) hms.seconds().count(),
195 .fraction = (SQLUINTEGER) (((std::chrono::duration_cast<std::chrono::nanoseconds>(hms.to_duration()).count() % 1'000'000'000LLU) / 100) * 100)
200 static LIGHTWEIGHT_FORCE_INLINE native_type
constexpr ConvertToNative(SQL_TIMESTAMP_STRUCT
const& time)
noexcept
203 using namespace std::chrono;
204 auto const ymd = year_month_day { std::chrono::year { time.year } / std::chrono::month { time.month } / std::chrono::day { time.day } };
205 auto const hms = hh_mm_ss<duration_type> {
206 duration_cast<duration_type>(
208 + minutes { time.minute }
209 + seconds { time.second }
210 + nanoseconds { time.fraction }
213 return sys_days { ymd } + hms.to_duration();
218 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE native_type
value() const noexcept
220 return ConvertToNative(sqlValue);
223 LIGHTWEIGHT_FORCE_INLINE
SqlDateTime& operator+=(duration_type duration)
noexcept
229 LIGHTWEIGHT_FORCE_INLINE SqlDateTime& operator-=(duration_type duration)
noexcept
231 *
this = SqlDateTime {
value() - duration };
235 friend LIGHTWEIGHT_FORCE_INLINE SqlDateTime operator+(SqlDateTime dateTime, duration_type duration)
noexcept
237 return SqlDateTime { dateTime.value() + duration };
240 friend LIGHTWEIGHT_FORCE_INLINE SqlDateTime operator-(SqlDateTime dateTime, duration_type duration)
noexcept
242 return SqlDateTime { dateTime.value() - duration };
245 SQL_TIMESTAMP_STRUCT sqlValue {};
251struct std::formatter<Lightweight::SqlDateTime>: std::formatter<std::string>
254 -> std::format_context::iterator
260 auto const milliseconds = value.sqlValue.fraction / 1'000'000;
261 return std::formatter<std::string>::format(std::format(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}",
263 value.sqlValue.month,
266 value.sqlValue.minute,
267 value.sqlValue.second,
274struct std::formatter<std::optional<Lightweight::SqlDateTime>>: std::formatter<std::string>
276 LIGHTWEIGHT_FORCE_INLINE
auto format(std::optional<Lightweight::SqlDateTime>
const& value,
277 std::format_context& ctx)
const -> std::format_context::iterator
279 if (!value.has_value())
280 return std::formatter<std::string>::format(
"nullopt", ctx);
281 return std::formatter<std::string>::format(std::format(
"{}", value.value()), ctx);
289struct SqlDataBinder<SqlDateTime::native_type>
291 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
293 SqlDateTime::native_type* result,
295 SqlDataBinderCallback
const& )
noexcept
297 SQL_TIMESTAMP_STRUCT sqlValue {};
298 auto const rc = SQLGetData(stmt, column, SQL_C_TYPE_TIMESTAMP, &sqlValue,
sizeof(sqlValue), indicator);
299 if (SQL_SUCCEEDED(rc))
300 *result = SqlDateTime::ConvertToNative(sqlValue);
306struct LIGHTWEIGHT_API SqlDataBinder<SqlDateTime>
308 static constexpr auto ColumnType = SqlColumnTypeDefinitions::DateTime {};
310 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
312 SqlDateTime
const& value,
313 [[maybe_unused]] SqlDataBinderCallback
const& cb)
noexcept
315#if defined(_WIN32) || defined(_WIN64)
318 using namespace std::string_view_literals;
319 if (cb.ServerType() == SqlServerType::MICROSOFT_SQL && cb.DriverName() ==
"SQLSRV32.DLL"sv)
323 SQLSMALLINT sqlType { SQL_TYPE_TIMESTAMP };
324 SQLULEN paramSize { 23 };
325 SQLSMALLINT decimalDigits { 3 };
326 SQLSMALLINT nullable {};
328 auto const sqlDescribeParamResult =
329 SQLDescribeParam(stmt, column, &hints.sqlType, &hints.paramSize, &hints.decimalDigits, &hints.nullable);
330 if (SQL_SUCCEEDED(sqlDescribeParamResult))
332 return SQLBindParameter(stmt,
339 (SQLPOINTER) &value.sqlValue,
346 return SQLBindParameter(stmt,
353 (SQLPOINTER) &value.sqlValue,
358 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
359 SQLHSTMT stmt, SQLUSMALLINT column, SqlDateTime* result, SQLLEN* indicator, SqlDataBinderCallback& )
noexcept
362 *indicator =
sizeof(result->sqlValue);
363 return SQLBindCol(stmt, column, SQL_C_TYPE_TIMESTAMP, &result->sqlValue, 0, indicator);
366 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
370 SqlDataBinderCallback
const& )
noexcept
372 return SQLGetData(stmt, column, SQL_C_TYPE_TIMESTAMP, &result->sqlValue,
sizeof(result->sqlValue), indicator);
375 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(SqlDateTime
const& value)
noexcept
377 return std::format(
"{}", value);
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::year year() const noexcept
Returns the year of this date-time object.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::day day() const noexcept
Returns the day of this date-time object.
static LIGHTWEIGHT_FORCE_INLINE SqlDateTime Now() noexcept
Returns the current date and time.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDateTime(std::chrono::system_clock::time_point value) noexcept
Constructs a date and time from a time point.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlDate Date() const noexcept
Constructs only a date from a SQL date-time structure.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDateTime(std::chrono::year year, std::chrono::month month, std::chrono::day day, std::chrono::hours hour, std::chrono::minutes minute, std::chrono::seconds second, std::chrono::nanoseconds nanosecond=std::chrono::nanoseconds(0)) noexcept
Constructs a date and time from individual components.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::month month() const noexcept
Returns the month of this date-time object.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::seconds second() const noexcept
Returns the second of this date-time object.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::hours hour() const noexcept
Returns the hour of this date-time object.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::minutes minute() const noexcept
Returns the minute of this date-time object.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::nanoseconds nanosecond() const noexcept
Returns the nanosecond of this date-time object.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDateTime(std::chrono::year_month_day ymd, std::chrono::hh_mm_ss< duration_type > time) noexcept
Constructs a date and time from individual components.
static LIGHTWEIGHT_FORCE_INLINE SqlDateTime NowUTC() noexcept
Return the current date and time in UTC.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlTime Time() const noexcept
Constructs only a time from a SQL date-time structure.
constexpr LIGHTWEIGHT_FORCE_INLINE native_type value() const noexcept
Returns the current date and time.