3 回答

TA貢獻1830條經驗 獲得超9個贊
1、C語言中讀取系統時間的函數為time(),其函數原型為:#include <time.h>time_t time( time_t * ) ;time_t就是long,函數返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現在的的秒數。2、C語言還提供了將秒數轉換成相應的時間格式的函數:char * ctime(const time_t *timer); //將日歷時間轉換成本地時間,返回轉換后的字符串指針 可定義字符串或是字符指針來接收返回值struct tm * gmtime(const time_t *timer); //將日歷時間轉化為世界標準時間(即格林尼治時間),返回結構體指針 可定義struct tm *變量來接收結果struct tm * localtime(const time_t * timer); //將日歷時間轉化為本地時間,返回結構體指針 可定義struct tm *變量來接收結果3、例程:
12345678910111213141516171819202122 | #include <time.h> void main() { time_t t; struct tm *pt ; char *pc ; time (&t); pc= ctime (&t) ; printf ( "ctime:%s" , pc ); pt= localtime (&t) ; printf ( "year=%d" , pt->tm_year+1900 ); } //時間結構體struct tm 說明: 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()為負。*/ }; |

TA貢獻1803條經驗 獲得超6個贊
常用的與時間有關的Win32 API有兩個:GetSystemTime(); 與 SetSystemTime(); 下面是函數原型:
VOID GetSystemTime(LPSYSTEMTIME lpSystemTime);
BOOL SetSystemTime( const SYSTEMTIME *lpSystemTime );
我們查一下 MSDN 看看 LPSYSTEMTIME 與 SYSTEMTIME 是什么:
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;
void main()
{
SYSTEMTIME st;
GetSystemTime(&st); // Win32 API 獲取系統當前時間,并存入結構體st中
......下面 你自己處理。
}

TA貢獻1863條經驗 獲得超2個贊
/* asctime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "The current date/time is: %s", asctime (timeinfo) );
return 0;
}
/* localtime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
return 0;
}
/* ctime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
time ( &rawtime );
printf ( "The current local time is: %s", ctime (&rawtime) );
return 0;
}
/* gmtime example */
#include <stdio.h>
#include <time.h>
#define MST (-7)
#define UTC (0)
#define CCT (+8)
int main ()
{
time_t rawtime;
tm * ptm;
time ( &rawtime );
ptm = gmtime ( &rawtime );
puts ("Current time around the World:");
printf ("Phoenix, AZ (U.S.) : %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);
printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);
printf ("Beijing (China) : %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min);
return 0;
}
- 3 回答
- 0 關注
- 723 瀏覽
添加回答
舉報