首页 > 系统相关 >linux时间比较大小----亲测OK

linux时间比较大小----亲测OK

时间:2023-11-20 12:34:37浏览次数:47  
标签:__ OK linux ---- tm 时间 str time return

摘自:https://blog.csdn.net/u010429831/article/details/122722187 (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

 

标签:__,OK,linux,----,tm,时间,str,time,return
From: https://www.cnblogs.com/LiuYanYGZ/p/17843662.html

相关文章

  • VS 2022 不支持 .NET Framework 4.5 项目解决办法(Visual Studio 2022)
    VS2022不支持.NETFramework4.5项目解决办法(VisualStudio2022)默认VisualStudio2022不再支持安装.NETFramework4.5组件@选择组件里面已经不能选择4.5/4.0的框架了此时如果打开基于.NETFramework4.5的项目会收到提示:xxxx面向不再受支持的。NETFramewo......
  • 网站/软件注册邮件发送?验证码邮件发信?
    在当今数字化时代,网站和软件的注册过程成为用户踏入数字世界的第一步。为了确保安全性和用户身份验证,验证码邮件发信成为一种常见的做法。蜂邮EDM将探讨网站/软件注册邮件发送的重要性以及验证码邮件发信的实际效果。1.注册流程关键性:在用户注册过程中,网站/软件注册邮件发送起着至......
  • Ubuntu 16.04 安装英伟达(Nvidia)显卡驱动
    Ubuntu16.04安装英伟达(Nvidia)显卡驱动配有英伟达显卡的主机,装完Ubuntu16.04后出现闪屏现象,是由于没有安装显卡驱动。显卡型号NVIDIACorporationGM204[GeForceGTX970]由于没有显卡驱动,屏幕闪屏,以下安装过程在终端模式下进行(按Ctrl+Alt+F1切换到终端界面)如果不能......
  • 简单统计SQLSERVER用户数据表大小(包括记录总数和空间占用情况)
    在SQLSERVER,简单的组合sp_spaceused和sp_MSforeachtable这两个存储过程,可以方便的统计出用户数据表的大小,包括记录总数和空间占用情况,非常实用,在SqlServer2K和SqlServer2005中都测试通过。/*1.execsp_spaceused'表名'      (SQL统计数据,大量事务操作后可能不准)2.exe......
  • PG国产电竞巨兽震撼登场:一加12搭载骁龙8 Gen3,首发最强电子游戏屏幕
    一加官微宣布,备受期待的一加12将于12月4日发布,成为一加品牌迎来的十周年力作。品牌创始人刘作虎在十周年围炉夜话中表达了对这一时刻的感慨,并承诺与用户共同见证一加12的十年超越之作。据PGSOFT电子游艺官方透露,一加12至少有两大引人注目的卖点。首先是其搭载的首发国产2K东方屏,被......
  • 【略读论文|时序知识图谱补全】Logic and Commonsense-Guided Temporal Knowledge Gra
    会议:AAAI,时间:2023,学校:北京航空航天大学文中谓词可以视为关系。以往的TKG补全(TKGC)方法不能同时表示事件的时效性和因果关系。为了应对这些问题,作者提出了一个逻辑和尝试引导嵌入模型(LCGE),从常识的角度共同学习涉及事件的及时性和因果关系的时间敏感表示,以及事件的时间无关表示......
  • 采用开源工具搭建网络监控平台的解决方案
    目标建立一个全面、可扩展的网络监控系统,实时监控公司网络的健康状况、性能指标和安全警报,以提高网络可靠性和效率。解决方案采用集成多个开源工具的方法,构建一个包括实时监控、数据分析、可视化和告警的综合网络监控平台。核心组件数据采集:使用Prometheus作为主要的数据采集系统。......
  • GPU服务器的含义
    GPU服务器是配备了图形处理单元(GPU)的服务器,用于加速处理图形、计算密集型任务和并行计算工作负载。传统的中央处理单元(CPU)主要用于通用目的的计算,而GPU则专注于处理与图形相关的计算任务。近年来,由于GPU在并行计算方面的卓越性能,GPU服务器在多个领域得到广泛应用。以下是GPU服务器......
  • #yyds干货盘点#VueRouter模拟实现
    hash模式URL中#号后面的内容作为路径地址监听hashchange事件根据当前路与地址找到对应的组件重新渲染history模式通过history.pushState()方法改变地址栏监听popstate事件根据当前路由地址找到对应组件重新渲染基本使用//1.注册路由插件Vue.use(VueRouter)//2.创建router对......
  • 无涯教程-Ruby - 迭代器
    迭代器不过是collections 集合支持的方法。存储一组数据成员的对象称为集合。在Ruby中,数组和哈希可以称为集合。迭代器一个接一个地返回集合的所有元素。无涯教程将在这里讨论两个迭代器,分别是each和collect。Each迭代器每个迭代器返回数组或哈希的所有元素。collecti......