一、相关系统调用
1、time函数
函数原型:time_t time(time_t *time)
头文件:#include <time.h>
功能:获得系统当前时间
返回值(长整型):成功返回从公元1970-1-1,00:00:00到当前的秒数,失败返回-1
参数(传出参数):同返回值
2、gmtime函数
函数原型:struct tm *gmtime(const time_t *time)
头文件:#include <time.h>
功能:将从公元1970-1-1,00:00:00到当前的秒数时间转换成日常使用的年月日形式
返回值:日常生活中所使用的时间表示,用结构体struct tm表示,utc时间,不加时区
参数(传入参数):从公元1970-1-1,00:00:00到当前的秒数时间,长整型
3、localtime函数
函数原型:struct tm *localtime(const time_t time)
头文件:#include <time.h>
功能:将utc秒时间转换为年月日时分秒的形式,并加时区,表征当地时间
返回值:struct tm,当地时间
参数(传入参数):从公元1970-1-1,00:00:00到当前的秒数时间,长整型
4、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日 */
int tm_isdst; // 夏令时标识符,夏令时tm_isdst为正;不实行夏令时tm_isdst为0
};
二、常用接口封装
1、获取当前本地时间,日常表示形式,使用结构体描述
int get_time_local_common(service_time_t *cur_time)
{
time_t t;
struct tm *tp;
if((0 < time(&t)) && (NULL != (tp = localtime(&t))))
{
cur_time->year = tp->tm_year + 1900;
cur_time->mouth = tp->tm_mon + 1;
cur_time->week = tp->tm_wday;
cur_time->day = tp->tm_mday;
cur_time->hour = tp->tm_hour;
cur_time->minute = tp->tm_min;
cur_time->second = tp->tm_sec;
}
else
{
return -1;
}
return 0;
}
2、获取当前本地时间,日常表示形式,使用字符串描述
int get_time_local_string(char *time_str, size_t buf_len)
{
time_t t;
struct tm *tp;
if((0 < time(&t)) && (NULL != (tp = localtime(&t))))
{
snprintf(time_str, buf_len, "%d/%02d/%02d %02d:%02d:%02d",
tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
}
else
{
return -1;
}
return 0;
}
3、获取当前utc时间,日常表示形式,使用结构体描述
int get_time_utc_common(service_time_t *utc_time)
{
time_t t;
struct tm *tp;
if((0 < time(&t)) && (NULL != (tp = gmtime(&t))))
{
utc_time->year = tp->tm_year + 1900;
utc_time->mouth = tp->tm_mon + 1;
utc_time->week = tp->tm_wday;
utc_time->day = tp->tm_mday;
utc_time->hour = tp->tm_hour;
utc_time->minute = tp->tm_min;
utc_time->second = tp->tm_sec;
}
else
{
return -1;
}
return 0;
}
4、获取当前utc时间,日常表示形式,使用字符串描述
int get_time_utc_string(char *time_str, size_t buf_len)
{
time_t t;
struct tm *tp;
if((0 < time(&t)) && (NULL != (tp = gmtime(&t))))
{
snprintf(time_str, buf_len, "%d/%02d/%02d %02d:%02d:%02d",
tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
}
else
{
return -1;
}
return 0;
}
5、获取当前时区
int g_timezone = -100;
int get_timezone()
{
if(-100 == g_timezone)
{
time_t t;
struct tm *tp;
if(0 < time(&t))
{
int second_utc = t % (24 * 60 * 60);
if(NULL != (tp = localtime(&t)))
{
int second_local = tp->tm_hour * 3600 + tp->tm_min * 60 + tp->tm_sec;
g_timezone = (second_local - second_utc) / (60 * 60);
if(12 < g_timezone)
{
g_timezone = 24 - timezone;
}
}
}
}
return g_timezone;
}
6、时间表示结构体
typedef struct
{
int year;
int mouth;
int day;
int week;
int hour;
int minute;
int second;
}service_time_t;
7、测试主函数
int main()
{
service_time_t time;
printf("timezone = %d\n", get_timezone());
if(0 <= get_time_local_common(&time))
{
printf("local time : %d/%d/%d week:%d %d:%d:%d\n", time.year, time.mouth, time.day,
time.week, time.hour, time.minute, time.second);
}
if(0 <= get_time_utc_common(&time))
{
printf(" utc time : %d/%d/%d week:%d %d:%d:%d\n", time.year, time.mouth, time.day,
time.week, time.hour, time.minute, time.second);
}
char time_str[30];
if(0 <= get_time_local_string(time_str, sizeof(time_str)))
{
printf("local time string : %s\n", time_str);
}
if(0 <= get_time_utc_string(time_str, sizeof(time_str)))
{
printf(" utc time string : %s\n", time_str);
}
return 0;
}
8、测试程序输出
timezone = 8
local time : 2020/9/29 week:2 21:40:25
utc time : 2020/9/29 week:2 13:40:25
local time string : 2020/09/29 21:40:25
utc time string : 2020/09/29 13:40:25