码迷,mamicode.com
首页 > 系统相关 > 详细

Linux获取系统当前时间(精确到毫秒)

时间:2015-11-18 19:39:15      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

void sysLocalTime(void)
{
    time_t             timesec;
    struct tm         *t;
    
    
    time(×ec);
    t = localtime(×ec);
    
    printf("%d-%d-%d %d:%d:%d\n", 1900+t->tm_year, 1+t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
    
}

void sysUsecTime(void)
{
    struct timeval    tv;
    struct timezone tz;
    
    struct tm         *t;
    
    gettimeofday(&tv, &tz);
    printf("tv_sec:%ld\n",tv.tv_sec);
    printf("tv_usec:%ld\n",tv.tv_usec);
    printf("tz_minuteswest:%d\n",tz.tz_minuteswest);
    printf("tz_dsttime:%d\n",tz.tz_dsttime);
    
    t = localtime(&tv.tv_sec);
    printf("time_now:%d-%d-%d %d:%d:%d.%ld\n", 1900+t->tm_year, 1+t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec);
}

int main(void)
{
    sysLocalTime();
    
    sysUsecTime();
    
    return 0;
}

  

Linux获取系统当前时间(精确到毫秒)

标签:

原文地址:http://www.cnblogs.com/chars/p/4975460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!