用C获得当前系统时间.doc_第1页
用C获得当前系统时间.doc_第2页
用C获得当前系统时间.doc_第3页
用C获得当前系统时间.doc_第4页
全文预览已结束

下载本文档

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

文档简介

#include #include void main () time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( 007The current date/time is: %s, asctime (timeinfo) ); exit(0); = #include - 必须的时间函数头文件 time_t - 时间类型(time.h 定义) struct tm - 时间结构,time.h 定义如下: int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; time ( &rawtime ); - 获取时间,以秒计,从1970年1月一日起算,存于rawtime localtime ( &rawtime ); - 转为当地时间,tm 时间结构 asctime ()- 转为标准ASCII时间格式: 星期 月 日 时:分:秒 年 = 你要的格式可这样输出: printf ( %4d-%02d-%02d %02d:%02d:%02dn,1900+timeinfo-tm_year, 1+timeinfo-tm_mon, timeinfo-tm_mday,timeinfo-tm_hour,timeinfo-tm_min,timeinfo-tm_sec); 就是直接打印tm,tm_year 从1900年计算,所以要加1900, 月tm_mon,从0计算,所以要加1Example /* TIMES.C illustrates various time and date functions including: * time _ftime ctime asctime * localtime gmtime mktime _tzset * _strtime _strdate strftime * * Also the global variable: * _tzname */ #include #include #include #include #include void main() char tmpbuf128, ampm = AM; time_t ltime; struct _timeb tstruct; struct tm *today, *gmt, xmas = 0, 0, 12, 25, 11, 93 ; /* Set time zone from TZ environment variable. If TZ is not set, * the operating system is queried to obtain the default value * for the variable. */ _tzset(); /* Display operating system-style date and time. */ _strtime( tmpbuf ); printf( OS time:tttt%sn, tmpbuf ); _strdate( tmpbuf ); printf( OS date:tttt%sn, tmpbuf ); /* Get UNIX-style time and display as number and string. */ time( ime ); printf( Time in seconds since UTC 1/1/70:t%ldn, ltime ); printf( UNIX time and date:ttt%s, ctime( ime ) ); /* Display UTC. */ gmt = gmtime( ime ); printf( Coordinated universal time:tt%s, asctime( gmt ) ); /* Convert to time structure and adjust for PM if necessary. */ today = localtime( tm_hour 12 ) strcpy( ampm, PM ); today-tm_hour -= 12; if( today-tm_hour = 0 ) /* Adjust if midnight hour. */ today-tm_hour = 12; /* Note how pointer addition is used to skip the first 11 * characters and printf is used to trim off terminating * characters. */ printf( 12-hour time:tttt%.8s %sn, asctime( today ) + 11, ampm ); /* Print additional time information. */ _ftime( &tstruct ); printf( Plus milliseconds:ttt%un, litm ); printf( Zone difference in seconds from UTC:t%un, tstruct.timezone ); printf( Time zone name:tttt%sn, _tzname0 ); printf( Daylight savings:ttt%sn, tstruct.dstflag ? YES : NO ); /* Make time for noon on Christmas, 1993. */ if( mktime( &xmas ) != (time_t)-1 ) printf( Christmastttt%sn, asctime( &xmas ) ); /* Use time structure to build a customized time string. */ today = localtime( ime ); /* Use strftime to build a customized time string. */ strftime( tmpbuf, 128, Today is %A, day %d of %B in the year %Y.n, today ); printf( tmpbuf ); Output OS time: 21:51:03 OS date: 05/03/94 Time in seconds since UTC 1/1/70: 768027063 UNIX time and date: Tue May 03 21:51:03 1994 Coordinated universal time: Wed May 04 04:51:03 1994 12-hour time: 09:51:03 PM Plus milliseconds: 279 Zone

温馨提示

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

评论

0/150

提交评论