1. 程式人生 > >c++ 如何獲取系統時間

c++ 如何獲取系統時間

c++ 如何獲取系統時間 2008-04-28 15:34

//方案— 優點:僅使用C標準庫;缺點:只能精確到秒級
#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;
}
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
根據格式字串生成字串。
struct tm *localtime(const time_t *timer);
取得當地時間,localtime獲取的結果由結構tm返回
返回的字串可以依下列的格式而定:
%a 星期幾的縮寫。Eg:Tue
%A 星期幾的全名。 Eg: Tuesday
%b 月份名稱的縮寫。
%B 月份名稱的全名。
%c 本地端日期時間較佳表示字串。
%d 用數字表示本月的第幾天 (範圍為 00 至 31)。日期
%H 用 24 小時制數字表示小時數 (範圍為 00 至 23)。
%I 用 12 小時制數字表示小時數 (範圍為 01 至 12)。
%j 以數字表示當年度的第幾天 (範圍為 001 至 366)。
%m 月份的數字 (範圍由 1 至 12)。
%M 分鐘。
%p 以 ''AM'' 或 ''PM'' 表示本地端時間。
%S 秒數。
%U 數字表示為本年度的第幾周,第一個星期由第一個週日開始。
%W 數字表示為本年度的第幾周,第一個星期由第一個週一開始。
%w 用數字表示本週的第幾天 ( 0 為週日)。
%x 不含時間的日期表示法。
%X 不含日期的時間表示法。 Eg: 15:26:30
%y 二位數字表示年份 (範圍由 00 至 99)。
%Y 完整的年份數字表示,即四位數。 Eg:2008
%Z(%z) 時區或名稱縮寫。Eg:中國標準時間
%% % 字元。

//方案二 優點:能精確到毫秒級;缺點:使用了windows API
#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;
}

//方案三,優點:利用系統函式,還能修改系統時間
//此檔案必須是c++

檔案
#include<stdlib.h>
#include<iostream>
using namespace std;
void main()
{
system("time");
}

//方案四,將當前時間折算為秒級,再通過相應的時間換算即可
//此檔案必須是c++檔案
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
time_t now_time;
now_time = time(NULL);
cout<<now_time;
return 0;
}

