有時候我們需要在使用者介面顯示時間,像是在頁面設定只要網客瀏覽到你的部落格,部落格就會提示現在是什麼時間,接著問候你「早安」、「午安」、「晚安」,或是更俏皮的話語「該吃飯啦~」、「該睡覺哩~」等等,不過現在最常見的是,時間悄悄躺在頁面左右兩旁,默默地滴答滴答……
在此我使用到兩個變數型態:time_t和tm,前者是長整數,後者是充滿整數型態的結構。還有使用四個函數:time(*t_time)、asctime(*tm)、localtime(*t_time)和gmtime(*t_time),後兩者回傳*tm。直接來看程式碼:
/**
Theme: Show time
Compiler: Dev C++ 4.9.9.2
Date: 100/05/05
Author: ShengWen
Blog: https://cg2010studio.wordpress.com/
*/
#include<iostream>
#include<iomanip>
using namespace std;
#define YEAR_SET 1900
#define MON_SET 1
int main(){
tm *ptrnow;
time_t loc_now=0, std_now=0;
time(&loc_now);//seconds from 1970/01/01
time(&std_now);//seconds from 1970/01/01
ptrnow=localtime(&loc_now);//get local time
cout<<"#Local time: "<<endl;
cout<<setw(10)<<"year: "<<ptrnow->tm_year+YEAR_SET<<endl;
cout<<setw(10)<<"month: "<<ptrnow->tm_mon+MON_SET<<endl;
cout<<setw(10)<<"mday: "<<ptrnow->tm_mday<<endl;
cout<<setw(10)<<"wday: "<<ptrnow->tm_wday<<endl;
cout<<setw(10)<<"yday: "<<ptrnow->tm_yday<<endl;
cout<<setw(10)<<"hour: "<<ptrnow->tm_hour<<endl;
cout<<setw(10)<<"minute: "<<ptrnow->tm_min<<endl;
cout<<setw(10)<<"second: "<<ptrnow->tm_sec<<endl;
cout<<asctime(ptrnow)<<endl;//transform to string
ptrnow=gmtime(&std_now);//get standard time
cout<<"#Standard time: "<<endl;
cout<<setw(10)<<"year: "<<ptrnow->tm_year+YEAR_SET<<endl;
cout<<setw(10)<<"month: "<<ptrnow->tm_mon+MON_SET<<endl;
cout<<setw(10)<<"mday: "<<ptrnow->tm_mday<<endl;
cout<<setw(10)<<"wday: "<<ptrnow->tm_wday<<endl;
cout<<setw(10)<<"yday: "<<ptrnow->tm_yday<<endl;
cout<<setw(10)<<"hour: "<<ptrnow->tm_hour<<endl;
cout<<setw(10)<<"minute: "<<ptrnow->tm_min<<endl;
cout<<setw(10)<<"second: "<<ptrnow->tm_sec<<endl;
cout<<asctime(ptrnow)<<endl;//transform to string
system("pause");
return EXIT_SUCCESS;
}
看了程式碼,相信你會好奇tm結構裡有什麼東西,就如同上面所述,結構裡頭全部都是int的變數:
struct tm {
int tm_sec; /* 秒– 取值區間為[0,59] */
int tm_min; /* 分- 取值區間為[0,59] */
int tm_hour; /* 時- 取值區間為[0,23] */
int tm_year; /* 年份,其值等於實際年份減去1900 */
int tm_mon; /* 月份- 取值區間為[0,11] */
int tm_mday; /* 月中的日- 取值區間為[1,31] */
int tm_wday; /* 週中的日(星期)– 取值區間為[0,6] */
int tm_yday; /* 年中的1月1日開始的天數– 取值區間為[0,365] */
int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。 不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。 */
};
從結構定義來看,就可以知道我為何要tm_year+YEAR_SET和tm_mon+MON_SET。time(*t_time):取得從1970/01/01開始算的秒數為t_time
asctime(*tm):將tm結構轉為string
localtime(*t_time):由t_time取得秒數,接著轉換為tm,區域時間
gmtime(*t_time):由t_time取得秒數,接著轉換為tm,格林威治標準時間
看程式執行結果:
#Local time:
year: 2011
month: 5
mday: 5
wday: 4
yday: 124
hour: 22
minute: 58
second: 36
Thu May 05 22:58:36 2011#Standard time:
year: 2011
month: 5
mday: 5
wday: 4
yday: 124
hour: 14
minute: 58
second: 36
Thu May 05 14:58:36 2011請按任意鍵繼續 . . .
聰明的你一定會偷笑,因為你知道我執行此程式的時間!話說Coordinated Universal Time(UTC)協調世界時間,又稱為世界標準時間,也就是大家所熟知的格林威治標準時間(Greenwich Mean Time,GMT)。比如,台灣的時間與UTC的時差為+8,也就是UTC+8,而美國是UTC-5。所以你可以看到#Local time比#Standard time快了8小時。
最後其實yday應該是125,不過你可以當作已經過了124天!有趣的是:t_time是長整數,在32位元其可表示的範圍「1970年1月1日0時0分0秒」到「2038年1月18日19時14分07秒」,超過了怎麼辦?只好使用64位元囉~
參考:C/C++時間函數使用方法。
Comments on: "[C++] 顯示時間 (Show Time)" (4)
請問這code是不是少了什麼,例如include"time.h"之類的?
讚讚
我測試過Dev C++和Visual C++,兩者上頭的code都能執行,沒有問題,不知道你是用哪種compiler?
讚讚
Visual C++ 2008。
我沒有include的話,time、localtime、asctime、gmtime都會變成未定義
讚讚
Dev C++ 4.9.9.2和Visual C++ 2010都ok,呵~我是這樣子的,如果可以不引入就能編譯的話不去寫引入那一行。我猜time.h應該被涵蓋進iostream或iomanip之中,可以再check一下:P
讚讚