讓你提前認識軟體開發(11):localtime與GetLocalTime
第1部分 重新認識C語言
localtime與GetLocalTime
【文章摘要】
在C語言中,有兩個獲取當前時間的函式:localtime和GetLocalTime。它們在諸如對賬一類的程式中應用很廣。
本文對localtime和GetLocalTime進行較為詳細的介紹,並舉例說明它們的用法。
【關鍵詞】
localtime GetLocalTime 日期時間
1.前言
在C語言中,獲取當前系統時間的操作時有發生。例如,在對賬程式中,需要在對賬檔名中加入當天的日期及時間。一般而言,經常使用的獲取當前時間的函式有兩個:localtime和GetLocalTime
本文分別介紹了localtime和GetLocalTime函式,並舉例說明了它們的用法。
2. localtime和GetLocalTime函式介紹
2.1 localtime函式介紹
功能:把從1970-1-1零點零分(程式中是從1900年開始算的)到當前時間系統所偏移的秒數時間轉換為日曆時間。
說明:此函式獲得的tm結構體的時間,已經進行過時區轉化,成為了本地時間。
用法:struct tm *localtime(const time_t *clock)。
返回值:返回指向tm結構體的指標。
tm
#ifndef _TM_DEFINED
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; /*
int tm_yday; /*從每年的1月1日開始的天數–取值區間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */
int tm_isdst; /*夏令時識別符號,實行夏令時的時候,tm_isdst為正;不實行夏令時的進候,tm_isdst為0;不瞭解情況時,tm_isdst()為負 */
long int tm_gmtoff; /*指定了日期變更線東面時區中UTC東部時區正秒數或UTC西部時區的負秒數*/
const char *tm_zone; /*當前時區的名字(與環境變數TZ有關)*/
};
#define _TM_DEFINED
#endif
2.2 GetLocalTime函式介紹
函式功能:該函式用來獲取當地的當前系統日期和時間。
函式原型:void GetLocalTime(LPSYSTEMTIME lpSystemTime // address of system times structure);
引數說明:
lpSystemTime:指向一個使用者自定義包含日期和時間資訊的型別為SYSTEMTIME的變數,該變數用來儲存函式獲取的時間資訊。
SYSTEMTIME結構體的定義如下:
typedef struct _SYSTEMTIME
{
WORD wYear; //年
WORD wMonth; //月
WORD wDayOfWeek; //星期,0為星期日,1為星期一,2為星期二……
WORD wDay; //日
WORD wHour; //時
WORD wMinute; //分
WORD wSecond; //秒
WORD wMilliseconds; //毫秒
}SYSTEMTIME,*PSYSTEMTIME;
3. localtime和GetLocalTime函式應用舉例
3.1例子程式
包含localtime和GetLocalTime函式的例子程式如下:
//用於測試時間函式
#include <afxinet.h>
typedef signed int INT32; // 重定義資料型別
/****************************************************************
*功能描述: 主函式 *
*輸入引數: 無 *
*輸出引數: 無 *
*返 回 值:無 *
*其他說明: 無 *
*修改日期版本號修改人修改內容
* ------------------------------------------------------------------------------
* 20140409 V1.0 zzx 建立
****************************************************************/
INT32 main(void)
{
//用於GetLocalTime輸入引數
INT32 iGetLocalTimeDate = 0;
INT32 iGetLocalTimeTime = 0;
//用於localtime輸入引數
INT32 iLocalTimeDate = 0;
INT32 iLocalTimeTime = 0;
time_t now;
struct tm today;
SYSTEMTIME sysTime;
//使用GetLocalTime函式獲取日期和時間 begin
{
GetLocalTime(&sysTime);
iGetLocalTimeDate = sysTime.wYear * 10000 + sysTime.wMonth * 100 + sysTime.wDay;
iGetLocalTimeTime = sysTime.wHour * 10000 + sysTime.wMinute * 100 + sysTime.wSecond;
}
//使用GetLocalTime函式獲取日期和時間 end
//使用localtime函式獲取日期和時間 begin
{
time(&now);
today = *localtime(&now);
iLocalTimeDate = (1900 + today.tm_year) * 10000 + (1 + today.tm_mon) * 100 + today.tm_mday;
iLocalTimeTime = today.tm_hour * 10000 + today.tm_min * 100 + today.tm_sec;
}
//使用localtime函式獲取日期和時間 end
//先比較日期
if (iGetLocalTimeDate == iLocalTimeDate)
{
printf("The date is the same, it is %04d年%02d月%02d日.\n", sysTime.wYear, sysTime.wMonth, sysTime.wDay);
}
else
{
printf("The date is not the same. In UseGetLocalTime(), it is %d. In UseLocalTime(), it is %d.\n", iGetLocalTimeDate, iLocalTimeDate);
}
//再比較時間
if (iGetLocalTimeTime == iLocalTimeTime)
{
printf("The time is the same, it is %02d:%02d:%02d.\n", sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
}
else
{
printf("The time is not the same. In UseGetLocalTime(), it is %d. In UseLocalTime(), it is %d.\n", iGetLocalTimeTime, iLocalTimeTime);
}
printf("Local time is %s", asctime(&today));
printf("It is the %d day of week.\n", sysTime.wDayOfWeek);
return 0;
}
3.2程式說明
本例子程式包含如下操作:
(1)用GetLocalTime函式獲取當前日期和時間,其中日期存放在變數iGetLocalTimeDate中(格式為:yyyymmdd),時間存放在變數iGetLocalTimeTime中(格式為:hhmmss)。
(2)用localtime函式獲取當前日期和時間,其中日期存放在變數iLocalTimeDate中(格式為:yyyymmdd),時間存放在變數iLocalTimeTime中(格式為:hhmmss)。
(3)比較用這兩個函式獲得的日期和時間是否相同。若相同,則將日期和時間分別打印出來;若不相同,則分別將用這兩個函式獲得的日期和時間打印出來。
(4)用asctime函式轉換日期和時間為相應的ASCII碼,返回字串格式為:星期,月,日,小時,分,秒,年。
(5)呼叫sysTime.wDayOfWeek列印當前日期是一週中的第幾天。
3.3程式輸出
本例子程式的輸出結果如下:
4.總結
“條條大路通羅馬”,要做成功一件事情,我們可能會有很多種方法。
在C語言中,常用的獲取當前時間的函式有localtime和GetLocalTime兩個。經過程式測試表明,只要用法正確,它們獲取的日期和時間結果就是相同的。