在TC2.0中如何作業系統時間?

來源:互聯網
上載者:User
有time()函數吧.獲得的時間可以儲存在time_t變數中.可以用localtime()函數轉換成tm結構。
(1)
  /*   
    * Get   the   OS/'s   current   time,   And   dispaly   it   on   the   screen.   
    * The   same   time,   exchange   it   to   minutes   Or   seconds   as   you   choose.   
    */   
  #include   <stdio.h>   
  #include   <stdlib.h>   
  #include   <time.h>   
  #include   <conio.h>   
  #include   <assert.h>   
   
  #define   NUMBERTOTIME_(x) NumberToTime_##x   
  #define   NUMBER_(x) TimeToNumber_##x   
  #define   SHOWTIME_(x) ShowTime_##x   
   
  int   TimeToNumber_M(int,   int,   int);   
  int   TimeToNumber_S(int,   int,   int);   
  void   NumberToTime_M(int,   int   *,   int   *);   
  void   NumberToTime_S(int,   int   *,   int   *,   int   *);   
  void   ShowTime_M(int);   
  void   ShowTime_S(int);   
   
   
  /*     
    *   A   main   loop   to   test   the   salient   features   of   the   time   class.   
    */   
   
  main()   
  {   
  int   number,c=/'//0/';   
  // time_t   ltime;   
  char   tmpbuf[9];   
  int   i,   Time[3];   
  char   *current,   *next;   
   
  // clrscr();   
   
  _tzset();   
   
  _strdate(   tmpbuf   );   
  printf(   /"OS/'s   current   date://t//t//t//t%s//n/",   tmpbuf   );   
   
  _strtime(   tmpbuf   );   
          printf(   /"OS/'s   current   time://t//t//t//t%s//n/",   tmpbuf   );   
            
  // time(<ime);   
  // printf(/"Time   in   seconds   since   UTC   1/1/70://t%ld//n/",   ltime   );   
   
  // Respectively   read   hours,   minutes,   seconds   from   the   tmpbuf     
   
  current   =   tmpbuf;   
  i   =   0;   
  while   (*current   !=   0)   
  {   
  if   (*current   ==   /':/'   ||   *current   ==   /'//')   
  {   
  current++;   
  }   
  next   =   current   +   1;   
  Time    =   (*current-48)   *   10   +   (*next-48); // change   the   CHAR   to   DECIMAL.   
  i++;   
  current   =   current+2; //   Because   the   format   of   time   is   hh:mm:ss   
  }   
   
  printf(/"Choose   the   displaying   style   on   the   screen://n/");   
  printf(/"M   -->   Show   time   in   MINUTES.//nS   -->   Show   time   in   SECONDS.//n//n/");   
  printf(/"Please   choose   the   show   style:/");   
  c   =   getchar();   
  switch   (c)   
  {   
  case   /'M/':   
  case   /'m/':   
  number   =   NUMBER_(M)(Time[0],Time[1],Time[2]);   
  printf(/"//nThe   CurrentTime   is   at:   %d   Min     /",number);   
  SHOWTIME_(M)(number);   
  break;   
  case   /'S/':   
  case   /'s/':   
  number   =   NUMBER_(S)(Time[0],Time[1],Time[2]);   
  printf(/"//nThe   CurrentTime   is   at:   %d   Sec     /",number);   
  SHOWTIME_(S)(number);   
  break;   
  default:   
  printf(/"What   you   inputed   is   not   /'M/'   Or   /'S/'!//n/");   
  }   
   
  /*     
    *   Assure   yourself   that   minutes   are   added   correctly.   
    */   
  switch   (c)   
  {   
  case   /'M/':   
  case   /'m/':   
  printf(/"After   32   minutes://n//n/");   
  for   (i=0;   i<10;   ++i)   
  {   
  number   +=   32; /*   Add   32   minutes   to   the   time.   */   
  SHOWTIME_(M)(number);   
  }   
  break;   
  case   /'S/':   
  case   /'s/':   
  printf(/"After   32   seconds://n//n/");   
  for   (i=0;   i<10;   ++i)   
  {   
  number   +=   32; /*   Add   32   minutes   to   the   time.   */   
  SHOWTIME_(S)(number);   
  }   
  default:   
  break;   
  }   
  getch();   
  printf(/"//n/");   
  }   
   
  /*   
    *   A   subroutine   to   convert   hours   And   minutes   into   an   
    *   integer   number.   This   does   not   checking   for   the   sake   
    *   of   brevity   (you/'ve   seen   it   done   before!).   
    *   
    */   
   
  int   TimeToNumber_M(int   Hours,   int   Minutes,   int   Seconds)   
  {   
  return   (Seconds*0   +   Minutes   +   Hours   *   60);   
  }   
   
  /*   
    *   A   subroutine   to   convert   hours   And   minutes   And   seconds     
    *   into   an   integer   number.     
    */   
   
  int   TimeToNumber_S(int   Hours,   int   Minutes,   int   Seconds)   
  {   
  return   (Seconds   +   Minutes*60   +   Hours*60*60);   
  }   
   
  /*   
    *   Convert   an   integer   to   hours   And   minutes.   
    */   
   
  void   NumberToTime_M(int   Number,   int   *Hours,   int   *Minutes)   
  {   
  *Minutes   =   Number   %   60;   
  *Hours   =   Number   /   60;   
  }   
   
  /*   
    *   Convert   an   integer   to   hours   And   minutes.   
    */   
   
  void   NumberToTime_S(int   Number,   int   *Hours,   int   *Minutes,   int   *Seconds)   
  {   
  int   temp;   
  *Hours   =   Number   /   (60*60);   
  temp   =   Number   %   (60*60);   
  *Minutes   =     temp   /   60;   
  *Seconds   =   temp   %   60;   
  }   
   
  /*   
    *   A   quickie   way   to   show   time   by   minutes.   
    */   
   
  void   ShowTime_M(int   Number)   
  {   
  int   Hours,   Minutes;   
  NumberToTime_M(Number,   &Hours,   &Minutes);   
  printf(/"%02d:%02d//n/",Hours,   Minutes);   
  }   
   
  /*   
    *   A   quickie   way   to   show   time   by   seconds.   
    */   
   
  void   ShowTime_S(int   Number)   
  {   
  int   Hours,   Minutes,   Seconds;   
  NumberToTime_S(Number,   &Hours,   &Minutes,   &Seconds);   
  printf(/"%02d:%02d:%02d//n/",Hours,   Minutes,Seconds);   
   
  }

(2)
  /*   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   <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(   <ime   );   
          printf(   /"Time   in   seconds   since   UTC   1/1/70://t%ld//n/",   ltime   );   
          printf(   /"UNIX   time   And   date://t//t//t%s/",   ctime(   <ime   )   );   
   
          /*   Display   UTC.   */   
          gmt   =   gmtime(   <ime   );   
          printf(   /"Coordinated   universal   time://t//t%s/",   asctime(   gmt   )   );   
   
          /*   Convert   to   time   structure   And   adjust   for   PM   if   necessary.   */   
          today   =   localtime(   <ime   );   
          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(   <ime   );   
          /*Use trftime 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.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.