预备知识:
函数: ctime
功 能: 把日期和时间转换为字符串
用 法: char *ctime(const time_t *time);
NAME
asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, local-
time_r - transform date and time to broken-down time or ASCII
SYNOPSIS
#include
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);
DESCRIPTION
The ctime(), gmtime() and localtime() functions all take an argument of data type
time_t which represents calendar time. When interpreted as an absolute time
value, it represents the number of seconds elapsed since 00:00:00 on January 1,
1970, Coordinated Universal Time (UTC).
The asctime() and mktime() functions both take an argument representing broken-
down time which is a representation separated into year, month, day, etc.
Broken-down time is stored in the structure tm which is defined in as
follows:
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
The call ctime(t) is equivalent to asctime(localtime(t)). It converts the calen-
dar time t into a string of the form
"Wed Jun 30 21:49:08 1993\n"
The gmtime() function converts the calendar time timep to broken-down time repre-
sentation, expressed in Coordinated Universal Time (UTC). It may return NULL when
the year does not fit into an integer. The return value points to a statically
allocated struct which might be overwritten by subsequent calls to any of the
date and time functions. The gmtime_r() function does the same, but stores the
data in a user-supplied struct.
The localtime() function converts the calendar time timep to broken-time repre-
sentation, expressed relative to the user's specified time zone. The function
acts as if it called tzset(3) and sets the external variables tzname with infor-
mation about the current time zone, timezone with the difference between Coordi-
nated Universal Time (UTC) and local standard time in seconds, and daylight to a
non-zero value if daylight savings time rules apply during some part of the year.
The return value points to a statically allocated struct which might be overwrit-
ten by subsequent calls to any of the date and time functions. The localtime_r()
function does the same, but stores the data in a user-supplied struct. It need
not set tzname.
The asctime() function converts the broken-down time value tm into a string with
the same format as ctime(). The return value points to a statically allocated
string which might be overwritten by subsequent calls to any of the date and time
functions. The asctime_r() function does the same, but stores the string in a
user-supplied buffer of length at least 26.
The mktime() function converts a broken-down time structure, expressed as local
time, to calendar time representation. The function ignores the specified con-
tents of the structure members tm_wday and tm_yday and recomputes them from the
other information in the broken-down time structure. If structure members are
outside their legal interval, they will be normalized (so that, e.g., 40 October
is changed into 9 November). Calling mktime() also sets the external variable
tzname with information about the current time zone. If the specified broken-
down time cannot be represented as calendar time (seconds since the epoch),
mktime() returns a value of (time_t)(-1) and does not alter the tm_wday and
tm_yday members of the broken-down time structure.
程序清单:
#include <stdio.h>
#include <time.h>
int main(int argc,char **argv)
{
time_t tm;
time(&tm);
printf("Today's date and time:%sn",ctime(&tm));
return 0;
}
|
编译并运行源程序:
obe-240 eagle/test> gcc -o time3 time3.c
obe-240 eagle/test> ./time3
Today's date and time:Mon Dec 13 19:20:12 2010
阅读(1044) | 评论(1) | 转发(0) |