嵌入式新手如何设定Linux的时间函数_第1页
免费预览已结束,剩余7页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、嵌入式新手如何设定linux的时间函数 一、时光相关解释格林威治时光表示0时区的标及时间。其他时区的时光和此标及时间均有时光差。utc(universaltime coordinated)是世界协调时光,是格林威治时光在互联网中的表示办法二、标及时间函数1、time(取得本地目前的时光秒数)includetime_ttime(time_t *t);函数解释 此函数会返回从公元1970年1月1日的utc时光从0时0分0秒(epoch,linux纪元)算起到现在所经过的秒数。假如t并非空指针的话,此函数也会将返回值存到t指针所指的内存。返回值 胜利则返回秒数,失败则返回(time_t)-1)值,错

2、误缘由存于errno中。time_t定义为longint范例 includemian()longint seconds= time(time_t*)null);printf(“%dn”,seconds);执行 9.73e+082、gmtime(按照本地时光取得目前的utc时光)includestructtm*gmtime(const time_t*timep);函数解释 gmtime()将参数timep 所指的time_t 结构中的信息转换成真切世界所用法的时光日期表示办法,然后将结果由结构tm返回。结构tm的定义为structtminttm_sec;inttm_min;inttm_hour;

3、inttm_mday;inttm_mon;inttm_year;inttm_wday;inttm_yday;inttm_isdst;inttm_sec 代表目前秒数,正常范围为0-59,但允许至61秒inttm_min 代表目前分数,范围0-59inttm_hour 从午夜算起的时数,范围为0-23inttm_mday 目前月份的日数,范围01-31inttm_mon 代表目前月份,从一月算起,范围从0-11inttm_year 从1900年算起至今的年数inttm_wday 一星期的日数,从星期一算起,范围为0-6inttm_yday 从此年1月1日算起至今的天数,范围为0-365inttm

