time.h
Standard C library with time and date related functions
Functions:
asctime Convert tm structure to string
clock Return number of clock ticks since process start
ctime Convert time_t value to string
difftime Return difference between two times
gmtime Convert time_t value to tm structure as UTC time
localtime Convert time_t value to tm structure as local time
mktime Convert tm structure to time_t value
time Get current time
time_t time ( time_t * timer );
Get current time.
Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC from the system clock.
Parameters.
timer
Location where to store the retrieved value. If this is NULL the value is not stored. But it is still returned by the function.
time_t is generally defined by default to long.
Return Value.
Elapsed time in seconds, as described.
Portability.
Defined in ANSI-C.
Example.
/* time example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t seconds;
seconds = time (NULL);
printf ("%ld hours since January 1, 1970", seconds/3600);
return 0;
}
Output:
266344 hours since January 1, 1970
/* ctime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) );
return 0;
}
ysol用的ctime只是一个将time_t类型的时间变量转成时间字符串,并不是类
如果想获取各种时间信息如年、月、日等,那么可以用localtime函数将time_t类型转成tm结构,tm结构中有详细的时间信息