Search code examples
c++macosc++20clang++c++-chrono

MacOS std::chrono::clock_cast not found


I am trying to use the new C++20 chrono library, more specifically the new clock_cast.

If I compile on Linux with clang++ 16.0.6 it works as expected.
However, if I try to compile on MacOS I encounter the following error:

error: no member named 'clock_cast' in namespace 'std::chrono'

Clang is version 17.0.5 on the Mac, so I would expect the chrono library to be implemented, do I need to add another include?
I couldn't find anything online about this issue.

Clang version:

Homebrew clang version 17.0.5
Target: arm64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

Also tried with the default xcode clang version, same error.

Minimum reproducible example:

#include <chrono>

int main() {
    auto time = std::chrono::high_resolution_clock::now();
    auto system_time = std::chrono::clock_cast<std::chrono::system_clock>(time);
}

Compilation options:

/opt/homebrew/opt/llvm/bin/clang++ -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -D_LARGEFILE_SOURCE -isystem /opt/homebrew/Cellar/openssl@3/3.1.4/include -std=gnu++20 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk -fPIC -o test.cpp.o -c test.cpp

Solution

  • This has not yet been implemented.

    Here is the libc++ status page for C++20:

    https://libcxx.llvm.org/Status/Cxx20.html

    The pertinent row is:

    P0355R7 LWG Extending chrono to Calendars and Time Zones Jacksonville Partial [9]

    The footnote is:

    [9] P0355: The implementation status is:

    • Calendars mostly done in Clang 7
    • Input parsers not done
    • Stream output Obsolete due to P1361R2 “Integration of chrono with text formatting”
    • Time zone and leap seconds In Progress
    • TAI clock not done
    • GPS clock not done
    • UTC clock not done

    Note that this:

    auto time = std::chrono::high_resolution_clock::now();
    auto system_time = std::chrono::clock_cast<std::chrono::system_clock>(time);
    

    will never portably work as there is no defined relationship between high_resolution_clock and system_clock. On gcc these are the same clock and so clock_cast just performs the identity conversion. On libc++ high_resolution_clock is the same as steady_clock, and steady_clock has no defined relationship to system_clock.

    clock_cast will perform conversions among: system_clock, utc_clock, tai_clock, gps_clock, file_clock, and any user-defined clock which defines one of these pairs of static member functions:

    to_sys
    from_sys
    

    or

    to_utc
    from_utc
    

    See this answer (section "Update for C++20") for an example of a user-defined clock which participates in the clock_cast infrastructure.