5#include "../SqlColumnTypeDefinitions.hpp"
34 return SqlDateTime { std::chrono::system_clock::now() };
37#if !defined(LIGHTWEIGHT_CXX26_REFLECTION)
41 return SqlDateTime { std::chrono::system_clock::time_point { std::chrono::utc_clock::now().time_since_epoch() } };
58 constexpr std::weak_ordering operator<=>(
SqlDateTime const& other) const noexcept
65 return (*this <=> other) == std::weak_ordering::equivalent;
71 return !(*
this == other);
75 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDateTime(std::chrono::year_month_day ymd,
76 std::chrono::hh_mm_ss<duration_type> time)
noexcept:
78 .year = (SQLSMALLINT) (
int) ymd.year(),
79 .month = (SQLUSMALLINT) (
unsigned) ymd.month(),
80 .day = (SQLUSMALLINT) (
unsigned) ymd.day(),
81 .hour = (SQLUSMALLINT) time.hours().count(),
82 .minute = (SQLUSMALLINT) time.minutes().count(),
83 .second = (SQLUSMALLINT) time.seconds().count(),
85 (SQLUINTEGER) (std::chrono::duration_cast<std::chrono::nanoseconds>(time.to_duration()).count() / 100) * 100,
92 std::chrono::year
year,
93 std::chrono::month
month,
95 std::chrono::hours
hour,
96 std::chrono::minutes
minute,
97 std::chrono::seconds
second,
98 std::chrono::nanoseconds
nanosecond = std::chrono::nanoseconds(0)) noexcept:
100 .year = (SQLSMALLINT) (
int)
year,
101 .month = (SQLUSMALLINT) (
unsigned)
month,
102 .day = (SQLUSMALLINT) (
unsigned)
day,
103 .hour = (SQLUSMALLINT)
hour.count(),
104 .minute = (SQLUSMALLINT)
minute.count(),
105 .second = (SQLUSMALLINT)
second.count(),
106 .fraction = (SQLUINTEGER) (
nanosecond.count() / 100) * 100,
112 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDateTime(std::chrono::system_clock::time_point
value)
noexcept:
120 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::year
year() const noexcept
122 return std::chrono::year(
static_cast<int>(
sqlValue.year));
126 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::month
month() const noexcept
128 return std::chrono::month(
static_cast<unsigned>(
sqlValue.month));
132 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::day
day() const noexcept
134 return std::chrono::day(
static_cast<unsigned>(
sqlValue.day));
138 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::hours
hour() const noexcept
140 return std::chrono::hours(
static_cast<unsigned>(
sqlValue.hour));
144 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::minutes
minute() const noexcept
146 return std::chrono::minutes(
static_cast<unsigned>(
sqlValue.minute));
150 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::seconds
second() const noexcept
152 return std::chrono::seconds(
static_cast<unsigned>(
sqlValue.second));
156 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::nanoseconds
nanosecond() const noexcept
158 return std::chrono::nanoseconds(
static_cast<unsigned>(
sqlValue.fraction));
164 LIGHTWEIGHT_FORCE_INLINE
constexpr operator native_type() const noexcept
170 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
SqlDate Date() const noexcept
176 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
SqlTime Time() const noexcept
179 std::chrono::minutes {
minute() },
180 std::chrono::seconds {
second() },
181 std::chrono::microseconds { std::chrono::duration_cast<std::chrono::microseconds>(
nanosecond()) } };
187 using namespace std::chrono;
188 auto const totalDays = floor<days>(
value);
189 auto const ymd = year_month_day { totalDays };
191 hh_mm_ss<duration_type> { std::chrono::duration_cast<duration_type>(floor<nanoseconds>(
value - totalDays)) };
197 std::chrono::year_month_day ymd, std::chrono::hh_mm_ss<duration_type> hms)
noexcept
201 return SQL_TIMESTAMP_STRUCT {
202 .year = (SQLSMALLINT) (
int) ymd.year(),
203 .month = (SQLUSMALLINT) (
unsigned) ymd.month(),
204 .day = (SQLUSMALLINT) (
unsigned) ymd.day(),
205 .hour = (SQLUSMALLINT) hms.hours().count(),
206 .minute = (SQLUSMALLINT) hms.minutes().count(),
207 .second = (SQLUSMALLINT) hms.seconds().count(),
208 .fraction = (SQLUINTEGER) (((
static_cast<unsigned long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(hms.to_duration()).count()) % 1'000'000'000LLU) / 100) * 100)
217 using namespace std::chrono;
218 auto const ymd = year_month_day { std::chrono::year { time.year } / std::chrono::month { time.month } / std::chrono::day { time.day } };
219 auto const hms = hh_mm_ss<duration_type> {
220 duration_cast<duration_type>(
222 + minutes { time.minute }
223 + seconds { time.second }
224 + nanoseconds { time.fraction }
227 return sys_days { ymd } + hms.to_duration();
258 return SqlDateTime { dateTime.value() - duration };
268struct std::formatter<Lightweight::SqlDateTime>: std::formatter<std::string>
271 -> std::format_context::iterator
277 auto const milliseconds = value.sqlValue.fraction / 1'000'000;
278 return std::formatter<std::string>::format(std::format(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}",
280 value.sqlValue.month,
283 value.sqlValue.minute,
284 value.sqlValue.second,
291struct std::formatter<std::optional<Lightweight::SqlDateTime>>: std::formatter<std::string>
293 LIGHTWEIGHT_FORCE_INLINE
auto format(std::optional<Lightweight::SqlDateTime>
const& value,
294 std::format_context& ctx)
const -> std::format_context::iterator
296 if (!value.has_value())
297 return std::formatter<std::string>::format(
"nullopt", ctx);
298 return std::formatter<std::string>::format(std::format(
"{}", value.value()), ctx);
306struct SqlDataBinder<SqlDateTime::native_type>
308 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
312 SqlDataBinderCallback
const& )
noexcept
314 SQL_TIMESTAMP_STRUCT sqlValue {};
315 auto const rc = SQLGetData(stmt, column, SQL_C_TYPE_TIMESTAMP, &sqlValue,
sizeof(sqlValue), indicator);
316 if (SQL_SUCCEEDED(rc))
323struct LIGHTWEIGHT_API SqlDataBinder<SqlDateTime>
325 static constexpr auto ColumnType = SqlColumnTypeDefinitions::DateTime {};
327 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
329 SqlDateTime
const& value,
330 [[maybe_unused]] SqlDataBinderCallback
const& cb)
noexcept
332#if defined(_WIN32) || defined(_WIN64)
335 using namespace std::string_view_literals;
336 if (cb.ServerType() == SqlServerType::MICROSOFT_SQL && cb.DriverName() ==
"SQLSRV32.DLL"sv)
340 SQLSMALLINT sqlType { SQL_TYPE_TIMESTAMP };
341 SQLULEN paramSize { 23 };
342 SQLSMALLINT decimalDigits { 3 };
343 SQLSMALLINT nullable {};
345 auto const sqlDescribeParamResult =
346 SQLDescribeParam(stmt, column, &hints.sqlType, &hints.paramSize, &hints.decimalDigits, &hints.nullable);
347 if (SQL_SUCCEEDED(sqlDescribeParamResult))
349 return SQLBindParameter(stmt,
356 (SQLPOINTER) &value.sqlValue,
363 return SQLBindParameter(stmt,
370 (SQLPOINTER) &value.sqlValue,
375 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
376 SQLHSTMT stmt, SQLUSMALLINT column, SqlDateTime* result, SQLLEN* indicator, SqlDataBinderCallback& )
noexcept
379 *indicator =
sizeof(result->sqlValue);
380 return SQLBindCol(stmt, column, SQL_C_TYPE_TIMESTAMP, &result->sqlValue, 0, indicator);
383 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
387 SqlDataBinderCallback
const& )
noexcept
389 return SQLGetData(stmt, column, SQL_C_TYPE_TIMESTAMP, &result->sqlValue,
sizeof(result->sqlValue), indicator);
392 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(SqlDateTime
const& value)
noexcept
394 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 SQL_TIMESTAMP_STRUCT constexpr ConvertToSqlValue(std::chrono::year_month_day ymd, std::chrono::hh_mm_ss< duration_type > hms) noexcept
Converts year_month_day and hh_mm_ss components to the underlying SQL timestamp structure.
static LIGHTWEIGHT_FORCE_INLINE SqlDateTime Now() noexcept
Returns the current date and time.
constexpr bool operator==(SqlDateTime const &other) const noexcept
Equality comparison operator.
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.
std::chrono::system_clock::time_point native_type
The native C++ type representing a date-time value.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::month month() const noexcept
Returns the month of this date-time object.
SQL_TIMESTAMP_STRUCT sqlValue
Holds the underlying SQL timestamp structure.
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.
std::chrono::system_clock::duration duration_type
The duration type used for arithmetic operations.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::minutes minute() const noexcept
Returns the minute of this date-time object.
LIGHTWEIGHT_FORCE_INLINE SqlDateTime & operator-=(duration_type duration) noexcept
Subtracts a duration from this date-time.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::nanoseconds nanosecond() const noexcept
Returns the nanosecond of this date-time object.
constexpr SqlDateTime() noexcept=default
Default constructor.
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.
static LIGHTWEIGHT_FORCE_INLINE SQL_TIMESTAMP_STRUCT constexpr ConvertToSqlValue(native_type value) noexcept
Converts a native time_point to the underlying SQL timestamp structure.
LIGHTWEIGHT_FORCE_INLINE SqlDateTime & operator+=(duration_type duration) noexcept
Adds a duration to this date-time.
constexpr LIGHTWEIGHT_FORCE_INLINE native_type value() const noexcept
Returns the current date and time.
constexpr bool operator!=(SqlDateTime const &other) const noexcept
Inequality comparison operator.
static LIGHTWEIGHT_FORCE_INLINE native_type constexpr ConvertToNative(SQL_TIMESTAMP_STRUCT const &time) noexcept
Converts a SQL timestamp structure to the native time_point representation.