5#include "../SqlColumnTypeDefinitions.hpp"
24 using native_type = std::chrono::system_clock::time_point;
25 using duration_type = std::chrono::system_clock::duration;
30 return SqlDateTime { std::chrono::system_clock::now() };
33#if !defined(LIGHTWEIGHT_CXX26_REFLECTION)
37 return SqlDateTime { std::chrono::system_clock::time_point { std::chrono::utc_clock::now().time_since_epoch() } };
48 constexpr std::weak_ordering operator<=>(
SqlDateTime const& other) const noexcept
52 constexpr bool operator==(SqlDateTime
const& other)
const noexcept
54 return (*this <=> other) == std::weak_ordering::equivalent;
57 constexpr bool operator!=(SqlDateTime
const& other)
const noexcept
59 return !(*
this == other);
63 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDateTime(std::chrono::year_month_day ymd,
64 std::chrono::hh_mm_ss<duration_type> time)
noexcept:
66 .year = (SQLSMALLINT) (
int) ymd.year(),
67 .month = (SQLUSMALLINT) (
unsigned) ymd.month(),
68 .day = (SQLUSMALLINT) (
unsigned) ymd.day(),
69 .hour = (SQLUSMALLINT) time.hours().count(),
70 .minute = (SQLUSMALLINT) time.minutes().count(),
71 .second = (SQLUSMALLINT) time.seconds().count(),
73 (SQLUINTEGER) (std::chrono::duration_cast<std::chrono::nanoseconds>(time.to_duration()).count() / 100) * 100,
80 std::chrono::year
year,
81 std::chrono::month
month,
83 std::chrono::hours
hour,
84 std::chrono::minutes
minute,
85 std::chrono::seconds
second,
86 std::chrono::nanoseconds
nanosecond = std::chrono::nanoseconds(0)) noexcept:
88 .year = (SQLSMALLINT) (
int)
year,
89 .month = (SQLUSMALLINT) (
unsigned)
month,
90 .day = (SQLUSMALLINT) (
unsigned)
day,
91 .hour = (SQLUSMALLINT)
hour.count(),
92 .minute = (SQLUSMALLINT)
minute.count(),
93 .second = (SQLUSMALLINT)
second.count(),
94 .fraction = (SQLUINTEGER) (
nanosecond.count() / 100) * 100,
100 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDateTime(std::chrono::system_clock::time_point
value)
noexcept:
101 sqlValue { ConvertToSqlValue(
value) }
108 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::year
year() const noexcept
110 return std::chrono::year(
static_cast<int>(sqlValue.year));
114 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::month
month() const noexcept
116 return std::chrono::month(
static_cast<unsigned>(sqlValue.month));
120 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::day
day() const noexcept
122 return std::chrono::day(
static_cast<unsigned>(sqlValue.day));
126 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::hours
hour() const noexcept
128 return std::chrono::hours(
static_cast<unsigned>(sqlValue.hour));
132 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::minutes
minute() const noexcept
134 return std::chrono::minutes(
static_cast<unsigned>(sqlValue.minute));
138 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::seconds
second() const noexcept
140 return std::chrono::seconds(
static_cast<unsigned>(sqlValue.second));
144 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::nanoseconds
nanosecond() const noexcept
146 return std::chrono::nanoseconds(
static_cast<unsigned>(sqlValue.fraction));
151 LIGHTWEIGHT_FORCE_INLINE
constexpr operator native_type() const noexcept
156 static LIGHTWEIGHT_FORCE_INLINE SQL_TIMESTAMP_STRUCT
constexpr ConvertToSqlValue(native_type
value)
noexcept
158 using namespace std::chrono;
159 auto const totalDays = floor<days>(
value);
160 auto const ymd = year_month_day { totalDays };
162 hh_mm_ss<duration_type> { std::chrono::duration_cast<duration_type>(floor<nanoseconds>(
value - totalDays)) };
163 return ConvertToSqlValue(ymd, hms);
166 static LIGHTWEIGHT_FORCE_INLINE SQL_TIMESTAMP_STRUCT
constexpr ConvertToSqlValue(
167 std::chrono::year_month_day ymd, std::chrono::hh_mm_ss<duration_type> hms)
noexcept
171 return SQL_TIMESTAMP_STRUCT {
172 .year = (SQLSMALLINT) (
int) ymd.year(),
173 .month = (SQLUSMALLINT) (
unsigned) ymd.month(),
174 .day = (SQLUSMALLINT) (
unsigned) ymd.day(),
175 .hour = (SQLUSMALLINT) hms.hours().count(),
176 .minute = (SQLUSMALLINT) hms.minutes().count(),
177 .second = (SQLUSMALLINT) hms.seconds().count(),
178 .fraction = (SQLUINTEGER) (((std::chrono::duration_cast<std::chrono::nanoseconds>(hms.to_duration()).count() % 1'000'000'000LLU) / 100) * 100)
183 static LIGHTWEIGHT_FORCE_INLINE native_type
constexpr ConvertToNative(SQL_TIMESTAMP_STRUCT
const& time)
noexcept
186 using namespace std::chrono;
187 auto const ymd = year_month_day { std::chrono::year { time.year } / std::chrono::month { time.month } / std::chrono::day { time.day } };
188 auto const hms = hh_mm_ss<duration_type> {
189 duration_cast<duration_type>(
191 + minutes { time.minute }
192 + seconds { time.second }
193 + nanoseconds { time.fraction }
196 return sys_days { ymd } + hms.to_duration();
201 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE native_type
value() const noexcept
203 return ConvertToNative(sqlValue);
206 LIGHTWEIGHT_FORCE_INLINE
SqlDateTime& operator+=(duration_type duration)
noexcept
212 LIGHTWEIGHT_FORCE_INLINE SqlDateTime& operator-=(duration_type duration)
noexcept
214 *
this = SqlDateTime {
value() - duration };
218 friend LIGHTWEIGHT_FORCE_INLINE SqlDateTime operator+(SqlDateTime dateTime, duration_type duration)
noexcept
220 return SqlDateTime { dateTime.value() + duration };
223 friend LIGHTWEIGHT_FORCE_INLINE SqlDateTime operator-(SqlDateTime dateTime, duration_type duration)
noexcept
225 return SqlDateTime { dateTime.value() - duration };
228 SQL_TIMESTAMP_STRUCT sqlValue {};
234struct std::formatter<Lightweight::SqlDateTime>: std::formatter<std::string>
237 -> std::format_context::iterator
243 auto const milliseconds = value.sqlValue.fraction / 1'000'000;
244 return std::formatter<std::string>::format(std::format(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}",
246 value.sqlValue.month,
249 value.sqlValue.minute,
250 value.sqlValue.second,
257struct std::formatter<std::optional<Lightweight::SqlDateTime>>: std::formatter<std::string>
259 LIGHTWEIGHT_FORCE_INLINE
auto format(std::optional<Lightweight::SqlDateTime>
const& value,
260 std::format_context& ctx)
const -> std::format_context::iterator
262 if (!value.has_value())
263 return std::formatter<std::string>::format(
"nullopt", ctx);
264 return std::formatter<std::string>::format(std::format(
"{}", value.value()), ctx);
272struct SqlDataBinder<SqlDateTime::native_type>
274 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
276 SqlDateTime::native_type* result,
278 SqlDataBinderCallback
const& )
noexcept
280 SQL_TIMESTAMP_STRUCT sqlValue {};
281 auto const rc = SQLGetData(stmt, column, SQL_C_TYPE_TIMESTAMP, &sqlValue,
sizeof(sqlValue), indicator);
282 if (SQL_SUCCEEDED(rc))
283 *result = SqlDateTime::ConvertToNative(sqlValue);
289struct LIGHTWEIGHT_API SqlDataBinder<SqlDateTime>
291 static constexpr auto ColumnType = SqlColumnTypeDefinitions::DateTime {};
293 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
295 SqlDateTime
const& value,
296 [[maybe_unused]] SqlDataBinderCallback
const& cb)
noexcept
298#if defined(_WIN32) || defined(_WIN64)
301 using namespace std::string_view_literals;
302 if (cb.ServerType() == SqlServerType::MICROSOFT_SQL && cb.DriverName() ==
"SQLSRV32.DLL"sv)
306 SQLSMALLINT sqlType { SQL_TYPE_TIMESTAMP };
307 SQLULEN paramSize { 23 };
308 SQLSMALLINT decimalDigits { 3 };
309 SQLSMALLINT nullable {};
311 auto const sqlDescribeParamResult =
312 SQLDescribeParam(stmt, column, &hints.sqlType, &hints.paramSize, &hints.decimalDigits, &hints.nullable);
313 if (SQL_SUCCEEDED(sqlDescribeParamResult))
315 return SQLBindParameter(stmt,
322 (SQLPOINTER) &value.sqlValue,
329 return SQLBindParameter(stmt,
336 (SQLPOINTER) &value.sqlValue,
341 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
342 SQLHSTMT stmt, SQLUSMALLINT column, SqlDateTime* result, SQLLEN* indicator, SqlDataBinderCallback& )
noexcept
345 *indicator =
sizeof(result->sqlValue);
346 return SQLBindCol(stmt, column, SQL_C_TYPE_TIMESTAMP, &result->sqlValue, 0, indicator);
349 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
353 SqlDataBinderCallback
const& )
noexcept
355 return SQLGetData(stmt, column, SQL_C_TYPE_TIMESTAMP, &result->sqlValue,
sizeof(result->sqlValue), indicator);
358 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(SqlDateTime
const& value)
noexcept
360 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.
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 native_type value() const noexcept
Returns the current date and time.