4、_isdst 日光节省时光的旗标此函数返回的时光日期未经时区转换,而是utc时光。返回值 返回结构tm代表目前utc 时光范例 includemain()char*wday="sun","mon","tue","wed","thu","fri","sat"time_ttimep;structtm *p;time( p=gmtime( printf(“%d%d%d”,(1900+p->tm_year),(1+p->tm_mon),p->tm

5、_mday);printf(“%s%d;%d;%dn”,wdayp->tm_wday, p->tm_hour, p->tm_min, p->tm_sec);执行 2000/10/28 sat 8:15:383、localtime(取得当地目前utc时光和日期) includestructtm *localtime(const time_t * timep);函数解释 localtime()将参数timep所指的time_t结构中的信息转换成真切世界所用法的时光日期表示办法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时光日期已经转换成当地

6、时区。返回值 返回结构tm代表目前的当地时光。范例 includemain()char*wday=“sun”,”mon”,”tue”,”wed”,”thu”,”fri”,”sat”;time_ttimep;structtm *p;time( p=localtime( printf(“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);printf(“%s%d:%d:%dn”,wdayp->tm_wday,p->tm_hour, p->tm_min, p->tm_sec);执行 2000/10/

7、28 sat 11:12:224、ctime(将时光和日期以字符串格式表示) includechar*ctime(const time_t *timep);函数解释 ctime()将参数timep所指的time_t结构中的信息转换成真切世界所用法的时光日期表示办法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时光,字符串格式为“wedjun 30 21 :49 :08 1993n”。若再调用相关的时光日期函数,此字符串可能会被破坏。返回值 返回一字符串表示目前当地的时光日期。范例 includemain()time_ttimep;time( printf(“%s”,ctime(&am

8、p;timep);执行 sat oct 28 10 : 12 : 05 20005、asctime(将时光和日期以字符串格式表示) includechar* asctime(const struct tm * timeptr);函数解释 asctime()将参数timeptr所指的tm结构中的信息转换成真切世界所用法的时光日期表示办法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时光,字符串格式为:“wedjun 30 21:49:08 1993n”返回值 若再调用相关的时光日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。附加解释 返回一字符串表示

9、目前当地的时光日期。范例 includemain()time_ttimep;time( printf(“%s”,asctime(gmtime(&timep);执行 sat oct 28 02:10:06 20006、mktime(将时光结构数据转换成经过的秒数) includetime_tmktime(strcut tm * timeptr);函数解释 mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的utc时光所经过的秒数。返回值 返回经过的秒数。范例 includemain()time_ttimep;strcuttm *

10、p;time( printf(“time(): %d n”,timep);p=localtime( timep= mktime(p);printf(“time()->localtime()->mktime():%dn”,timep);执行 time():974943297time()->localtime()->mktime():974943297设置系统时光标准c库中惟独猎取系统时光的api,好似还没有设置系统时光的api,本文将谈谈如何在linux和windows平台设置系统时光,最后给出一个与平台无关的设置系统时光的封闭函数。下设置系统时光:1linux下设置系统

11、时光的函数有好几个,先来看看最常用的stime()函数,这个函数只能精确到秒。define _svid_sourceincludeint stime(time_t *t);参数解释:t是以秒为单位的时光值,从gmt1970年1月1日0时0分0秒开头计算。返回值:胜利返回0,错误返回-1,errno错误码,efault表示传递的参数错误,如时光值是无效的值,eperm表示权限不够,注重惟独root用户才有修改系统时光的权限。假如要让一般程序修改系统时光,可以先切换到root用户操作,修改完成后,再切换到一般用户,或者用指令chmod+s给执行文件加上root用户的权限。2linux是如何管理时光

12、的?在系统启动时,linux操作系统将时光从中读到系统时光变量中,以后修改时光通过修改系统时光实现。为了保持系统时光与cmos时光的全都性,linux每隔11分钟会将系统时光写入cmos,同步时光。从这可以看出,猎取系统时光有两个途径,一种是从cmos中读,一种是从系统中读,但修改时光却惟独一种,即修改linux系统中的时光,而修改cmos中的时光是无效的,由于cmos中的时光会被定时重写掉。另外还有一点要注重,修改了系统时光并不是马上生效的,如果你修改了系统时光并马上关机,再开机的时候,时光还是本来的,由于修改的时光还没有来得及写入cmos中。3通过settimeofday()函数来设置系统

13、时光,这个函数设置的精度可以精确到微秒。includeintsettimeofday(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中没有实现。该函数返回值与st

14、ime()一样,同样也需要root权限。4设置cmos时光,其实它是通过rtc(real-timeclock)设备驱动来完成的,你可以用ioctl()函数来设置时光,固然也可以通过操作/dev/rtc设备文件,在此就不具体解释了。深圳、广州、郑州专业实训,联系郭教师qq754634522二、windows下设置系统时光1设置当前时区的时光includebool setlocaltime(constsystemtime* lpsystemtime);typedef struct _systemtime / st word wyear; word wmonth; /月份从1开头 word wday

15、ofweek; /setlocaltime()不用法这个参数 word wday; word whour; word wminute; word wsecond; word wmilliseconds; systemtime;函数胜利返回非零,失败返回零。注重要求调用进程必须有se_systemtime_name权限。2另外还有一个函数setsystemtime(),它的参数与setlocaltime一样,只不过以utc时区为基准的。bool setsystemtime(constsystemtime* lpsystemtime);二、 一个封装的设置系统时光的函数/设置胜利返回true,否则

16、返回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.t

17、m_sec; if(!setlocaltime(&st) return true; else return false; else /将structtm结构时光转换成gmt时光time_t struct time_t st; st = mktime( if(st=-1) return false; if(!stime(st) return true; else return false;endif 三、linux系统时光函数1、gettimeofday(取得目前的时光) includeincludeintgettimeofday ( struct timeval * tv , stru

18、ct timezone * tz )函数解释 gettimeofday()会把目前的时光有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。timeval结构定义为:structtimevallongtv_sec; longtv_usec;timezone结构定义为:structtimezoneinttz_minuteswest;inttz_dsttime;上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime所代表的状态如下dst_nonedst_usadst_austdst_wetdst_metdst_eetdst_candst_gbdst_rum

19、dst_turdst_austalt返回值 胜利则返回0,失败返回1,错误代码存于errno。附加解释efault指针tv和tz所指的内存空间超出存取权限。范例 includeincludemain()structtimeval tv;structtimezone tz;gettimeofday(&tv , printf(“tv_sec;%dn”, tv,.tv_sec) ;printf(“tv_usec;%dn”,tv.tv_usec);printf(“tz_minuteswest;%dn”, tz.tz_minuteswest);printf(“tz_dsttime,%dn”,tz

20、.tz_dsttime);执行 tv_sec: 974857339tv_usec:136996tz_minuteswest:-540tz_dsttime:02、settimeofday(设置目前时光)includeincludeintsettimeofday ( const struct timeval *tv,const struct timezone *tz);函数解释 settimeofday()会把目前时光设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。具体的解释请参考gettimeofday()。注重,惟独root权限才干用法此函数修改时光。返回值 胜利则返回0,失败返回

21、1,错误代码存于errno。错误代码 eperm 并非由root权限调用settimeofday(),权限不够。einval时区或某个数据是不正确的,无法正确设置时光。3、clock_gettime(猎取指定时钟的时光值)includeintclock_gettime( clockid_t clock_id,struct timespec * tp );解释:clock_id指定要猎取时光的时钟,按照posix的指定可以是以下值:clock_realtimesystemwiderealtime clock.clock_monotonicrepresentsmonotonic time. can

22、not be set.clock_process_cputime_idhighresolution per-process timer.clock_thread_cputime_idthread-specifictimer.clock_realtime_hrhighresolution version of clock_realtime.clock_monotonic_hrhighresolution version of clock_monotonic.structtimespec time_ttv_sec; long tv_nsec; ;4、adjtimex(tunekernel cloc

23、k)includeintadjtimex(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 re

24、turns 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

25、 set. it may contain a bitwise-or combinationof zero or more of the following bits:defineadj_offset 0x0001defineadj_frequency 0x0002defineadj_maxerror 0x0004defineadj_esterror 0x0008defineadj_status 0x0010defineadj_timeconst 0x0020defineadj_tick 0x4000defineadj_offset_singleshot 0x8001 ordinaryusers are restricted to a zero value for mode. only the supe-ruser may set anyparameters. returnvalueonsuccess, adjtimex()

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论