1,時間的獲取: 通過time()函式來獲得日曆時間(Calendar Time),其原型為:time_t time(time_t * timer); #include "stdafx.h" #include "time.h" #include "stdio.h" #include "stdlib.h" int main(void) { struct tm t;            //定義tm時間結構,用來儲存時間格式的資料資訊 time_t t_of_day;     //定義time_t時間結構 t.tm_year=2006-1900;//以1900年為標準計算時間 t.tm_mon=6;                 //
為結構體成員賦值 t.tm_mday=1; t.tm_hour=0; t.tm_min=0; t.tm_sec=1; t.tm_isdst=0; t_of_day=mktime(&t); // 使用mktime()函式將用tm結構表示的時間轉化為日曆時間:time_t型變數。其函式原型如下:time_t mktime(struct tm * timeptr);ctime()函式(引數為time_t結構)將時間以固定的格式顯示出來,返回值是char*型的字串。 return 0; } 2,時間的儲存,通過預定義的兩種結構來儲存: 1,日曆時間(Calendar Time)是通過time_t資料型別來表示的,用time_t表示的時間(日曆時間)是從一個時間點(例如:1970年1月1日0時0分0秒)到此時的秒數。在time.h中,我們也可以看到time_t是一個長整型數: #ifndef _TIME_T_DEFINED typedef long time_t;         /* 時間值 */ #define _TIME_T_DEFINED      /* 避免重複定義 time_t */ #endif 2,在標準C/C++中,我們可通過tm結構來獲得日期和時間,tm結構在time.h中的定義如下: struct tm {         int tm_sec;     /* 秒 – 取值區間為[0,59] */         int tm_min;     /* 分 - 取值區間為[0,59] */         int tm_hour;    /* 時 - 取值區間為[0,23] */         int tm_mday;    /* 一個月中的日期 - 取值區間為[1,31] */         int tm_mon;     /* 月份(從一月開始,0代表一月) - 取值區間為[0,11] */         int tm_year;    /* 年份,其值等於實際年份減去1900 */         int tm_wday;    /* 星期 – 取值區間為[0,6],其中0代表星期天,1代表星期一,以此類推 */         int tm_yday;    /* 從每年的1月1日開始的天數 – 取值區間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */         int tm_isdst;   /* 夏令時識別符號,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不瞭解情況時,tm_isdst()為負。*/         }; 3,時間的顯示: time.h 標頭檔案中提供了asctime()函式(引數為tm結構指標)和ctime()函式(引數為time_t結構)將時間以固定的格式顯示出來,兩者的返回值 都是char*型的字串。返回的時間格式為:星期幾 月份 日期 時:分:秒 年/n/0;time.h還提供了兩種不同的函式將日曆時間(一個用time_t表示的整數)轉換為我們平時看到的把年月日時分秒分開顯示的時間格式 tm: struct tm * gmtime(const time_t *timer); gmtime()函式是將日曆時間轉化為世界標準時間(即格林尼治時間),並返回一個tm結構體來儲存這個時間 struct tm * localtime(const time_t * timer);localtime()函式是將日曆時間轉化為本地時間 #include <stdafx.h> #include <time.h> #include <stdio.h> #include <stdlib.h> int main(void) {        struct tm *local,*ptr; //定義tm結構指標儲存時間資訊        time_t t;                       //時間結構或者物件        t=time(NULL);                     //獲取當前系統的日曆時間        //通過time()函式來獲得日曆時間(Calendar Time),        //其原型為:time_t time(time_t * timer);        local=localtime(&t);//localtime()函式是將日曆時間轉化為本地時間        printf("Local hour is: %d/n",local->tm_hour);//輸出tm結構體的時間成員        printf("UTC hour is: %d/n",local->tm_hour);        //local=gmtime(&t);        //gmtime()函式是將日曆時間轉化為世界標準時間(即格林尼治時間),        //並返回一個tm結構體來儲存這個時間        ptr=gmtime(&t);//將日曆時間轉化為世界標準時間        printf("The UTC time is %s/n",asctime(ptr)); //格式化輸出世界標準時間        printf("The local time is %s/n",ctime(&t));//輸出本地時間        /*asctime()函式(引數為tm結構指標)和ctime()函式(引數為time_t結構)將時間以固定的格式顯示出來,兩者的返回值都是char*型的字串。返回的時間格式為:星期幾 月份 日期 時:分:秒 年/n/0 */        return 0; } 4,時間差的計算: 所用函式:C/C++中的計時函式是clock(),而與其相關的資料型別是clock_t。在MSDN中對clock函式定義如下: clock_t clock( void );函式返回從“開啟這個程式程序”到“程式中呼叫clock()函式”時之間的CPU時鐘計時單元(clock tick)數,clock_t是一個長整形數,儲存時間的資料型別。在time.h檔案中,還定義了一個常量CLOCKS_PER_SEC,它用來表示一 秒鐘會有多少個時鐘計時單元,其定義如下: #define CLOCKS_PER_SEC ((clock_t)1000) 每 過千分之一秒(1毫秒),呼叫clock()函式返回的值就加1,時鐘計時單元的長度為1毫秒,那麼計時的精度也為1毫秒,那麼我們可不可以通過改變 CLOCKS_PER_SEC的定義,通過把它定義的大一些,從而使計時精度更高呢?這樣是不行的。在標準C/C++中,最小的計時單位是一毫秒。 double difftime(time_t time1, time_t time0);這個函式來計算時間差。 #include "stdafx.h" #include "time.h" #include "stdio.h" #include "stdlib.h" int main(void) {     time_t c_start,t_start, c_end,t_end;       c_start = clock();        t_start = time(NULL) ;     system("pause") ;        c_end = clock();     t_end = time(NULL) ;     printf("The pause used %f ms by time()./n",difftime(c_end,c_start)) ;        printf("The pause used %f s by clock()./n",difftime(t_end,t_start)) ;     system("pause");     return 0; } 5,時間的其他用途 用作隨機數的種子,由於時間獲得的實際上是一個double型別的長整數,通過time(NULL)函式獲得,作為srand(time(NULL))的種子產生隨機數比較好。 #include "stdafx.h" #include "time.h" #include "stdio.h" #include "stdlib.h" int main(void) {        srand(time(NULL));        //設定種子,如果將這個函式註釋掉,每次執行程式得到的隨機數十相同的        for(int i=0;i<100;i++)        {               printf("%d/t",rand());        }     system("pause");     return 0;

相關推薦

linux c獲取系統時間

#include<iostream> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <unistd.h> int main(){ struc

C# 獲取系統時間時間格式

--DateTime 數字型  System.DateTime currentTime=new System.DateTime();  取當前年月日時分秒      currentTime=System.DateTime.Now;  取當前年     int 年=curren

Windows下C++獲取系統時間

使用GetLocalTime()函式 標頭檔案包含 Windows.h #include<Windows.h>  SYSTEMTIME sysTime;  GetLocalTime(&sysTime);

c++獲取系統時間精確到秒

