




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Linux下时间分类:Linux编程2010-11-02 15:12167人阅读评论(0)收藏举报1.时间类型1) time_t是一个长整型,一般用来表示用1970年以来的秒数。2)struct timeval有两个成员,一个是秒,一个是微妙。structtimevallongtv_sec;longtv_usec;3) struct timespec有两个成员,一个是秒,一个是纳秒。structtimespectime_ttv_sec;longtv_nsec;4) struct tm是直观意义上的时间表示方法structtminttm_sec;inttm_min;inttm_hour;inttm_mday;inttm_mon;inttm_year;inttm_wday;inttm_yday;inttm_isdst;5)struct timeb/含时区和日光修正time_ttime;/从1970来经过的秒数unsigned shortmillitm;/毫秒shorttimezone;/时区shortdstflag;/为日光节约时间的修正值,如果为非0代表启用日光节约修正;6)struct timezoneint tz_minuteswest;/和格林尼治时间相差多少分int tz_dsttime;/日光节约时间的状态;日光节约时间:夏时制。2.时间函数参数都是以结构体的指针方式传入的,也就是说要取地址传进去1) char *asctime(const struct tm *timeptr)将时间和日期以字符串格式显示。2)clock_t clock(void)取得进程占用cpu的大约时间。3)char *ctime(const time_t *timep)将时间和日期以字符串格式显示。4) double difftime(time_t time1, time_t time0)计算时间time1和time0间的差距。5) int ftime(struct timeb *tp)取得目前的时间和日期。6) int gettimeofday(struct timeval *tv, struct timezone *tz)取得目前的时间。7)strcut tm *gmtime(const time_t *timep)time_t结构时间转tm结构时间,tm为UTC时区。8) struct tm *localtime(const time_t *timep) /秒数变成tm将time_t结构时间转tm结构时间,tm是当地时区。9)time_t mktime(struct tm *timeptr) /tm结构变成秒数将tm结构时间转换为time_t。10) int settimeofday(const struct timeval *tv, const struct timezone *tz)设置时间为tv,设置时区为tz。11) size_t strftime(char *s, size_t max, const char *format, const struct tm *tm)将参数tm的时间结构依照参数format所指定的字符串格式做转换,转换后的字符串将复制到参数s所指的字符串数组中,该字符串的最大长度为参数max所控制。12) time_t time(time_t *t)取得目前的时间,时间按照UTC。13) void tzset(void)从环境变量TZ取得目前当地的时间。3.延迟函数主要的延迟函数有:sleep(),nap(),napms(),usleep(),delay(),nanosleep(),select(), pselect().1) unsigned int sleep(unsigned int seconds)延时seconds秒。2) unsigned int nap( unsigned int milli_seconds );int napms( int milli_seconds );延时 milli_seconds 毫妙3) int usleep( useconds_t micro_seconds);unsigned int delay( unsigned int micro_seconds);延时 micro_seconds微妙4) int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)延时的时间为rqtp,如果nansleep被信号中断,且rmtp不为NULL,则rmtp指定的位置上包含的就是剩余时间。5) int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)如果将readfds, writefds, exceptfds置为NULL,timeout为非零,则延时timeout。5) int pselect(int maxfdp1, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec tsptr, const sigset_t *sigmask)如果将readfds, writefds, exceptfds置为NULL,tsptr为非零,则延时tsptr。struct tminttm_sec;/secondsaftertheminute-0,61inttm_min;/minutesafterthehour-0,59inttm_hour;/hoursaftermidnight-0,23inttm_mday;/dayofthemonth-1,31inttm_mon;/monthssinceJanuary-0,11inttm_year;/yearssince1900inttm_wday;/dayssinceSunday-0,6inttm_yday;/dayssinceJanuary1-0,365inttm_isdst;/DaylightSavingsTimeflaglong int tm_gmtoff;/Offsetfromgmtconst char *tm_zone;/Stringforzonename;struct timespectime_ttv_sec;/Thenumberofseconds since1970longtv_nsec;/Thenumberofnanosecondsstructtimevalinttv_sec;/secondsinttv_usec;/microseconds;struct itimerspecstructtimespecit_interval;/timerperiodstructtimespecit_value;/timerexpiration;structitimervalstructtimevalit_interval;/timerintervalstructtimevalit_value;/currentvalue;time_ttime(time_t*t);此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t并非空指针的话,此函数也会将返回值存到t指针所指的内存。structtm*localtime(consttime_t*timer);structtm*localtime_r(consttime_t*timer,structtm*result);将time_t*所指的秒数转换得到当前时间的tm结构time_tmktime(structtm*timeptr);用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0秒算起至今的UTC时间所经过的秒数。与localtime_r互为相反过程。char*ctime(consttime_t*timer);char*ctime_r(consttime_t*timer,char*buf);将time_t*所指的秒数格式化输出。size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);将参数tm的时间结构依照参数format所指定的字符串格式做转换,转换后的字符串将复制到参数s所指的字符串数组中,该字符串的最大长度为参数max所控制。可实现与ctime相反的操作。intclock_gettime(clockid_tclock_id,structtimespec*tp);int gettimeofday( struct timeval * when,void * not_used );获取当前时间。前者使用的数据结构中有一个纳秒级的参数,而后者是微秒,所以可以认为前者的精度比较高。intclock_settime(clockid_tid,conststructtimespec*tp);int settimeofday( const struct timeval *when,void *not_used );设置当前时间例1:typedef structunsigned short Year;/year;unsigned charMonth;/month;unsigned charDay;/day;unsigned charHour;/hour;unsigned charMinute;/minute;unsigned charSec;/second;unsigned charWeek;TS;/获取日期void TSGet(TS *ts) struct tm tmp_tm;time_t time_of_day;time_of_day = time(NULL);localtime_r(&time_of_day, &tmp_tm);ts-Year = tmp_tm.tm_year + 1900;ts-Month = tmp_tm.tm_mon + 1;ts-Day = tmp_tm.tm_mday;ts-Hour = tmp_tm.tm_hour;ts-Minute = tmp_tm.tm_min;ts-Sec = tmp_tm.tm_sec;ts-Week = tmp_tm.tm_wday;/设置时间void TSSet(TS ts) struct tm tmpsec;struct timespec rtime;struct timespec cur_time;tmpsec.tm_sec = ts.Sec;tmpsec.tm_min = ts.Minute;tmpsec.tm_hour = ts.Hour;tmpsec.tm_mday = ts.Day;tmpsec.tm_mon = ts.Month - 1;tmpsec.tm_year = ts.Year - 1900;rtime.tv_sec = mktime(&tmpsec);clock_gettime(CLOCK_REALTIME, &cur_time);rtime.tv_nsec = cur_time.tv_nsec;clock_settime(CLOCK_REALTIME, &rtime);例2:int main(int argc, char *argv)struct tm tmp_tm;time_t time_of_day;/1970/1/1 0:0:0至今的秒数struct timespec spec;char buf30;time_of_day = time(NULL);printf(time_of_day = %ld/n, time_of_day);localtime_r(&time_of_day, &tmp_tm);printf(%d-%d-%d, %d:%d:%d/n, tmp_tm.tm_year,tmp_tm.tm_mon,tmp_tm.tm_mday,tmp_tm.tm_hour,tmp_tm.tm_min,tmp_tm.tm_sec);spec.tv_sec = mktime(&tmp_tm);printf(spec = %ld/n, spec.tv_sec);ctime_r(&spec.tv_sec, buf);printf(%s/n,buf);结果:time_of_day = 1276169161110-5-10, 11:26:1spec = 1276169161Thu Jun 10 11:26:01 2010例3:void function()unsigned int i;sleep(3);for(i=0;i100;i+ )printf(i = %d/n,i);int main(int argc, char *argv)struct timeval tpsta
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- Feruloylacetyl-CoA-feruloylacetyl-coenzyme-A-生命科学试剂-MCE
- Ezutromid-Standard-生命科学试剂-MCE
- 农发行潍坊市高密市2025秋招笔试综合模拟题库及答案
- 农发行滁州市凤阳县2025秋招笔试英文行测高频题含答案
- 农发行秦皇岛市抚宁区2025秋招笔试性格测试题专练及答案
- 固态电池在电动汽车充电基础设施2025年市场渗透率预测报告
- 2025年汽车行业汽车玻璃市场应用与技术创新分析
- 农发行宜春市上高县2025秋招笔试EPI能力测试题专练及答案
- 2025年氢能重卡商业化运营技术创新与电力设备运输报告
- 农发行资阳市安岳县2025秋招笔试热点题型专练及答案
- GB/T 23902-2021无损检测超声检测超声衍射声时技术检测和评价方法
- 邀请函模板完整
- 2020新译林版高中英语选择性必修二全册课文及翻译(英汉对照)
- 大学物理第14章光的衍射课件
- 家长会 课件(共44张ppt) 九年级上学期
- 钻孔灌注桩施工安全控制培训教材课件
- 福建省莆田市各县区乡镇行政村村庄村名明细
- 大班幼儿随访电访记录表内有内容
- 干细胞精品课件
- 太阳能路灯说明书完整版
- 中国老龄化社会的潜藏价值(中英)
评论
0/150
提交评论