嵌入式新手如何设定Linux的时间函数

发布时间:2014-6-5 16:47    发布者:edu118gct
关键词: Linux , 时间
一、时间相关说明
格林威治时间表示0时区的标准时间。其他时区的时间和此标准时间均有时间差。UTC(UniversalTime Coordinated)是世界协调时间,是格林威治时间在互联网中的表示方法
二、标准C语言时间函数
1、time(取得本地目前的时间秒数)
#include
time_ttime(time_t *t);
函数说明  此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒(Epoch,linux纪元)算起到现在所经过的秒数。如果t并非空指针的话,此函数也会将返回值存到t指针所指的内存。
返回值  成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
time_t定义为longint
范例  #include
mian()
{
longint seconds= time((time_t*)NULL);
printf(“%d\n”,seconds);
}
执行  9.73E+08
2、gmtime(根据本地时间取得目前的UTC时间)
#include
structtm*gmtime(const time_t*timep);
函数说明  gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义为
structtm
{
inttm_sec;
inttm_min;
inttm_hour;
inttm_mday;
inttm_mon;
inttm_year;
inttm_wday;
inttm_yday;
inttm_isdst;
};
inttm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
inttm_min 代表目前分数,范围0-59
inttm_hour 从午夜算起的时数,范围为0-23
inttm_mday 目前月份的日数,范围01-31
inttm_mon 代表目前月份,从一月算起,范围从0-11
inttm_year 从1900年算起至今的年数
inttm_wday 一星期的日数,从星期一算起,范围为0-6
inttm_yday 从今年1月1日算起至今的天数,范围为0-365
inttm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值  返回结构tm代表目前UTC 时间
范例  #include
main(){
char*wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_ttimep;
structtm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
printf(“%s%d;%d;%d\n”,wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
执行  2000/10/28 Sat 8:15:38
3、localtime(取得当地目前UTC时间和日期)
#include
structtm *localtime(const time_t * timep);
函数说明  localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。
返回值  返回结构tm代表目前的当地时间。
范例  #include
main(){
char*wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_ttimep;
structtm *p;
time(&timep);
p=localtime(&timep);
printf(“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf(“%s%d:%d:%d\n”,wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
执行  2000/10/28 Sat 11:12:22
4、ctime(将时间和日期以字符串格式表示)
#include
char*ctime(const time_t *timep);
函数说明  ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为“WedJun 30 21 :49 :08 1993\n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
返回值  返回一字符串表示目前当地的时间日期。
范例  #include
main()
{
time_ttimep;
time(&timep);
printf(“%s”,ctime(&timep));
}
执行  Sat Oct 28 10 : 12 : 05 2000
5、asctime(将时间和日期以字符串格式表示)
#include
char* asctime(const struct tm * timeptr);
函数说明  asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“WedJun 30 21:49:08 1993\n”
返回值  若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
附加说明  返回一字符串表示目前当地的时间日期。
范例  #include
main()
{
time_ttimep;
time(&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
执行  Sat Oct 28 02:10:06 2000
6、mktime(将时间结构数据转换成经过的秒数)
#include
time_tmktime(strcut tm * timeptr);
函数说明  mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
返回值  返回经过的秒数。
范例
#include
main()
{
time_ttimep;
strcuttm *p;
time(&timep);
printf(“time(): %d \n”,timep);
p=localtime(&timep);
timep= mktime(p);
printf(“time()->localtime()->mktime():%d\n”,timep);
}
执行  time():974943297
time()->localtime()->mktime():974943297
设置系统时间
标准C库中只有获取系统时间的API,好像还没有设置系统时间的API,本文将谈谈如何在linux和windows平台设置系统时间,最后给出一个与平台无关的设置系统时间的封闭函数。
Linux下设置系统时间:
1.Linux下设置系统时间的函数有好几个,先来看看最常用的stime()函数,这个函数只能精确到秒。
#define _SVID_SOURCE
#include
int stime(time_t *t);
参数说明:
t是以秒为单位的时间值,从GMT1970年1月1日0时0分0秒开始计算。
返回值:
成功返回0,错误返回-1,errno错误码,EFAULT表示传递的参数错误,如时间值是无效的值,EPERM表示权限不够,注意只有root用户才有修改系统时间的权限。如果要让普通程序修改系统时间,可以先切换到root用户操作,修改完成后,再切换到普通用户,或者用命令chmod+s给执行文件加上root用户的权限。
2.linux是如何管理时间的?
在系统启动时,Linux操作系统将时间从CMOS中读到系统时间变量中,以后修改时间通过修改系统时间实现。为了保持系统时间与CMOS时间的一致性,Linux每隔11分钟会将系统时间写入CMOS,同步时间。从这可以看出,获取系统时间有两个途径,一种是从CMOS中读,一种是从系统中读,但修改时间却只有一种,即修改linux系统中的时间,而修改CMOS中的时间是无效的,因为CMOS中的时间会被定时重写掉。另外还有一点要注意,修改了系统时间并不是马上生效的,假如你修改了系统时间并马上关机,再开机的时候,时间还是原来的,因为修改的时间还没有来得及写入CMOS中。
3.通过settimeofday()函数来设置系统时间,这个函数设置的精度可以精确到微秒。
#include
intsettimeofday(const struct timeval *tv , const struct timezone *tz);
struct timeval {
    time_t      tv_sec;   
    suseconds_t tv_usec;   
};
struct timezone {
    inttz_minuteswest;   
    inttz_dsttime;        
};
tz参数为时区,时区结构中tz_dsttime在linux中不支持,应该置为0,通常将参数tz设置为NULL,表示使用当前系统的时区。该函数是glib中的,但在mingw中没有实现。
该函数返回值与stime()一样,同样也需要root权限。
4.设置CMOS时间,其实它是通过RTC(Real-timeclock)设备驱动来完成的,你可以用ioctl()函数来设置时间,当然也可以通过操作/dev/rtc设备文件,在此就不详细说明了。深圳、广州、郑州专业嵌入式实训,联系郭老师QQ754634522
二、windows下设置系统时间
1.设置当前时区的时间
#include
BOOL SetLocalTime(constSYSTEMTIME* lpSystemTime);
typedef struct _SYSTEMTIME{  // st
    WORD wYear;
    WORD wMonth; //月份从1开始
    WORD wDayOfWeek; //SetLocalTime()不使用这个参数
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME;
函数成功返回非零,失败返回零。注意要求调用进程必需有SE_SYSTEMTIME_NAME权限。
2.另外还有一个函数SetSystemTime(),它的参数与SetLocalTime一样,只不过以UTC时区为基准的。
BOOL SetSystemTime(constSYSTEMTIME* lpSystemTime);
二、 一个封装的设置系统时间的函数
//设置成功返回true,否则返回false
       bool set_local_time(struct tm& t)
{
#ifdef_WIN32
       SYSTEMTIME st;
       memset(&st, 0,sizeof(SYSTEMTIME));
       st.wYear = t.tm_year + 1970; //注意structtm结构中的年是从1970年开始的计数
       st.wMonth = t.tm_mon + 1; //注意structtm结构中的月份是从0开始的
       st.wDay = t.tm_mday;
       st.wHour = t.tm_hour;
       st.wMinute = t.tm_min;
       st.wSecond = t.tm_sec;
       if(!SetLocalTime(&st))
              return true;
              else
                     return false;
       #else
              //将structtm结构时间转换成GMT时间time_t
              struct time_t st;
              st = mktime(&t);
              if(st==-1)
                     return false;
              if(!stime(st))
                     return true;
              else
                     return false;
#endif
}

三、linux系统时间函数
1、gettimeofday(取得目前的时间)
#include
#include
intgettimeofday ( struct timeval * tv , struct timezone * tz )
函数说明  gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeval结构定义为:
structtimeval{
longtv_sec;   
longtv_usec;
};
timezone结构定义为:
structtimezone{
inttz_minuteswest;
inttz_dsttime;
};
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime所代表的状态如下
DST_NONE
DST_USA
DST_AUST
DST_WET
DST_MET
DST_EET
DST_CAN
DST_GB
DST_RUM
DST_TUR
DST_AUSTALT
返回值  成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
范例  #include
#include
main(){
structtimeval tv;
structtimezone tz;
gettimeofday(&tv , &tz);
printf(“tv_sec;%d\n”, tv,.tv_sec) ;
printf(“tv_usec;%d\n”,tv.tv_usec);
printf(“tz_minuteswest;%d\n”, tz.tz_minuteswest);
printf(“tz_dsttime,%d\n”,tz.tz_dsttime);
}
执行  tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0

2、settimeofday(设置目前时间)
#include
#include
intsettimeofday ( const struct timeval *tv,const struct timezone *tz);

函数说明  settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。

返回值  成功则返回0,失败返回-1,错误代码存于errno。
错误代码  EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL时区或某个数据是不正确的,无法正确设置时间。
3、clock_gettime(获取指定时钟的时间值)
#include
intclock_gettime( clockid_t clock_id,struct timespec * tp );
说明:clock_id指定要获取时间的时钟,根据Posix的指定可以是以下值:
CLOCK_REALTIME
Systemwiderealtime clock.

CLOCK_MONOTONIC
Representsmonotonic time. Cannot be set.

CLOCK_PROCESS_CPUTIME_ID
Highresolution per-process timer.

CLOCK_THREAD_CPUTIME_ID
Thread-specifictimer.

CLOCK_REALTIME_HR
Highresolution version of CLOCK_REALTIME.

CLOCK_MONOTONIC_HR
Highresolution version of CLOCK_MONOTONIC.

structtimespec {
time_ttv_sec;      
long  tv_nsec;      
};

4、adjtimex(tunekernel clock)
#include
intadjtimex(struct timex *buf);
说明:
Linux  uses  David L. Mills' clock adjustmentalgorithm (see RFC 1305).The system call adjtimex() reads and optionally setsadjustment parame-ters  for  this  algorithm.   It  takes a pointer to a timexstructure,updates kernel parameters from  field  values,  and  returns  the  same structure  with  current kernelvalues.  This structure isdeclared as follows:
structtimex {
intmodes;         
longoffset;        
longfreq;         
longmaxerror;      
longesterror;      
intstatus;         
longconstant;      
longprecision;     
longtolerance;     
structtimeval time;
longtick;         
};
Themodes field determines which parameters, if any, to  set.   It  may contain a bitwise-or combinationof zero or more of the following bits:

#defineADJ_OFFSET            0x0001
#defineADJ_FREQUENCY         0x0002
#defineADJ_MAXERROR          0x0004
#defineADJ_ESTERROR          0x0008
#defineADJ_STATUS            0x0010
#defineADJ_TIMECONST         0x0020
#defineADJ_TICK              0x4000
#defineADJ_OFFSET_SINGLESHOT 0x8001  
Ordinaryusers are restricted to a zero value for mode.  Only the supe-ruser may set anyparameters.
RETURNVALUE
Onsuccess, adjtimex() returns the clock state:
#defineTIME_OK   0
#defineTIME_INS  1
#defineTIME_DEL  2
#defineTIME_OOP  3
#defineTIME_WAIT 4
#defineTIME_BAD  5  
Onfailure, adjtimex() returns -1 and sets errno.
ERRORS
EFAULT
bufdoes not point to writable memory.
EINVAL
Anattempt is made to set buf.offset to a value outside the range -131071 to +131071,or to set buf.status to a value other than those listed above, or to setbuf.tick to a value outside the range 900000/HZ to 1100000/HZ, where HZ is thesystem  timer interruptfrequency.
EPERM
buf.modeis non-zero and the caller does not have sufficient privilege.Under Linux theCAP_SYS_TIME capability is required.
CONFORMINGTO
adjtimex()is Linux specific and should not be used in programs intended to be portable.See adjtime(3) for a more portable, but less flexible, method of adjusting thesystem clock.更多嵌入式学习详情联系郭老师QQ754634522


本文地址:https://www.eechina.com/thread-129873-1-1.html     【打印本页】

本站部分文章为转载或网友发布,目的在于传递和分享信息,并不代表本网赞同其观点和对其真实性负责;文章版权归原作者及原出处所有,如涉及作品内容、版权和其它问题,我们将根据著作权人的要求,第一时间更正或删除。
您需要登录后才可以发表评论 登录 | 立即注册

厂商推荐

相关在线工具

相关视频

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表