| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 3: Basic Library Functions Oracle Solaris 11 Information Library |
enable_extended_FILE_stdio(3C)
posix_spawnattr_getschedparam(3C)
posix_spawnattr_getschedpolicy(3C)
posix_spawnattr_getsigdefault(3C)
posix_spawnattr_getsigignore_np(3C)
posix_spawnattr_getsigmask(3C)
posix_spawnattr_setschedparam(3C)
posix_spawnattr_setschedpolicy(3C)
posix_spawnattr_setsigdefault(3C)
posix_spawnattr_setsigignore_np(3C)
posix_spawnattr_setsigmask(3C)
posix_spawn_file_actions_addclose(3C)
posix_spawn_file_actions_addclosefrom_np(3C)
posix_spawn_file_actions_adddup2(3C)
posix_spawn_file_actions_addopen(3C)
posix_spawn_file_actions_destroy(3C)
posix_spawn_file_actions_init(3C)
pthread_attr_getdetachstate(3C)
pthread_attr_getinheritsched(3C)
pthread_attr_getschedparam(3C)
pthread_attr_getschedpolicy(3C)
pthread_attr_setdetachstate(3C)
pthread_attr_setinheritsched(3C)
pthread_attr_setschedparam(3C)
pthread_attr_setschedpolicy(3C)
pthread_barrierattr_destroy(3C)
pthread_barrierattr_getpshared(3C)
pthread_barrierattr_setpshared(3C)
pthread_condattr_getpshared(3C)
pthread_condattr_setpshared(3C)
pthread_cond_reltimedwait_np(3C)
pthread_key_create_once_np(3C)
pthread_mutexattr_getprioceiling(3C)
pthread_mutexattr_getprotocol(3C)
pthread_mutexattr_getpshared(3C)
pthread_mutexattr_getrobust(3C)
pthread_mutexattr_setprioceiling(3C)
pthread_mutexattr_setprotocol(3C)
pthread_mutexattr_setpshared(3C)
pthread_mutexattr_setrobust(3C)
pthread_mutex_getprioceiling(3C)
pthread_mutex_reltimedlock_np(3C)
pthread_mutex_setprioceiling(3C)
pthread_rwlockattr_destroy(3C)
pthread_rwlockattr_getpshared(3C)
pthread_rwlockattr_setpshared(3C)
pthread_rwlock_reltimedrdlock_np(3C)
pthread_rwlock_reltimedwrlock_np(3C)
pthread_rwlock_timedrdlock(3C)
pthread_rwlock_timedwrlock(3C)
rctlblk_get_enforced_value(3C)
- convert date and time to string
#include <time.h>char *ctime(const time_t *clock);
struct tm *localtime(const time_t *clock);
struct tm *gmtime(const time_t *clock);
char *asctime(const struct tm *tm);
extern time_t timezone, altzone;extern int daylight;extern char *tzname[2];voidtzset(void);
char *ctime_r(const time_t *clock,char *buf,intbuflen);
struct tm *localtime_r(const time_t *restrictclock,struct tm *restrictres);
struct tm *gmtime_r(const time_t *restrictclock,struct tm *restrictres);
char *asctime_r(const struct tm *restricttm,char *restrictbuf,intbuflen);
cc [flag... ]file...-D_POSIX_PTHREAD_SEMANTICS [library... ]char *ctime_r(const time_t *clock,char *buf);
char *asctime_r(const struct tm *tm,char *buf);
Thectime() function converts the time pointed to byclock, representing thetime in seconds since the Epoch (00:00:00 UTC, January 1, 1970), tolocal time in the form of a 26-character string, as shown below.Time zone and daylight savings corrections are made before string generation. The fieldsare in constant width:
Fri Sep 13 00:00:00 1986\n\0
Thectime() function is equivalent to:
asctime(localtime(clock))
Thectime(),asctime(),gmtime(), andlocaltime() functions return values in one oftwo thread-specific data objects: a broken-down time structure and an array ofchar. Execution of any of the functions can overwrite the information returned ineither of these objects by any of the other functions executed bythe same thread.
Thectime_r() function has the same functionality asctime() except that thecaller must supply a bufferbuf with lengthbuflen to store theresult;buf must be at least 26 bytes. The standard-conformingctime_r() function doesnot take abuflen parameter.
Thelocaltime() andgmtime() functions return pointers totm structures (see below).Thelocaltime() function corrects for the main time zone and possible alternate(“daylight savings”) time zone; thegmtime() function converts directly to Coordinated UniversalTime (UTC), which is what the UNIX system uses internally.
Thelocaltime_r() andgmtime_r() functions have the same functionality aslocaltime() andgmtime() respectively, except that the caller must supply a bufferres tostore the result.
Theasctime() function converts atm structure to a 26-character string, asshown in the previous example, and returns a pointer to the string.
Theasctime_r() function has the same functionality asasctime() except that thecaller must supply a bufferbuf with lengthbuflen for the resultto be stored. Thebuf argument must be at least 26 bytes.The standard-conformingasctime_r() function does not take abuflen parameter. Theasctime_r() function returns a pointer tobuf upon success. In caseof failure,NULL is returned anderrno is set.
Declarations of all the functions and externals, and thetm structure, arein the <time.h> header. The members of thetm structure are:
int tm_sec; /* seconds after the minute — [0, 60] */ /* for leap seconds */int tm_min; /* minutes after the hour — [0, 59] */int tm_hour; /* hour since midnight — [0, 23] */int tm_mday; /* day of the month — [1, 31] */int tm_mon; /* months since January — [0, 11] */int tm_year; /* years since 1900 */int tm_wday; /* days since Sunday — [0, 6] */int tm_yday; /* days since January 1 — [0, 365] */int tm_isdst; /* flag for alternate daylight savings time */
The value oftm_isdst is positive if daylight savings time is ineffect, zero if daylight savings time is not in effect, and negativeif the information is not available. Previously, the value oftm_isdst wasdefined as non-zero if daylight savings was in effect.
The externaltime_t variablealtzone contains the difference, in seconds, between CoordinatedUniversal Time and the alternate time zone. The external variabletimezone containsthe difference, in seconds, between UTC and local standard time. The external variabledaylight indicates whether time should reflect daylight savings time. Bothtimezone andaltzone default to 0 (UTC). The external variabledaylight is non-zero ifan alternate time zone exists. The time zone names are contained in theexternal variabletzname, which by default is set to:
char *tzname[2] = { “GMT”, “ ”};
These functions know about the peculiarities of this conversion for various timeperiods for the U.S. (specifically, the years 1974, 1975, and 1987). Theystart handling the new daylight savings time starting with the first Sundayin April, 1987.
Thetzset() function uses the contents of the environment variableTZ tooverride the value of the different external variables. It is called byasctime() and can also be called by the user. IfTZ isnot specified or has an invalid setting,tzset() usesGMT0. Seeenviron(5) fora description of theTZ environment variable.
Starting and ending times are relative to the current local time zone.If the alternate time zone start and end dates and the timeare not provided, the days for the United States that year willbe used and the time will be 2 AM. If the startand end dates are provided but the time is not provided, the timewill be 2 AM. The effects oftzset() change the values ofthe external variablestimezone,altzone,daylight, andtzname.
Note that in most installations,TZ is set to the correct valueby default when the user logs on, using the local/etc/default/init file(seeTIMEZONE(4)).
Upon successful completion, thegmtime() andlocaltime() functions return a pointer toastruct tm. If an error is detected,gmtime() andlocaltime() return anull pointer.
Upon successful completion, thegmtime_r() andlocaltime_r() functions return the address ofthe structure pointed to by theres argument. If an error isdetected,gmtime_r() andlocaltime_r() return a null pointer and seterrno to indicatethe error.
Thectime_r() andasctime_r() functions will fail if:
The length of the buffer supplied by the caller is not large enough to store the result.
Thegmtime(),gmtime_r(),localtime(), andlocaltime_r() functions will fail if:
The result cannot be represented.
These functions do not support localized date and time formats. Thestrftime(3C)function can be used when localization is required.
Thelocaltime(),localtime_r(),gmtime(),gmtime_r(),ctime(), andctime_r() functions assume Gregorian dates.Times before the adoption of the Gregorian calendar will not match historical records.
Example 1 Examples of thetzset() function.
Thetzset() function scans the contents of the environment variable and assignsthe different fields to the respective variable. For example, the most completesetting for New Jersey in 1986 could be:
EST5EDT4,116/2:00:00,298/2:00:00
or simply
EST5EDT
An example of a southern hemisphere setting such as the Cook Islandscould be
KDT9:30KST10:00,63/5:00,302/20:00
In the longer version of the New Jersey example of TZ,tzname[0]is EST,timezone is set to 5*60*60,tzname[1] is EDT,altzone isset to 4*60*60, the starting date of the alternate time zone is the117th day at 2 AM, the ending date of the alternate timezone is the 299th day at 2 AM (using zero-based Julian days),anddaylight is set positive. Starting and ending times are relative tothe current local time zone. If the alternate time zone start and enddates and the time are not provided, the days for the UnitedStates that year will be used and the time will be 2AM. If the start and end dates are provided but the timeis not provided, the time will be 2 AM. The effects oftzset() are thus to change the values of the external variablestimezone,altzone,daylight, andtzname. Thectime(),localtime(),mktime(), andstrftime() functions also update theseexternal variables as if they had calledtzset() at the time specifiedby thetime_t orstruct tm value that they are converting.
Seeattributes(5) for descriptions of the following attributes:
|
Theasctime(),ctime(),gmtime(), andlocaltime() functions are safe to use inmultithread applications because they employ thread-specific data. However, their use is discouragedbecause standards do not require them to be thread-safe. Theasctime_r() andgmtime_r()functions are MT-Safe. Thectime_r(),localtime_r(), andtzset() functions are MT-Safe inmultithread applications, as long as no user-defined function directly modifies one of thefollowing variables:timezone,altzone,daylight, andtzname. These four variables are notMT-Safe to access. They are modified by thetzset() function in anMT-Safe manner. Themktime(),localtime_r(), andctime_r() functions calltzset().
time(2),Intro(3),getenv(3C),mktime(3C),printf(3C),putenv(3C),setlocale(3C),strftime(3C),TIMEZONE(4),attributes(5),environ(5),standards(5)
When compiling multithreaded programs, seeIntro(3).
The return values forasctime(),ctime(),gmtime(), andlocaltime() point to thread-specificdata whose content is overwritten by each call by the same thread.
Setting the time during the interval of change fromtimezone toaltzoneor vice versa can produce unpredictable results. The system administrator must changethe Julian start and end days annually.
Iftzset() has previously evaluated the timezone identified by the value oftheTZ environment variable,tzset() can reuse the previous settings ofthe external variablesaltzone,daylight,timezone, andtzname[] associated with that timezone.
Solaris 2.4 and earlier releases provided definitions of thectime_r(),localtime_r(),gmtime_r(),andasctime_r() functions as specified in POSIX.1c Draft 6. The final POSIX.1cstandard changed the interface forctime_r() andasctime_r(). Support for the Draft6 interface is provided for compatibility only and might not be supportedin future releases. New applications and libraries should use the standard-conforming interface.
For POSIX.1c-conforming applications, the_POSIX_PTHREAD_SEMANTICS and_REENTRANT flags are automatically turned onby defining the_POSIX_C_SOURCE flag with a value >= 199506L.
In Solaris 10,gmtime(),gmtime_r(),localtime(), andlocaltime_r() were updated to returna null pointer if an error is detected. This change was basedon the SUSv3 specification. Seestandards(5).
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |