栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux下c库函数设置系统时间

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux下c库函数设置系统时间

设置时间,首先了解时间这一个结构体,在处理时间时,经常用到它:

struct tm   
{   
int tm_sec; //当前秒    
int tm_min; //当前分钟    
int tm_hour; //当前小时    
int tm_mday; //当前在本月中的天,如11月1日,则为1    
int tm_mon; //当前月,范围是0~11    
int tm_year; //当前年和1900的差值,如2006年则为36    
int tm_wday; //当前在本星期中的天,范围0~6    
int tm_yday; //当前在本年中的天,范围0~365    
int tm_isdst; //这个我也不清楚    
}   

直接上代码:

int getSystemTime()   
{   
time_t timer;   
struct tm* t_tm;   
time(&timer);   
t_tm = localtime(&timer);   
printf("today is %4d%02d%02d%02d%02d%02d/n", t_tm.tm_year+1900,   
t_tm.tm_mon+1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec);   
return 0;   
}   
  
  
int SetSystemTime(char *dt)  
{  
    struct rtc_time tm;  
    struct tm _tm;  
    struct timeval tv;  
    time_t timep;  
    sscanf(dt, "%d-%d-%d %d:%d:%d", &tm.tm_year,  
        &tm.tm_mon, &tm.tm_mday,&tm.tm_hour,  
        &tm.tm_min, &tm.tm_sec);  
    _tm.tm_sec = tm.tm_sec;  
    _tm.tm_min = tm.tm_min;  
    _tm.tm_hour = tm.tm_hour;  
    _tm.tm_mday = tm.tm_mday;  
    _tm.tm_mon = tm.tm_mon - 1;  
    _tm.tm_year = tm.tm_year - 1900;  
  
    timep = mktime(&_tm);  
    tv.tv_sec = timep;  
    tv.tv_usec = 0;  
    if(settimeofday (&tv, (struct timezone *) 0) < 0)  
    {  
    printf("Set system datatime error!/n");  
    return -1;  
    }  
    return 0;  
}  

看看其中的关键函数:settimeofday:

NAME
       gettimeofday, settimeofday - get / set time

SYNOPSIS
       #include

       int gettimeofday(struct timeval *tv, struct timezone *tz);

       int settimeofday(const struct timeval *tv, const struct timezone *tz);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       settimeofday(): _BSD_SOURCE

DEscriptION
       The  functions  gettimeofday()  and  settimeofday()  can  get and set the time as well as a timezone.  The tv argument is a struct timeval (as specified in
       ):

           struct timeval {
               time_t      tv_sec;    
               suseconds_t tv_usec;    
           };

       and gives the number of seconds and microseconds since the Epoch (see time(2)).  The tz argument is a struct timezone:

           struct timezone {
               int tz_minuteswest;    
               int tz_dsttime;        
           };

       If either tv or tz is NULL, the corresponding structure is not set or returned.  (However, compilation warnings will result if tv is NULL.)

       The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL.  (See NOTES below.)

       Under Linux, there are some peculiar "warp clock" semantics associated with the settimeofday() system call if on the very first call (after  booting)  that
       has a non-NULL tz argument, the tv argument is NULL and the tz_minuteswest field is nonzero.  (The tz_dsttime field should be zero for this case.)  In such
       a case it is assumed that the CMOS clock is on local time, and that it has to be incremented by this amount to get UTC system time.  No doubt it is  a  bad
       idea to use this feature.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/693292.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号