Calendar time and clocks for C++ macros. More...
| int | time() |
| double | difftime(int later, int earlier) |
| int | clock() |
| int | localtime(int t, tm *out) |
| int | gmtime(int t, tm *out) |
| int | mktime(tm *t) |
| string | strftime(string fmt, tm *t) |
| string | asctime(tm *t) |
Calendar time and clocks for C++ macros. Load with #include
Unix time is a single integer: seconds since 1 January 1970, 00:00:00 UTC. That is what time() returns. To show a date to a human, convert it into a tm (broken-down fields: year, month, day, hour, …) with localtime or gmtime, then format it with strftime.
Several calls differ from desktop C:
time_t is treated as int in macros.
#include <time.h>
int main(){
int now = time();
tm t;
if (localtime(now, &t) != 0) return 1;
string s = strftime("%Y-%m-%d %H:%M:%S", &t);
cout("local: ", s, "\n");
cout("hour = ", t.tm_hour, "\n");
}
A tm is a structure (a group of named fields). Declare tm t; then fill it with localtime. Fields:
| Field | Meaning |
|---|---|
| tm_sec | seconds 0 … 60 (60 = leap second) |
| tm_min | minutes 0 … 59 |
| tm_hour | hours 0 … 23 |
| tm_mday | day of month 1 … 31 |
| tm_mon | month 0 … 11 (0 = January, 11 = December) |
| tm_year | years since 1900 (2026 → 126) |
| tm_wday | weekday 0 … 6 (0 = Sunday) |
| tm_yday | day of year 0 … 365 |
| tm_isdst | daylight-saving: positive if DST, 0 if not, negative if unknown |
POSIX tm_gmtoff is not bound.
| Name | Meaning |
|---|---|
| CLOCKS_PER_SEC | how many clock() ticks are one second |
| time_t | alias for int in this binding |
Current calendar time as seconds since 1970-01-01 UTC.
Takes no arguments.
Returns: int — Unix time. Useful as a timestamp or as input to localtime.
Difference between two time() values, in seconds (as a double, so fractions are possible if the values allow it).
Takes later (int), usually the more recent time(), and earlier (int), the older time().
Returns: double — later - earlier in seconds. Can be negative if you swap the arguments.
Processor time used by the program, in ticks. Divide by CLOCKS_PER_SEC to get seconds. This is not wall-clock time; it measures CPU work.
Takes no arguments.
Returns: int — ticks since an unspecified start (often since the macro started). Not comparable across machines without dividing by CLOCKS_PER_SEC.
Converts Unix time t into local time (your timezone) and writes the fields into out.
Takes t (int), value from time(), and out (tm pointer), address of a tm you declared (&t). Must not be omitted.
Returns: 0 on success, -1 on failure.
Same as localtime, but the result is UTC (no local timezone, no DST).
Takes t (int), Unix time, and out (tm pointer), &t of a tm variable.
Returns: 0 on success, -1 on failure.
Opposite of localtime: takes a filled tm (local time) and returns Unix time. It also normalizes the fields (for example day 32 becomes the next month).
Takes t (tm pointer), pointer to a tm. tm_wday and tm_yday are ignored on input and set on output.
Returns: Unix time (int), or -1 on error / if t is NULL.
Builds a human-readable date/time string from a tm using a format string.
Parameters:
Returns: string — the formatted text (empty on error). Maximum length is 255 characters.
This is not the four-argument C strftime.
Fixed English format: Www Mmm dd hh:mm:ss yyyy\n (for example Wed Sep 16 10:15:00 2026).
Takes t (tm pointer), &t.
Returns: string including a trailing newline. Empty if t is invalid.
Python: import time / datetime. This include is for C++ macros only.