
struct timespec {
time_t tv_sec/*seconds*/
long tv_nsec/*nonoseconds*/
}
这个tv_nsec就是纳秒.
t=localtime(&tt)sprintf(year,"%d",t->tm_year+1900)
要加1900的。
给你看看我的代码,另外一种形式,非常简单,记得采纳给分。
time_t t
time(&t)
char time_str[256]={0}
struct tm* tp= localtime(&t)
strftime(time_str,100,"%Y-%m-%d-%H:%M:%S",tp)
打印这个字符串就是系统当前的时间。
struct tm { int tm_sec /* Seconds (0-60) */ int tm_min /* Minutes (0-59) */ int tm_hour /* Hours (0-23) */ int tm_mday /* Day of the month (1-31) */ int tm_mon /* Month (0-11) */ int tm_year /* Year - 1900 */ int tm_wday /* Day of the week (0-6, Sunday = 0) */ int tm_yday /* Day in the year (0-365, 1 Jan = 0) */ int tm_isdst /* Daylight saving time */ }这就里面的参数已经很详尽也很简单了
当然如果你非要用以上概述的方法来完成任务的其实也很简单
只需要将以上的内容填充到这个结构体然后再调用mktime函数就可以了
下面我给个简单的实现可以参考下,然后根据自己的实际情况再做些修改
void settime(unsigned char *buf,int buf_len){struct tm t int year=0memset(&t,0,sizeof(t)) memcpy(&year,buf,2)//前两个字节为年份year-=1900//struct sm结构中年份是以1900年开始计算的 t.tm_year=year t.tm_mon=(int)buf[2]-1 t.tm_mday=(int)buf[3] t.tm_hour=(int)buf[4] t.tm_min=(int)buf[5] t.tm_sec=(int)buf[6]if(mktime(&t) == -1) //设置时间perror("mktime")}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)