純C/C++ mktime()函數
純C/C++ mktime()函數
資料來源: http://www.w3big.com/zh-TW/cplusplus/c-function-mktime.html
線上編譯器: https://www.tutorialspoint.com/compile_c_online.php
time_t mktime(struct tm *timeptr)函數,把timeptr所指向的結構轉換為一個依據本地時區的time_t值
code:
#include <stdio.h>
#include <time.h>
#define BST (+1)
#define CCT (+8)
int main ()
{
time_t rawtime;
struct tm *info;
int ret;
char buffer[80];
time(&rawtime);
/* 获取 GMT 时间 */
info = gmtime(&rawtime );
ret = mktime(info);
if( ret == -1 )
{
printf("错误:不能使用 mktime 转换时间。\n");
}
else
{
strftime(buffer, sizeof(buffer), "%c", info );
printf(buffer);
}
printf("当前的世界时钟:\n");
printf("伦敦:%2d:%02d\n", (info->tm_hour+BST)%24, info->tm_min);
printf("中国:%2d:%02d\n", (info->tm_hour+CCT)%24, info->tm_min);
return(0);
}