carb/time/Util.h

File members: carb/time/Util.h

// Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION 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