3、当地时区计算
需要用到的接口:
#include <time.h>
struct tm *localtime(const time_t *timep);
计算当地时区:
#include <stdio.h>
#include <time.h>
time_t get_utc_time(void)
{
return time(NULL);
}
int main(int argc, char **argv)
{
time_t utc_time = get_utc_time();
printf("utc_time = %ld s\n", utc_time);
struct tm *gmt_tm = gmtime(&utc_time);
printf("gmt time = %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n", gmt_tm->tm_year + 1900,
gmt_tm->tm_mon + 1,
gmt_tm->tm_mday,
gmt_tm->tm_hour,
gmt_tm->tm_min,
gmt_tm->tm_sec);
int gmt_hour = gmt_tm->tm_hour;
struct tm *local_tm = localtime(&utc_time);
printf("local time = %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n", local_tm->tm_year + 1900,
local_tm->tm_mon + 1,
local_tm->tm_mday,
local_tm->tm_hour,
local_tm->tm_min,
local_tm->tm_sec);
int local_hour = local_tm->tm_hour;
int local_time_zone = local_hour - gmt_hour;
if (local_time_zone < -12)
{
local_time_zone += 24;
}
else if (local_time_zone > 12)
{
local_time_zone -= 24;
}else{}
printf("local_time_zone = %d\n", local_time_zone);
return 0;
}
我们当前为北京时间:
我们把Ubuntu时间日期里的地点改到其它国家:
|