Windows下C/C++擷取當前系統時間__C++

來源:互聯網
上載者:User
Windows下CC擷取當前系統時間 方案一localtime 優點僅使用C標準庫缺點只能精確到秒級 方案二GetLocalTime sys   優點能精確到毫秒級缺點使用了windows API  方案三systemtime 方案四timenull 方案五CTime 如何在C中將filetime時間轉化為字串 filetime SYSTEMTIME st   char strTime128 sprintfstrTimed-d-d  dddstwYearstwMonthstwDaystwHourstwMinutestwSecond   如何在C中將filetime時間轉化為常規時間格式 Windows下C/C++擷取當前系統時間

【原文】http://blog.csdn.NET/dadalan/article/details/5771693

寫軟體時經常要用到擷取系統時間顯示到狀態列,這裡在前人的基礎上總結了一下幾種方案。 方案一:localtime() 優點:僅使用C標準庫;缺點:只能精確到秒級

time_t是定義在time.h中的一個類型,表示一個日曆時間,也就是從1970年1月1日0時0分0秒到此時的秒數,原型是:
 typedef long time_t;        /* time value */
可以看出time_t其實是一個長整型,由於長整型能表示的數值有限,因此它能表示的最遲時間是2038年1月18日19時14分07秒。

函數time可以擷取當前日曆時間時間,time的定義:
 time_t time(time_t *)

time_t (typedef __int64  time_t )只是一個長整型,不符合我們的使用習慣,需要轉換成本地時間,就要用到tm結構,time.h中結構tm的原型是:
[cpp] view plain copy struct tm {           int tm_sec;     /* seconds after the minute - [0,59] */           int tm_min;     /* minutes after the hour - [0,59] */           int tm_hour;    /* hours since midnight - [0,23] */           int tm_mday;    /* day of the month - [1,31] */           int tm_mon;     /* months since January - [0,11] */           int tm_year;    /* years since 1900 */           int tm_wday;    /* days since Sunday - [0,6] */           int tm_yday;    /* days since January 1 - [0,365] */           int tm_isdst;   /* daylight savings time flag */          };  

可以看出,這個機構定義了年、月、日、時、分、秒、星期、當年中的某一天、夏令時。可以用這個結構很方便的顯示時間。

用localtime擷取當前系統時間,該函數將一個time_t時間轉換成tm結構表示的時間,函數原型:
 struct tm * localtime(const time_t *)
使用gmtime函數擷取格林尼治時間,函數原型:
 struct tm * gmtime(const time_t *) 輸出方式1: [cpp] view plain copy #include <iostream>   #include <time.h>   using namespace std;   void dsptime(const struct tm *); //輸出時間。      int main(void)   {    time_t nowtime;    nowtime = time(NULL); //擷取日曆時間    cout << nowtime << endl;  //輸出nowtime       struct tm *local,*gm;    local=localtime(&nowtime);  //擷取當前系統時間    dsptime(local);     gm=gmtime(&nowtime);  //擷取格林尼治時間    dsptime(gm);         return 0;   }   void dsptime(const struct tm * ptm)   {    char *pxq[]={"日","一","二","三","四","五","六"};    cout << ptm->tm_year+1900 << "年" << ptm->tm_mon+1 << "月" << ptm->tm_mday << "日 " ;    cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " ;    cout << " 星期" <<pxq[ptm->tm_wday] << " 當年的第" << ptm->tm_yday << "天 " << endl;   }  

輸出方式2: [cpp] view plain copy #include <time.h>    #include <stdio.h>    int main( void )    {        time_t t = time(0);        char tmp[64];        strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );        puts( tmp );        return 0;    }  

C/C++在time.h中提供了一個自訂時間格式的函數strftime,函數原型:
 size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
參數說明:
 char *strDest:用來存放格式化後的字串緩衝,
 size_t maxsize:指定最多可以輸出的字元數,
 const char *format:格式化字串,
 const struct tm *timeptr:要轉換的時間。

可使用的格式化字串:
%a 星期幾的簡寫 
%A 星期幾的全稱 
%b 月分的簡寫 
%B 月份的全稱 
%c 標準的日期的時間串 
%C 年份的後兩位元字 
%d 十進位表示的每月的第幾天 
%D 月/天/年 
%e 在兩字元域中,十進位表示的每月的第幾天 
%F 年-月-日 
%g 年份的後兩位元字,使用基於周的年 
%G 年分,使用基於周的年 
%h 簡寫的月份名 
%H 24小時制的小時 
%I 12小時制的小時
%j 十進位表示的每年的第幾天 
%m 十進位表示的月份 
%M 十時製表示的分鐘數 
%n 新行符 
%p 本地的AM或PM的等價顯示 
%r 12小時的時間 
%R 顯示小時和分鐘:hh:mm 
%S 十進位的秒數 
%t 水平定位字元 
%T 顯示時分秒:hh:mm:ss 
%u 每周的第幾天,星期一為第一天 (值從0到6,星期一為0)
%U 第年的第幾周,把星期日做為第一天(值從0到53)
%V 每年的第幾周,使用基於周的年 
%w 十進位表示的星期幾(值從0到6,星期天為0)
%W 每年的第幾周,把星期一做為第一天(值從0到53) 
%x 標準的日期串 
%X 標準的時間串 
%y 不帶世紀的十進位年份(值從0到99)
%Y 帶世紀部分的十進位年份 
%z,%Z 時區名稱,如果不能得到時區名稱則返回Null 字元。
%% 百分比符號


方案二:GetLocalTime( &sys );  優點:能精確到毫秒級;缺點:使用了windows API 
[cpp] view plain copy #include <windows.h>    #include <stdio.h>    int main( void )    {    SYSTEMTIME sys;    GetLocalTime( &sys );    printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d/n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);    return 0;   }  
方案三:system("time"); 優點:利用系統函數,還能修改系統時間

[cpp] view plain copy //此檔案必須是c++檔案   #include<stdlib.h>   #include<iostream>   using namespace std;   void main()   {       system("time");   }  
方案四:time(null) 將目前時間折算為秒級,再通過相應的時間換算即可
[cpp] view plain copy //此檔案必須是c++檔案   #include<iostream>   #include<ctime>   using namespace std;   int main()   {   time_t now_time;   now_time = time(NULL);   cout<<now_time;   return 0;   }  
方案五:CTime 使用MFC裡面的CTime類,更加方便
第一種: [cpp] view plain copy CString str; //擷取系統時間         CTime tm; tm=CTime::GetCurrentTime();         str=tm.Format("%Y-%m-%d %H:%M:%S");  //主要是Y m d,H M S中間的串連符自己定義      MessageBox(str,NULL,MB_OK);  

第二種: [cpp] view plain copy SYSTEMTIME st;         CString strDate,strTime;         GetLocalTime(&st);         strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDay);         strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond); 

注釋:

如何在C++中將filetime時間轉化為字串。

filetime 轉化為systemtime



SYSTEMTIME st;   char strTime[128]; sprintf(strTime,"%d-%d-%d  %d:%d:%d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);  


參考:

如何在C++中將filetime時間轉化為常規時間格式。 http://bbs.csdn.net/topics/310015002

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.