I'm using the boost mysql library in a project. I need to use boost::mysql::datetime (datetime) to represent timestamps. A datetime value can be constructed from a boost::mysql::datetime::time_point.
using time_point = std::chrono::time_point< std::chrono::system_clock, std::chrono::duration< std::int64_t, std::micro >>;
How do I convert a "normal" std::chrono::system_clock::time_point to a boost::mysql::datetime::time_point?
As an interesting side note, boost::mysql does not use the boost Gregorian date library.
Details:
- Ubuntu 24.04
- C++23
- Boost 1.88
- gcc-14
Code:
#include <boost/asio.hpp>
#include <boost/mysql.hpp>
#include <chrono>
#include <exception>
#include <format>
#include <iostream>
#include <stdexcept>
int main()
{
boost::mysql::datetime::time_point tp = std::chrono::system_clock::now();
const std::time_t t_c = std::chrono::system_clock::to_time_t(tp);
std::cout << "The system clock is currently at " << std::ctime(&t_c);
return EXIT_SUCCESS;
}
The error for the above code:
main.cpp:11:75: error: conversion from ‘time_point<[...],duration<[...],ratio<[...],1000000000>>>’ to non-scalar type ‘time_point<[...],duration<[...],ratio<[...],1000000>>>’ requested
11 | boost::mysql::datetime::time_point tp = std::chrono::system_clock::now();
In case you want to reproduce my error message, here is the CMakeLists.txt file:
cmake_minimum_required(VERSION 3.31)
project(testTime LANGUAGES CXX)
if(POLICY CMP0167)
cmake_policy(SET CMP0167 NEW)
endif()
find_package(Boost 1.87.0 REQUIRED COMPONENTS system charconv program_options)
add_executable(testTime main.cpp)
target_compile_options(testTime PRIVATE -Wall -Wextra -pedantic -Werror)
target_compile_features(testTime PRIVATE cxx_std_23)
target_link_libraries(testTime ${Boost_LIBRARIES} ssl crypto)
std::chrono::time_point_cast?