TM(3)
okay bestie, buckle up π this is TM(3) from the Library Functions Manual, and itβs all about the tm library β your literal time-conversion support bestie. π°οΈβ¨
π NAME π
tm β time conversion support. thatβs the whole name, and it ate. π
πΈ SYNOPSIS πΈ
#include <tm.h>
π« DESCRIPTION π«
The tm library supports conversion between string date specifications, time_t clock values and struct tm values. localtime() and gmtime() (see ctime(3)) are used to determine local time zone information. lowkey the whole time-zone situation depends on them. π
time_t values are the number of seconds since the epoch, Jan 1 00:00:00 UTC 1970, with leap seconds omitted. no cap. β³
The global variable int tm_info.flags contains flags that allow all programs using the library to 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 theleapoption 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, bestie π§
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. ifclockisNULLthen the current time is used. ποΈstruct tm* tmfix(struct tm* 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_secwere61then it would change to1andtp->tm_minwould be incremented by1, and so on.tp->tm_wday,tp->tm_ydayandtp->tm_isdstare not changed as these can be computed from the other fields. π§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. ifformatisNULLor empty then the stringtm_info.format[40]is used. ifclockisNULLthen the current time is used. a pointer to the end ofbuf(i.e., the terminating'\0') is returned. β¨
format is in the style of printf(3), where %field causes the corresponding fixed size field to be placed in buf, zero padded if necessary, and \c and \nnn sequences are interpreted as in the C language. otherwise invalid %field specifications and all other characters in format are copied into buf without change. 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.hβ abbreviated month name.Hβ 24-hour clock hour.iβ internationaldate(1)date that includes the time zone type name.Iβ 12-hour clock hour.jβ 1-offset Julian date.Jβ 0-offset Julian date.lβls(1) -ldate that lists recent dates withhh:mmand distant dates withyyyy.mβ month number.Mβ minutes.nβ newline character.pβ meridian (e.g.,AMorPM).rβ 12-hour time ashh:mm:ss meridian.Rβ 24-hour time ashh:mm.Sβ seconds.tβ tab character.Tβ 24-hour time ashh:mm:ss.Uβ week number with Sunday as the first day.wβ weekday number.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.Yβ 4-digit year.zβ time zone type name.Zβ time zone name.+flag,-flagβ temporarily (untiltmform()returns) sets (+) or clears (-) thetm_info.flagsflags specified byflag:lβTM_LEAPuβTM_UTC
#β number of seconds since the epoch.
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. ifclockisNULLthen the current time is used. adjustments are only done if theTM_ADJUSTflag is set intm_info.flags. π¦struct tm* tmmake(time_t* clock)β returns a pointer to thetmstruct corresponding to the time pointed to byclock. ifclockisNULLthen the current time is used. π§time_t tmtime(struct tm* tp, int west)β returns thetime_tvalue corresponding totp. ifwestisTM_LOCALZONEthentmis 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 tm_info.format table π¦
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:
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βtmform()local time format used by the%Xfield.39βtmform()local date format used by the%xfield.40βtmform()format used if theformatargument 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 intmform().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-85β noise words to be ignored:at,in,on.
Low level support functions and data are described in <tm.h>. π«Ά
π₯ EXAMPLES π₯
#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)tmform(buf, x[i].format, &t);
puts(buf);
}
}
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 π
date(1), time(2), ctime(3) β go read these too, bestie. π
π BUGS π
struct tm values may get clobbered by the tm library routines as the ctime(3) routines typically return pointers to a single static struct tm area. tmdate() uses an internal international time zone name table that will probably always be incomplete. yeah, itβs giving βknown chaos,β so be careful. π
okay byeee π source/version vibes: TM(3) from the Library Functions Manual, and the footer is literally just TM(3) with no date attached β ngl, thatβs the whole footer. 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