//方案— 優點:僅使用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
#endif
2,在標準C/C++中,我們可通過tm結構來獲得日期和時間,tm結構在time.h中的定義如下:
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int 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));//輸出本地時間


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;

}

 


引文來源 c++ 如何獲取系統時間 - daniel的日誌 - 網易博客

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()