当前位置: 首页 > news >正文

C11 日期时间处理案例

文章目录

  • 显示当前日期时间
  • 得到当前日期时间的17位数字形式(YYYYmmddHHMMSSsss)
  • 从日期时间字符串得到time_t 类型时间戳
  • 从时期时间字符串得到毫秒单位的时间戳
  • 得到当前日期时间以毫秒为单位的时间戳
  • 一个综合案例

所有例子在VS2019上编译运行通过

显示当前日期时间

#include <stdio.h>
#include <time.h>
#include <stdlib.h>int main()
{const int base = TIME_UTC;timespec ts;if (timespec_get(&ts, base) != base) {perror("timespec_get failure");return EXIT_FAILURE;}tm t;localtime_s(&t, &ts.tv_sec);char buff[512];strftime(buff, sizeof buff, "%Y-%m-%d %H:%M:%S", &t);printf("%s.%03d\n", buff, int(ts.tv_nsec / 1000 / 1000));return EXIT_SUCCESS;
}

得到当前日期时间的17位数字形式(YYYYmmddHHMMSSsss)

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>uint64_t GetNowDTime()
{const int base = TIME_UTC;timespec ts;if (timespec_get(&ts, base) != base) {perror("timespec_get failure");return 0;}tm t;localtime_s(&t, &ts.tv_sec);char buff[512];strftime(buff, sizeof buff, "%Y%m%d%H%M%S", &t);snprintf(buff, sizeof buff, "%s%03d", buff, ts.tv_nsec / 1000 / 1000);return atoll(buff);
}int main()
{printf("localtime: %lld\n", GetNowDTime());return EXIT_SUCCESS;
}

从日期时间字符串得到time_t 类型时间戳

#include <stdio.h>
#include <time.h>
#include <stdlib.h>time_t GetTimeFromDatetimeStr(const char* const datetimeStr)
{tm t;int msec = 0.;if (sscanf_s(datetimeStr, "%4d-%2d-%2d %2d:%2d:%2d.%3d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec, &msec) == EOF) {perror("sscanf_s failure");return 0;}t.tm_year -= 1900;t.tm_mon -= 1;t.tm_isdst = -1;return mktime(&t);
}int main()
{time_t tt = GetTimeFromDatetimeStr("2025-05-05 12:13:56.123");char buff[128];ctime_s(buff, sizeof buff, &tt);printf(buff);return EXIT_SUCCESS;
}

从时期时间字符串得到毫秒单位的时间戳

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>uint64_t GetTimeFromDatetimeStr(const char* const datetimeStr)
{tm t;int msec = 0.;if (sscanf_s(datetimeStr, "%4d/%2d/%2d %2d:%2d:%2d.%3d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec, &msec) == EOF) {perror("sscanf_s failure");return 0;}t.tm_year -= 1900;time_t tt = mktime(&t);const uint64_t res = tt * 1000ull + msec;return res;
}int main()
{uint64_t timestamp = GetTimeFromDatetimeStr("2025/05/05 12:13:56.123");lldiv_t res = lldiv(timestamp, 1000);char buff[128];ctime_s(buff, sizeof buff, &res.quot);printf("%s\n", buff);return EXIT_SUCCESS;
}

得到当前日期时间以毫秒为单位的时间戳

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>uint64_t CurrTimestamp()
{const int base = TIME_UTC;timespec ts;if (timespec_get(&ts, base) != base){perror("timespec_get failure");return 0;}const uint64_t timestamp = (ts.tv_sec * 1000ull) + (ts.tv_nsec / 1000 / 1000);return timestamp;
}int main()
{time_t tt = CurrTimestamp() / 1000;if (tt == 0){return EXIT_FAILURE;}char buff[128];ctime_s(buff, sizeof buff, &tt);printf(buff);return EXIT_SUCCESS;
}

一个综合案例

X一个17位数字类型的日期时间(YYYYmmddHHMMSSsss)
得到当前日期时间与X的时延,以毫秒为单位

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>uint64_t CurrMSecTimestamp()
{const int base = TIME_UTC;timespec ts;if (timespec_get(&ts, base) != base){perror("timespec_get failure");return 0;}const uint64_t timestamp = (ts.tv_sec * 1000ull) + (ts.tv_nsec / 1000 / 1000);return timestamp;
}uint64_t DiffFromNumTime(uint64_t dt)
{char buff[128];snprintf(buff, sizeof buff, "%lld", dt);tm t;int msec;sscanf_s(buff, "%4d%2d%2d%2d%2d%2d%3d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec, &msec);t.tm_year -= 1900;t.tm_mon -= 1;t.tm_isdst = -1;const time_t tt = mktime(&t);if (tt <= 0) {perror("mktime error");return 0;}const uint64_t xTimestamp = tt * 1000ull + msec;const uint64_t cTimestamp = CurrMSecTimestamp();return cTimestamp - xTimestamp;
}uint64_t GetNowDTime()
{const int base = TIME_UTC;timespec ts;if (timespec_get(&ts, base) != base) {perror("timespec_get failure");return 0;}tm t;localtime_s(&t, &ts.tv_sec);char buff[512];strftime(buff, sizeof buff, "%Y%m%d%H%M%S", &t);snprintf(buff, sizeof buff, "%s%03d", buff, ts.tv_nsec / 1000 / 1000);return atoll(buff);
}int main()
{const uint64_t dt = GetNowDTime();const uint64_t d = DiffFromNumTime(dt);printf("%lld\n", d);return EXIT_SUCCESS;
}
http://www.xdnf.cn/news/574399.html

相关文章:

  • AtCoder 第406场初级竞赛 A~E题解
  • 学习黑客了解密码学
  • Coze工作流-变量以及变量的类型讲解
  • 最新版Chrome浏览器调用ActiveX控件之eDrawings Viewer专用包v2.0.42版本发布
  • 【AI流程应用】智能知识库搭建与实战应用
  • RK3588 RKNN ResNet50推理测试
  • Spring 定时器和异步线程池 实践指南
  • COMP3023 Design and Analysis of Algorithms
  • ./build/mkfs.jffs2: Command not found
  • 车载诊断架构 --- LIN 节点 ECU 故障设计原则
  • C++继承:从生活实例谈面向对象的精髓
  • 零基础设计模式——创建型模式 - 生成器模式
  • 时源芯微|六大步骤解决EMC问题
  • RAG系统的现实困境与突破:数据泥潭到知识自由
  • QT的自定义控件
  • 【题解-洛谷】B4302 [蓝桥杯青少年组省赛 2024] 出现奇数次的数
  • 数据库——redis
  • 测试--自动化测试概念
  • java21
  • BISS0001:高性能热释电红外感应IC的详细介绍
  • 学习STC51单片机10(芯片为STC89C52RC)
  • 近场探头阵列技术解析
  • (eNSP)主备接口备份配置
  • BitsAndBytesConfig参数描述
  • redisson-spring-boot-starter 版本选择
  • MySQL备份恢复:数据安全的终极指南
  • 基于Matlab建立不同信道模型
  • 苍穹外卖05 Redis常用命令在Java中操作Redis_Spring Data Redis使用方式店铺营业状态设置
  • 本特利内华达125768-01 RIM i/o模块规范
  • ESP.wdtFeed();的作用与功能,以及使用方法