TM(3)
π tm(3) β seconds resolution time conversion support π
okay bestie, this is the tm(3) page from the Library Functions Manual, and it's genuinely iconic for date/time stuff. let's get into it π
πΈ NAME πΈ
tm β seconds resolution time conversion support
β¨ SYNOPSIS β¨
#include <tm.h>
π¦ DESCRIPTION π¦
The tm library supports conversion between string date specifications, seconds-resolution time_t clock values, and Tm_t. Tm_t contains the elements of struct tm along with these additions:
unsigned _ast_int4_t tm_nsecβ the subsecond portion of the time, in nanoseconds.Tm_zone_t* tm_zoneβ the time zone name.
localtime() and gmtime() (see ctime(3)) are used to determine local time zone and savings time information.
time_t values are the number of seconds since the epoch, Jan 1 00:00:00 UTC 1970, with leap seconds omitted.
The global variable int tm_info.flags contains flags that let all programs using the library be controlled in a consistent manner. tm_info.flags is initialized by the tminit() routine described below, and may be explicitly reset after tminit() is called. The flags are:
TM_ADJUSTβ set bytminit()iflocaltime()andgmtime()do not compensate for leap seconds.TM_LEAPβtime_tvalues are interpreted as if they include leap seconds. Set bytminit()if the leap option is set in theTM_OPTIONSenvironment variable.TM_UTCβ times are relative to UTC (universal coordinated time, i.e., GMT.) Otherwise times are relative to the local time zone. Set bytminit()if the time zone name matches one oftm_info.format[43]throughtm_info.format[46]described below. If the time zone name is not determined bylocaltime()then the environment variablesTZNAME(as described in BSD 4.3) andTZ(as described in System V) are checked, in order. If this fails then the time zone name is constructed using the local time zone offset.
π The routines π
time_t tmdate(const char* date, char** end, time_t* clock)β parses the date specificationdateusing thetm_info.formatstring table (described below) and returns the equivalenttime_tvalue. If non-NULL,endis set to the position of the first unrecognized character indate.clockis used to provide default values for omitted components indate. Ifclockis NULL then the current time is used.Tm_t* tmfix(Tm_t* tp)β corrects any out of bounds fields intpand returnstpas its value. The corrections start withtp->tm_secand propagate down totp->tm_year. For example, iftp->tm_secwere 61 then it would change to 1 andtp->tm_minwould be incremented by 1, and so on.tp->tm_isdstis not changed β calltmtime()to determine its proper value after thetmfix()adjustments.char* tmfmt(char* buf, size_t len, const char* format, time_t* clock)β formats the date pointed to byclockinto the bufferbufwith sizelenbytes according to the format specificationformat. Ifformatis NULL or empty then the stringtm_info.format[40]is used. Ifclockis NULL then the current time is used. A pointer to the end ofbuf(i.e., the terminating'\0') is returned.
format is in printf(3) style, where %field names a fixed size field, zero padded if necessary, and \c and \nnn sequences are as in C. Invalid %field specifications and all other characters are copied without change. field may be preceded by %- to turn off padding or %_ to pad with space, otherwise numeric fields are padded with 0 and string fields are padded with space. field may also be preceded by E for alternate era representation or O for alternate digit representation (if supported by the current locale.) Finally, an integral width preceding field truncates the field to width characters. Escape sequences are interpreted as in the C language. String field values are taken from the tm_info.format string table. The fields are:
%β%character.aβ abbreviated weekday name.Aβ full weekday name.bβ abbreviated month name.cβctime(3)style date without the trailing newline.Cβdate(1)style date.dβ day of month number.Dβ date asmm/dd/yy.eβ blank padded day of month number.Eβ unpadded day of month number.fβ locale default override date format.Fβ locale default date format (tm_info.format[40].)hβ abbreviated month name.Hβ 24-hour clock hour.iβ internationaldate(1)date that includes the time zone type name (tm_info.format[107].)Iβ 12-hour clock hour.jβ 1-offset Julian date.Jβ 0-offset Julian date.kβdate(1)style date (tm_info.format[106].)Kβ language neutral, all numeric, no embedded space date with larger to smaller time units from left to right, suitable for sorting:'"%Y-%m-%d+%H:%M:%S"'.lβls(1) -ldate that lists recent dates with%gand distant dates with%G.mβ month number.Mβ minutes.nβ newline character.Nβ the time zone type or nation code.pβ meridian (e.g., AM or PM.)qβ the nanosecond part of the time.%Q<delim>recent<delim>distant<delim>β recent dates are formatted withrecentand distant dates are formatted withdistant, where<delim>is any character not appearing inrecentordistant.rβ 12-hour time ashh:mm:ss meridian.Rβ 24-hour time ashh:mm.sβ seconds since the epoch..precprecedingsappendsprecnanosecond digits, 9 ifprecis omitted.Sβseconds.subsecondssince the epoch.tβ tab character.Tβ 24-hour time ashh:mm:ss.uβ weekday number with 1 for Monday, 7 for Sunday.Uβ week number with Sunday as the first day.Vβ ISO week number (i18n is fun.)wβ weekday number with 0 for Sunday, 6 for Saturday.Wβ week number with Monday as the first day.xβ local date style, usingtm_info.format[39], that includes the month, day and year.Xβ local time style, usingtm_info.format[38], that includes the hours and minutes.yβ 2-digit year (you'll be sorry.)Yβ 4-digit year.zβ time zoneSHHMMwest of UTC offset whereSis+or-.Zβ time zone name.=[=]][-+]]flagβ set (default or+) or clear (-)flagintm_info.flagsfor the remainder offormat, or for the remainder of the process if==is specified.flagmay be:lβ (TM_LEAP) enable leap second adjustments.sβ (TM_SUBSECOND) append nanosecond.%Nto%S.uβ (TM_UTC) UTC time zone.
#β equivalent to%s.?alternateβ use the alternate format if a default format override has not been specified. e.g.,ls(1)uses%?%l. ExportTM_OPTIONS="format='override'"to override the default.
void tminit(Tm_zone_t* zone)β implicitly called by the othertmlibrary routines to initialize global data, including thetm_info.formattable and thetm_info.flagsglobal flags. Global data should only be modified after an explicit call totminit. Ifzone != 0then it specifies a time zone other than the local time zone.void tmset(Tm_zone_t* zone);βtmsetsets the reference timezone tozone.tm_info.localpoints to the local timezone andtm_info.zonepoints to the current reference timezone.time_t tmleap(time_t* clock)β returns atime_tvalue for the time pointed to byclockwith leap seconds adjusted for external routines that do not handle leap seconds. Ifclockis NULL then the current time is used. Adjustments are only done if theTM_ADJUSTflag is set intm_info.flags.Tm_t* tmmake(time_t* clock)β returns a pointer to theTm_tstruct corresponding to the time pointed to byclock. Ifclockis NULL then the current time is used.time_t tmtime(Tm_t* tp, int west)β returns thetime_tvalue corresponding totp. IfwestisTM_LOCALZONEthen the time is relative to the local time zone, otherwisewestis the number of minutes west of UTC with daylight savings time taken into account.tp->tm_wday,tp->tm_ydayandtp->tm_isdstare ignored in the conversion.
The library routines use a table of date strings pointed to by char** tm_info.format. The indices in tm_info.format are fixed by category. tm_info.format may be changed to point to other tables according to local language and date conventions. The contents by index (showing the USA English values) are:
okay bestie, picking up right where we left off β the numbered list continues, and every single one is important, no cap π
0-11β 3-character abbreviated month names.12-23β full month names.24-30β 3-character abbreviated weekday names.31-37β full weekday names.38β thetmfmt()local time format used by the%Xfield.39β thetmfmt()local date format used by the%xfield.40β thetmfmt()format used if the format argument isNULLor empty.41-42β meridian names:AM,PM.43-46β UTC time zone names:UTC,GMT,UCT,CUT.47-50β daylight savings time suffix names:DST.51-54β suffixes to be ignored when matching strings intmfmt().55-61β time part names:second,hour,minute,day,week,month,year.62-65β hours of the day names:midnight,morning,noon,evening.66-68β relative day names:yesterday,today,tomorrow.69-71β past relative time references:last,ago,past.72-75β current relative time references:this,now,current.75-77β future relative time references:next,hence,coming.78-80β exact relative time references:exactly.81-84β noise words to be ignored:at,in,on.85-94β ordinal suffixes:st,nd,rd,th,th,th,th,th,th,th.95-104β digit names.105β thetmfmt()format equivalent forctime(3):'"%a%b%e%T%Y"'.106β thetmfmt()date(1)default format:'"%a%b%e%T%Z%Y"'.107β thetmfmt()date(1)international format:'"%a%b%e%T%z%Z%Y"'.108β thetmfmt()ls(1)recent date format:'"%b%e%H:%M"'.109β thetmfmt()ls(1)distant date format:'"%b%e%Y"'.110β thetmfmt()date(1)meridian date format:'"%I:%M:%S%p"'.111β theERAname.112βERAalternative for39.113βERAalternative for38.114βERAalternative for40.115β theERAyear.116-125β ordinal names:first,no second!,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth.126-128β final time references, as in the last in the list:final,ending,nth.
also, lowkey important: low level support functions and data are described in <tm.h>. iykyk π«Ά
β¨ EXAMPLES β¨
this example is iconic, bestie β run it and watch the magic happen π
#include <tm.h>
main() {
int i;
time_t t;
char buf[128];
struct {
char* date;
char* format;
} x[] = {
"now", "%i",
"2 months ago", "%C",
"this Wednesday noon", "%x %I:%M %p",
"last December 25", "%A",
0, 0
};
for (i = 0; x[i].date; i++) {
t = tmdate(x[i].date, (char*)0, (time_t*)0);
(void)tmfmt(buf, sizeof(buf), x[i].format, &t);
puts(buf);
}
}
it produces:
Fri Sep 30 12:10:14 USA EDT 1988
Fri Jul 1 00:00:00 EDT 1988
10/05/88 12:00 PM
Friday
π SEE ALSO π
go say hi to date(1), time(2), and ctime(3) β tell them I sent you π«Ά
π BUGS π
β οΈ okay so this is the part where you gotta be careful, bestie:
the C library static struct tm values may get clobbered by tm library routines, because the ctime(3) and localtime(3) routines typically return pointers to a single static struct tm area. also, tmdate() uses an internal international time zone name table that will probably always be incomplete. not me warning you with love π
source, version, and date? they're living their best life in the footer β this is the TM(3) page, periodt π
π¦ where this page comes from
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms