TIME2POSIX(3)
π NAME π
okay so this is TIME2POSIX(3), from the Library Functions Manual β the page for time2posix and posix2time, aka the little duo that converts seconds since the Epoch. no cap, thatβs the whole vibe.
π«Ά LIBRARY π«Ά
bestie, these live in the Standard C Library (libc, -lc). iconic behavior.
β¨ SYNOPSIS β¨
#include <time.h>
time_t
time2posix(time_t t);
time_t
posix2time(time_t t);
πΈ DESCRIPTION πΈ
So IEEE Std 1003.1-1988 (βPOSIX.1β) says values cannot count leap seconds, and therefore the system time must be adjusted as each leap occurs. thatβs the rules, fr fr.
But if the time package is configured with leap-second support enabled, no such adjustment is needed, and time_t values continue to increase over leap events β like a true βseconds since...β value. the way that works means these values will differ from the ones POSIX requires by the net number of leap seconds inserted since the Epoch. lowkey a whole situation.
For many C programs this is not a problem, because the C standard says time_t is (mostly) opaque β time_t values should be obtained from and passed to functions such as time(2), localtime(3), mktime(3), and difftime(3). obsessed with that for them. However, POSIX gives an arithmetic expression for computing a time_t value directly from a given Universal date and time, and the same relationship is assumed by some applications. any programs creating/dissecting time_t values using such a relationship will typically not handle intervals over leap seconds correctly. not me finding out the hard way π.
The time2posix() and posix2time() functions address this mismatch by converting between local time_t values and their POSIX equivalents. This is done by accounting for the number of time-base changes that would have taken place on a POSIX system as leap seconds were inserted or deleted. these converted values can then be used when communicating with POSIX-compliant systems. slay.
The time2posix() function converts a time_t value to its POSIX counterpart, if one exists. The posix2time() function does the reverse but is less well-behaved: for a positive leap second hit the result is not unique, and for a negative leap second hit the corresponding POSIX time_t doesnβt exist so an adjacent value is returned. both of those are red flags that the POSIX representation has problems. periodt.
The following table summarizes the relationship between a time T and its conversion to, and back from, the POSIX representation over the leap second inserted at the end of June, 1993. In this table, X=time2posix(T), Y=posix2time(X), A=741484816, and B=A-17 because 17 positive leap seconds preceded this leap second. the columns are DATE, TIME, T, X, Y:
- Date
1993-06-30, time23:59:59βT = A,X = B,Y = A - Date
1993-06-30, time23:59:60βT = A+1,X = B+1,Y = A+1 or A+2 - Date
1993-07-01, time00:00:00βT = A+2,X = B+1,Y = A+1 or A+2 - Date
1993-07-01, time00:00:01βT = A+3,X = B+2,Y = A+3
A leap second deletion would look like the following, and posix2time(B+1) would return either A or A+1. again, columns are DATE, TIME, T, X, Y:
- Date
????-06-30, time23:59:58βT = A,X = B,Y = A - Date
????-07-01, time00:00:00βT = A+1,X = B+2,Y = A+1 - Date
????-07-01, time00:00:01βT = A+2,X = B+3,Y = A+2
If leap-second support is not enabled, local time_t and POSIX time_t values are equivalent, and both time2posix() and posix2time() degenerate to the identity function. itβs giving βsame picture, different font.β π
π RETURN VALUES π
If successful, these functions return the resulting timestamp without modifying errno. Otherwise, they set errno and return ((time_t) -1). brutal but fair.
π ERRORS π
These functions fail if:
[EOVERFLOW]β the resulting value cannot be represented. This can happen forposix2time()if its argument is close to the maximumtime_tvalue. In environments where theTZenvironment variable names aTZiffile, overflow can happen for either function for an argument sufficiently close to an extremetime_tvalue if theTZiffile specifies unrealistic leap second corrections.
bestie, thatβs the edge-case corner: EOVERFLOW is not a vibe, itβs a real error condition. π«Ά
π SEE ALSO π
difftime(3), localtime(3), mktime(3), time(3) β go read the whole friend group, iykyk. π¦
π cute sign-off: source/version is Debian, date is March 8, 2026 β bye bestie, go convert those timestamps! π
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms