1. localtime
头文件:
#include <time.h>
函数定义:
struct tm *localtime(const time_t *timep);
功能:
把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtimes函数转换后的时间没有经过时区变换,是UTC时间 。
参数:
time_t 从1970-1-1时计算的utc时间。
返回值:
返回指向tm 结构体的指针.tm结构体是time.h中定义的用于分别存储时间的各个量(年月日等)的结构体.
示例:
time_t tNow =time(NULL);
time_t tEnd = tNow + 1800;
struct tm* ptm = localtime(&tNow);
struct tm* ptmEnd = localtime(&tEnd);
2. localtime_r
头文件:
#include <time.h>
函数定义:
struct tm *localtime_r(const time_t *timep, struct tm *result);
功能:
把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtimes函数转换后的时间没有经过时区变换,是UTC时间 。
参数:
time_t 从1970-1-1时计算的utc时间。 tm 结构体用于获取返回的时间。
返回值:
返回指向tm 结构体的指针.tm结构体是time.h中定义的用于分别存储时间的各个量(年月日等)的结构体.
示例:
time_t tNow =time(NULL);
time_t tEnd = tNow + 1800;
struct tm ptm = { 0 };
struct tm ptmEnd = { 0 };
localtime_r(&tNow, &ptm);
localtime_r(&tEnd, &ptmEnd);
3. strftime
头文件:
#include <time.h>
函数定义:
size_t strftime(char *s, size_t max, const char *format,
const struct tm *tm);
功能:
strftime() 函数根据区域设置格式化本地时间/日期,函数的功能将时间格式化,或者说格式化一个时间字符串。
参数:
示例:
struct timeval tv = {0,};
struct tm now = {0, };
char now_str[32];
gettimeofday (&tv, NULL);
localtime_r (&tv.tv_sec, &now);
strftime (now_str, 32, "%Y/%m/%d-%H:%M:%S", &now);