文章目录 1、sleep:延迟函数 2、time/localtime 3、示例:sleep + time + localtime 4、Linux时间调整
1、sleep:延迟函数
函数原型:unsigned int sleep ( unsigned int seconds) ;
功 能:延时
参 数:seconds: 秒,表示让程序暂停的时间(单位:秒)
返回值:当程序被其唤醒,返回剩余未暂停的时间。如果sleep函数被完全执行完毕, 返回值为0
2、time/localtime
# include <time.h> 函数原型:time_t time ( time_t * tloc) ;
参数:@tloc :一般传为NULL
返回值:成功返回秒钟数,失败返回- 1 == == == == == == == == == == == == == == == == == == == == == == == == == 函数原型:struct tm * localtime ( const time_t * timep) ;
参数:@timep : 秒钟数
返回值:成功返回一个指向tm结构体的指针,失败返回NULL struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon;
int tm_year; int tm_wday; int tm_yday; } ;
3、示例:sleep + time + localtime
# include <stdio.h>
# include <time.h>
# include <unistd.h>
int main ( ) { time_t ts; struct tm * tm; while ( 1 ) { if ( ( ts = time ( NULL ) ) == - 1 ) { printf ( "get time error" ) ; return - 1 ; } if ( ( tm = localtime ( & ts) ) == NULL ) { printf ( "get struct error" ) ; return - 1 ; } fprintf ( stdout , "%d-%02d-%02d %02d-%02d-%02d weekday=%d yearday=%d\n" , tm-> tm_year+ 1900 , tm-> tm_mon+ 1 , tm-> tm_mday, tm-> tm_hour, tm-> tm_min, tm-> tm_sec, tm-> tm_wday, \tm-> tm_yday) ; sleep ( 1 ) ; } return 0 ;
}
4、Linux时间调整