Util.h#

Fully qualified name: carb/time/Util.h

File members: carb/time/Util.h

// SPDX-FileCopyrightText: Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
//
// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
// property and proprietary rights in and to this material, related
// documentation and any modifications thereto. Any use, reproduction,
// disclosure or distribution of this material and related documentation
// without an express license agreement from NVIDIA CORPORATION or
// its affiliates is strictly prohibited.
#pragma once

#include "../Defines.h"

#include <time.h>

namespace carb
{
namespace time
{

template <size_t N>
inline char* asctime_r(const struct tm* tm, char (&buf)[N]) noexcept
{
    // Buffer requirements as specified:
    // https://linux.die.net/man/3/gmtime_r
    // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/asctime-s-wasctime-s?view=msvc-170
    static_assert(N >= 26, "Insufficient buffer size");
#if CARB_PLATFORM_WINDOWS
    return asctime_s(buf, N, tm) == 0 ? buf : nullptr;
#elif CARB_POSIX
    return ::asctime_r(tm, buf);
#else
    CARB_UNSUPPORTED_PLATFORM();
#endif
}

template <size_t N>
inline char* ctime_r(const time_t* timep, char (&buf)[N]) noexcept
{
    // Buffer requirements as specified:
    // https://linux.die.net/man/3/gmtime_r
    // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/ctime-s-ctime32-s-ctime64-s-wctime-s-wctime32-s-wctime64-s?view=msvc-170
    static_assert(N >= 26, "Insufficient buffer size");
#if CARB_PLATFORM_WINDOWS
    auto err = ctime_s(buf, N, timep);
    return err == 0 ? buf : nullptr;
#elif CARB_POSIX
    return ::ctime_r(timep, buf);
#else
    CARB_UNSUPPORTED_PLATFORM();
#endif
}

inline struct tm* gmtime_r(const time_t* timep, struct tm* result) noexcept
{
#if CARB_PLATFORM_WINDOWS
    auto err = ::gmtime_s(result, timep);
    return err == 0 ? result : nullptr;
#elif CARB_POSIX
    return ::gmtime_r(timep, result);
#else
    CARB_UNSUPPORTED_PLATFORM();
#endif
}

inline struct tm* localtime_r(const time_t* timep, struct tm* result) noexcept
{
#if CARB_PLATFORM_WINDOWS
    auto err = ::localtime_s(result, timep);
    return err == 0 ? result : nullptr;
#elif CARB_POSIX
    return ::localtime_r(timep, result);
#else
    CARB_UNSUPPORTED_PLATFORM();
#endif
}

} // namespace time
} // namespace carb