日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

Windows下C/C++获取当前系统时间

發布時間:2025/3/12 c/c++ 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows下C/C++获取当前系统时间 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 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 plaincopy
  • 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 plaincopy
  • #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 plaincopy
  • #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 時區名稱,如果不能得到時區名稱則返回空字符。
    %% 百分號


    方案二:GetLocalTime( &sys );?

    優點:能精確到毫秒級;缺點:使用了windows API?


    [cpp] view plaincopy
  • #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 plaincopy
  • //此文件必須是c++文件??
  • #include<stdlib.h>??
  • #include<iostream>??
  • using?namespace?std;??
  • void?main()??
  • {??
  • ????system("time");??
  • }??
  • 方案四:time(null)

    將當前時間折算為秒級,再通過相應的時間換算即可
    [cpp] view plaincopy
  • //此文件必須是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 plaincopy
  • 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 plaincopy
  • 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

    總結

    以上是生活随笔為你收集整理的Windows下C/C++获取当前系统时间的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。