System & concurrency

time.h

Calendar conversion, Unix timestamps, nanosecond timestamps, and the cosmic time scale.

C++23 mc/time.h
#include <mc/time.h>

Unix calendar conversions use the process’s local time zone. Invalid dates and nonexistent local times throw CError; repeated local times use the platform’s automatic choice.

Cosmic time is an exact count of seconds since January 1 of year −13,700,000,000 in the proleptic Gregorian calendar. It includes astronomical year zero, with no time zones or leap seconds.

Jump to a declaration · 14

Free functions & types

Types, constants & data

inline constexpr int64_t CTimeEpochYear = -13700000000LL;

Functions

cGetDayInfo

constexpr void cGetDayInfo(int year, int month, int day, int& dayOfYear, int& dayOfWeek);

Returns one-based day-of-year and weekday, with Sunday=1 and Saturday=7.

cDayOfYear

constexpr void cDayOfYear(uint64_t year, int dayOfYear, int& month, int& day);

Converts a one-based day-of-year into month and day outputs for the supplied year, accounting for leap years. Invalid day numbers throw CError.

cMakeTime

constexpr uint64_t cMakeTime(int64_t year, int month = 1, int day = 1, int hour = 0, int min = 0, int sec = 0);

Constructs seconds on the cosmic calendar timeline.

cUnmakeTime

constexpr void cUnmakeTime(uint64_t t, int64_t& year, int& month, int& day, int& hour, int& min, int& sec);

Splits cosmic seconds into calendar fields.

cEpochMake

inline double cEpochMake(int year, int month = 1, int day = 1, int hour = 0, int min = 0, double seconds = 0.0);

Constructs a Unix timestamp in seconds from local calendar fields.

cMakeNano

inline uint64_t cMakeNano(uint32_t year, uint32_t month, uint32_t day, uint32_t hour, uint32_t min, uint32_t sec, uint64_t nsec = 0);

Constructs Unix nanoseconds from local calendar fields and a nanosecond fraction.

cEpochUnmakeSeconds

inline void cEpochUnmakeSeconds(double t, int& year, int& month, int& day, int& hour, int& min, int& sec);
inline void cEpochUnmakeSeconds(double t, int& year, int& month, int& day, int& dayOfWeek, int& hour, int& min, int& sec);

Splits Unix seconds into local calendar fields, flooring fractional seconds.

cEpochUnmakeNano

inline void cEpochUnmakeNano(uint64_t t, int& year, int& month, int& day, int& hour, int& min, int& sec);
inline void cEpochUnmakeNano(uint64_t t, int& year, int& month, int& day, int& dayOfWeek, int& hour, int& min, int& sec);

Splits Unix nanoseconds into local calendar fields, discarding subsecond precision.

cEpochUnmake

inline void cEpochUnmake(double t, int& year, int& month, int& day, int& hour, int& min, int& sec);
inline void cEpochUnmake(double t, int& year, int& month, int& day, int& dayOfWeek, int& hour, int& min, int& sec);
inline void cEpochUnmake(uint64_t t, int& year, int& month, int& day, int& hour, int& min, int& sec);
inline void cEpochUnmake(uint64_t t, int& year, int& month, int& day, int& dayOfWeek, int& hour, int& min, int& sec);
template<std::integral T> requires (!std::same_as<T, uint64_t> && !std::same_as<T, bool>) inline void cEpochUnmake(T t, int& year, int& month, int& day, int& hour, int& min, int& sec);
template<std::integral T> requires (!std::same_as<T, uint64_t> && !std::same_as<T, bool>) inline void cEpochUnmake(T t, int& year, int& month, int& day, int& dayOfWeek, int& hour, int& min, int& sec);

Compatibility overloads: floating-point input is seconds; integral input is nanoseconds.

cTimeStr

inline cstr cTimeStr(uint64_t time, const cstr& format = "%Y-%m-%d %H:%M:%S");

Formats Unix seconds in the local time zone.

cNanoTimeStr

inline cstr cNanoTimeStr(uint64_t time, const cstr& format = "%Y-%m-%d %H:%M:%S");

Formats Unix nanoseconds in the local time zone.

cTimestamp

inline cstr cTimestamp(const cstr& format = "%Y-%m-%d %H:%M");

Formats the current wall-clock time in the local time zone. The default format includes the date, hour, and minute.

cNanoTime

inline uint64_t cNanoTime(const cstr& timeStr, const cstr& format = "%Y-%m-%d %H:%M:%S");

Parses local calendar text and returns Unix nanoseconds.