CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

如何获取系统时间

楼主nethanfei(银翼天使)2002-09-19 21:18:50 在 C/C++ / C语言 提问

请问各位高手,怎样在c++中获取实时系统时间?  
  在线等待!!!  
  最好能给出程序段! 问题点数:50、回复次数:3Top

1 楼Gpa(ChinaGPA.com)回复于 2002-09-19 21:21:22 得分 10

time_t   t;  
  t=time();  
  printf("%ld",t);  
  Top

2 楼bitjack(littlejack)回复于 2002-09-19 21:44:52 得分 40

#include   <time.h>  
  #include   <stdio.h>  
  #include   <sys/types.h>  
  #include   <sys/timeb.h>  
  #include   <string.h>  
   
  void   main()  
  {  
          char   tmpbuf[128],   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:\t\t\t\t%s\n",   tmpbuf   );  
          _strdate(   tmpbuf   );  
          printf(   "OS   date:\t\t\t\t%s\n",   tmpbuf   );  
   
          /*   Get   UNIX-style   time   and   display   as   number   and   string.   */  
          time(   &ltime   );  
          printf(   "Time   in   seconds   since   UTC   1/1/70:\t%ld\n",   ltime   );  
          printf(   "UNIX   time   and   date:\t\t\t%s",   ctime(   &ltime   )   );  
   
          /*   Display   UTC.   */  
          gmt   =   gmtime(   &ltime   );  
          printf(   "Coordinated   universal   time:\t\t%s",   asctime(   gmt   )   );  
   
          /*   Convert   to   time   structure   and   adjust   for   PM   if   necessary.   */  
          today   =   localtime(   &ltime   );  
          if(   today->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:\t\t\t\t%.8s   %s\n",  
                asctime(   today   )   +   11,   ampm   );  
   
          /*   Print   additional   time   information.   */  
          _ftime(   &tstruct   );  
          printf(   "Plus   milliseconds:\t\t\t%u\n",   tstruct.millitm   );  
          printf(   "Zone   difference   in   seconds   from   UTC:\t%u\n",    
                            tstruct.timezone   );  
          printf(   "Time   zone   name:\t\t\t\t%s\n",   _tzname[0]   );  
          printf(   "Daylight   savings:\t\t\t%s\n",    
                            tstruct.dstflag   ?   "YES"   :   "NO"   );  
   
          /*   Make   time   for   noon   on   Christmas,   1993.   */  
          if(   mktime(   &xmas   )   !=   (time_t)-1   )  
        printf(   "Christmas\t\t\t\t%s\n",   asctime(   &xmas   )   );  
   
          /*   Use   time   structure   to   build   a   customized   time   string.   */  
          today   =   localtime(   &ltime   );  
   
          /*   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   difference   in   seconds   from   UTC:         480  
  Time   zone   name:                                                    
  Daylight   savings:                                               YES  
  Christmas                                                               Sat   Dec   25   12:00:00   1993  
   
  Today   is   Tuesday,   day   03   of   May   in   the   year   1994.  
   
   
  我想这个程序肯定会满足你的,呵呵,无需再问了吧!Top

3 楼liubear()回复于 2002-09-20 01:25:12 得分 0

到msdn中去查吧,用法和demo都有.Top

相关问题

  • 如何获取系统时间改变?
  • 如何获取系统时间?
  • ASP.Net中如何获取系统时间?
  • 如何获取当前系统时间
  • 如何获取SQL SERVER2000的系统时间?
  • 如何获取系统时间并转化为字符串
  • 请问在汇编中如何获取系统时间
  • 如何获取服务器的系统时间呢?
  • 请教如何获取系统空闲时间
  • 如何用java.lang.Calendar类获取系统当前时间??!!

关键词

  • os
  • date
  • unix
  • tstruct
  • tmpbuf
  • ltime
  • today
  • utc
  • xmas
  • ampm

得分解答快速导航

  • 帖主:nethanfei
  • Gpa
  • bitjack

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo