(Linux_C环境编程:时间日期函数总结)
// TimeUtil.h
#ifndef __TIME_UTIL_H__
#define __TIME_UTIL_H__
#ifdef __cplusplus //告诉编译器,这部分代码按C语言的格式进行编译,而不是C++的
extern "C"{
#endif
int UTIL_is_time_expired(string from, string to);
#ifdef __cplusplus //告诉编译器,这部分代码按C语言的格式进行编译,而不是C++的
}
#endif
#endif /* __TIME_UTIL_H__ */
// TimeUtil.cpp
#include <string>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <time.h>
//#include "liblicense_log.h"
#include "TimeUtil.h"
#define DEBUG_TIMEUTIL (1)
#define print_ln(log_level, fmt, ...) do {printf("(%s|%d)" fmt "\r\n", __func__, __LINE__, ##__VA_ARGS__); fflush(stdout);} while(0)
#define xxxx_free(a) do {if(a) {free((void *)a); (a) = NULL;}} while(0)
/*****************************************************************
本地时间转time_t
本地时间 "2022-02-27 00:57:30"
本地时间 "2022-02-27"
参数 : time_str, 时间字符串
return : 0, 成功
-1, 失败
*****************************************************************/
static time_t str_to_time_t(string time_str)
{
struct tm tm;
time_t ret_seconds;
memset(&tm, 0, sizeof(struct tm));
strptime(time_str.c_str(), "%Y-%m-%d %H:%M:%S", &tm);
ret_seconds = timelocal(&tm); // timelocal 函数都是线程安全的
//printf("timelocal():%d\n", ret_seconds);
return ret_seconds;
}
/*****************************************************************
获取此刻本地时间
参数 : 无
return : 返回此刻时间 time_t
*****************************************************************/
static time_t get_now_time_t(void)
{
return time(NULL);
}
static string print_time_t(time_t &tm)
{
struct tm *p_tm = NULL;
char buf[256] = {0};
p_tm = localtime(&tm);
strftime(buf, sizeof(buf)-1, "%F %T", p_tm);
//printf("local time=%s\n", buf);
return buf;
}
/*****************************************************************
比较当前时间是否在(from, to]时间内(本地时间)
时间示例:
本地时间 "2022-02-27 00:57:30"
本地时间 "2022-02-27"
参数 : from, 开始时间, 参考时间示例
to, 结束时间, 参考时间示例
return : 0, 当前时间处在有效期内
-1, 当前时间处在开始时间之前
1, 当前时间处在结束时间之后(过期)
-2, 失败
*****************************************************************/
int UTIL_is_time_expired(string from, string to)
{
time_t time_from = str_to_time_t(from.c_str());
time_t time_to = str_to_time_t(to.c_str());
time_t time_curr = get_now_time_t();
xxxx_print_ln(xxxx_INFO, "DateFrom=%s, GraceTo=%s, curr=%s", from.c_str(), to.c_str(), print_time_t(time_curr).c_str());
xxxx_print_ln(xxxx_INFO, "DateFrom=%ld, GraceTo=%ld, curr=%ld", time_from, time_to, time_curr);
if (time_from >= time_to) {
return -2;
}
if (time_curr <= time_from) {
return -1;
} else if (time_curr <= time_to) {
return 0;
} else if (time_to < time_curr) {
return 1;
}
return -2;
}
#if DEBUG_TIMEUTIL
static string get_now()
{
time_t now;
struct tm *p_tm = NULL;
char buf[256] = {0};
now = time(NULL);
p_tm = localtime(&now);
strftime(buf, sizeof(buf)-1, "%F %T %a", p_tm);
printf("local time=%s\n", buf);
return buf;
}
int test1()
{
time_t to = str_to_time_t("2023-11-14 19:53:00");
time_t curr = time(NULL);
printf("curr=%d\n", curr);
printf("to=%d\n", to);
printf("curr-to=%d\n", curr - to);
printf("curr-to=%.0f\n", difftime(curr, to));
return 0;
}
int test2()
{
time_t to = str_to_time_t("2023-11-15");
time_t curr = time(NULL);
printf("curr=%d\n", curr);
printf("to=%d\n", to);
printf("curr-to=%d\n", curr - to);
printf("curr-to=%.0f\n", difftime(curr, to));
return 0;
}
int test3()
{
string from = "2023-11-14 20:14:30";
string to = "2023-11-14 20:15:00";
printf("from=%s\n", from.c_str());
printf("to =%s\n", to.c_str());
printf("ret=%d\n", UTIL_is_time_expired(from, to));
return 0;
}
int main()
{
test1();
test2();
test3();
return 0;
}
#endif
g++ TimeUtil.cpp