#include "time.h" #include "stdio.h" #include "stdlib.h" #include <iostream> using namespace std; int main() {time_t now;struct tm *timenow;time(&

C++獲取系統時間的方式

轉自https://download.csdn.net/download/punomo/791219 //方案— 優點:僅使用C標準庫;缺點:只能精確到秒級  #include <time.h>  #include <stdio.h>  in

Windows下c++獲取系統時間的三種方法

1、CTime類的static介面GetCurrentTime() 包含的標頭檔案#include <atltime.h> CTime類的物件表示的時間是基於格林威治標準時間(GMT)的。 CTime表示的日期上限是3000年12月31日,下限是1970年1月1

C獲取系統時間的方法(linux下)

asctime(將時間和日期以字串格式表示)  相關函式  time,ctime,gmtime,localtime 表頭檔案  #include<time.h> 定義函式  char * asctime(const struct tm * timeptr

linux下C獲取系統時間的方法

asctime(將時間和日期以字串格式表示)   相關函式  time,ctime,gmtime,localtime   表頭檔案  #include<time.h>   定義函式  char * asctime(const struct tm * ti

C獲取系統時間C語言獲取系統時間的幾種方式

C 語言中如何獲取時間?精度如何? 1 使用 time_t time( time_t * timer ) 精確到秒 2 使用 clock_t clock() 得到的是 CPU 時間 精確到 1/CLOCKS_PER_SEC 秒 3 計算時間差使用 double difftime( time_t tim

c++/c獲取系統時間,並格式化輸出

話說最難消受美人恩,女士的要求是很難拒接的。。。 應一女士要求,幫其實現個小程式。要求c/c++獲取系統時間,並以中文格式化輸出。 第一反應,so簡單。 話說函式: 1、size_t strftime( char *strDest, size_t maxsize, const

C/C++ 獲取系統時間系統延遲函式呼叫

C或C++呼叫Windows系統函式 實現延時 或 獲取當前時間的處理。可在VC++或MInGW上用 C或C++實現。 1、 實現延時:  標頭檔案: #include<windows.h>  函式:  Sleep(int time);  //time單位為ms,

Linux 環境下C/C++獲取系統時間 && 每隔500ms迴圈一次程式碼實現

環境:NetBeans IDE 8.2 + 遠端主機Linux 獲取當前系統時間getCurrentTime()程式碼如下: #include<sys/time.h> long getCurrentTime(){ struct timeval tv; gett

C# 獲取系統相關時間

獲取系統時鐘頻率 [DllImport("kernel32")] static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency); 獲取系統時鐘計數 [DllImport("ker

C# 獲取系統當前時間 多格式

 c#獲取當前日期時間 我們可以通過使用DataTime這個類來獲取當前的時間。通過呼叫類中的各種方法我們可以獲取不同的時間:如:日期(2008-09-04)、時間(12:12:12)、日期+時間(2008-09-04 12:11:10)等。 //獲取日期+時間 Dat

C語言獲取系統時間方法

需要利用C語言的時間函式time和localtime,具體說明如下: 一、函式介面介紹: 1、time函式。 形式為time_t time (time_t *__timer); 其中time_t為time.h定義的結構體,一般為長整型。 這個函式會獲取

C語言獲取系統時間的幾種方式

C語言中如何獲取時間?精度如何? 1 使用time_t time( time_t * timer ) 精確到秒 2 使用clock_t clock() 得到的是CPU時間 精確到1/CLOCKS_PER_SEC秒 3 計算時間差使用double difftime( time_t timer1, time_

C#/VB.net/VB 獲取系統時間

        系統執行中的一個度量主線就是時間,因此時間的獲取可以說是很普遍的。而東西多了,難免就混亂。今天總結一下vb/vb.net/C#獲取系統時間的語法格式,方便日後查閱。 vb6.0

C語言獲取系統時間的幾種方式 !

四.設定計時器 定義TIMER ID #define TIMERID_JISUANFANGSHI 2 在適當的地方設定時鐘,需要開始其作用的地方; SetTimer(TIMERID_JISUANFANGSHI,200,NULL); 在不需要定時器的時候的時候銷燬掉時鐘 KillTimer(TIMERID_J

C++ 獲取系統當前時間方式

#include <time.h> //// 方式一  time_t tt = time(NULL);  tm* t= localtime(&tt);  printf("%d-%02d-%02d %02d:%02d:%02d\n",    t->t

c++ 如何獲取系統時間

c++ 如何獲取系統時間 2008-04-28 15:34 //方案— 優點:僅使用C標準庫;缺點:只能精確到秒級 #include <time.h> #include <stdio.h> int main( void ) { time_t