#std::format can't format std::chrono::time_point

36 messages · Page 1 of 1 (latest)

tardy dagger
#

So I have this piece of code:

auto time_now = std::chrono::system_clock::now();
auto time_delta = duration->to_seconds();
auto future = time_now + time_delta;
auto future_str = std::format("{}", future);

However, in the last line, I get the following error:

In template: call to deleted constructor of 'typename format_context::formatter_type<time_point<system_clock, duration<long long, ratio<1, 1000000>>>>' (aka 'formatter<std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<long long, std::ratio<1, 1000000>>>, char>') error occurred here while substituting into a lambda expression here in instantiation of function template specialization 'std::basic_format_arg<std::format_context>::handle::handle<std::chrono::time_point<std::chrono::system_clock>>' requested here in instantiation of function template specialization 'std::basic_format_arg<std::format_context>::basic_format_arg<std::chrono::time_point<std::chrono::system_clock>>' requested here in instantiation of function template specialization 'std::make_format_args<std::format_context, std::chrono::time_point<std::chrono::system_clock>>' requested here in instantiation of function template specialization 'std::format<std::chrono::time_point<std::chrono::system_clock>>' requested here 'formatter' has been explicitly marked deleted here

I'm aware that I have to set a time format for it (per this SO post and this one), however doing any of those (i.e std::format("{:%Y-%m-%d %X}", future);) doesn't make the error disappear.

queen flowerBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

left crown
#

I think you need to convert it to another type first

#

actually wait

#

no should work

#

hmmm

tardy dagger
#

Isn't it weird?

#

It also didn't have this issue with fmt

#

that to_seconds type is std::chrono::seconds

#

fyi

lean horizon
#

;compile -std=c++23 ```cpp
#include <chrono>
#include <format>
#include <iostream>

std::string foo(std::chrono::seconds time_delta) {
auto time_now = std::chrono::system_clock::now();
auto future = time_now + time_delta;
return std::format("{}", future);
}

int main() {
using namespace std::literals;
std::cout << foo(123s);
}

pallid nacelleBOT
#
Program Output
2024-01-02 10:10:07.959272101
lean horizon
#

seems to work fine, what compiler/version?

tardy dagger
#

Clangd 18

#

It's clangd that's giving me this error

lean horizon
#

Clang with libstdc++, libc++, or MSVC STL?

#

(checking with libstdc++ and libc++ both work for me)

tardy dagger
#

It's not MSVC STL

#

I'm on linux

lean horizon
#

If you're on linux and you haven't specified explicitly to use libc++, then it's probably using whatever version of libstdc++ you have installed

#

Which you can check with g++ --version

#

(there are macros it defines too otherwise)

#

But in that case it's quite likely that the version is too old

#

(though that would surprise me I think? GCC13 was the first with text formatting at all...)

#

Perhaps it's clangd doing something weird and falling over

tardy dagger
#
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -stdlib=libc++  -fexperimental-library -v")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -std=c++20 -stdlib=libc++ -fexperimental-library -lc++-abi")
#

I got this in my CMake Lists

lean horizon
#

But OK, yes, looks like you're using libc++ (whichever version you have installed, I assume trunk by '18'?)

tardy dagger
#

Should I switch back to libstdc++?

tardy dagger
tardy dagger
#

I probably should switch back to libstdc++ cause it's what most linux C(++) libs are compiled with