Repository: gabime/spdlog
File: include/spdlog/details/os-inl.h (C/C++)

Code before:
    if (::fstat64(fd, &st) == 0) {
        return static_cast<size_t>(st.st_size);
    }
#else  // other unix or linux 32 bits or cygwin
    struct stat st;
    if (::fstat(fd, &st) == 0) {
        return static_cast<size_t>(st.st_size);
    }
#endif
#endif
    throw_spdlog_ex("Failed getting file size from fd", errno);
    return 0;  // will not be reached.
}

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#if !defined(SPDLOG_NO_TZ_OFFSET)
#ifdef _WIN32
// Compare the timestamp as Local (mktime) vs UTC (_mkgmtime) to get the offset.
// This replaces our previous implementation using GetDynamicTimeZoneInformation().
// It is ~2.5x faster and fixes potential inaccuracy of DST handling of past timestamps.
SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
    std::tm local_tm = tm;  // copy since mktime may adjust it
    std::time_t true_utc_time = std::mktime(&local_tm);
    if (true_utc_time == -1) {
        return 0;  // Fallback if time is out of range (e.g. pre-1970)
    }

    std::tm utc_tm = tm;  // copy since _mkgmtime may adjust it
    std::time_t face_value_time = _mkgmtime(&utc_tm);
    if (face_value_time == -1) {
        return 0;  // Fallback
    }
    auto offset_seconds = face_value_time - true_utc_time;
    return static_cast<int>(offset_seconds / 60);
}
#else
// On unix simply use tm_gmtoff
SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
    return static_cast<int>(tm.tm_gmtoff / 60);
}
#endif  // _WIN32
#endif  // SPDLOG_NO_TZ_OFFSET

// Return current thread id as size_t
// It exists because the std::this_thread::get_id() is much slower(especially
// under VS 2013)
SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT {
#ifdef _WIN32

Review comment (suggestion):
The variable name 'true_utc_time' is misleading. The result of `mktime()` is actually a time_t representing the input interpreted as local time, not UTC time. Consider renaming to 'local_time' or 'local_timestamp' to clarify that this represents the local interpretation.

Code after:
    if (::fstat64(fd, &st) == 0) {
        return static_cast<size_t>(st.st_size);
    }
#else  // other unix or linux 32 bits or cygwin
    struct stat st;
    if (::fstat(fd, &st) == 0) {
        return static_cast<size_t>(st.st_size);
    }
#endif
#endif
    throw_spdlog_ex("Failed getting file size from fd", errno);
    return 0;  // will not be reached.
}

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#if !defined(SPDLOG_NO_TZ_OFFSET)
#ifdef _WIN32
// Compare the timestamp as Local (mktime) vs UTC (_mkgmtime) to get the offset.
SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
    std::tm local_tm = tm;  // copy since mktime might adjust it (normalize dates, set tm_isdst)
    std::time_t local_time_t = std::mktime(&local_tm);
    if (local_time_t == -1) {
        return 0; // fallback
    }

    std::time_t utc_time_t = _mkgmtime(&local_tm);
    if (utc_time_t == -1) {
        return 0; // fallback
    }
    auto offset_seconds = utc_time_t - local_time_t;
    return static_cast<int>(offset_seconds / 60);
}
#else
// On unix simply use tm_gmtoff
SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
    return static_cast<int>(tm.tm_gmtoff / 60);
}
#endif  // _WIN32
#endif  // SPDLOG_NO_TZ_OFFSET

// Return current thread id as size_t
// It exists because the std::this_thread::get_id() is much slower(especially
// under VS 2013)
SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT {
#ifdef _WIN32
    return static_cast<size_t>(::GetCurrentThreadId());
#elif defined(__linux__)
#if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)