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.pswp.cn/bicheng/82303.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python 训练营打卡 Day 34

GPU训练及类的call方法 一、GPU训练 与day33采用的CPU训练不同&#xff0c;今天试着让模型在GPU上训练&#xff0c;引入import time比较两者在运行时间上的差异 import torch # 设置GPU设备 device torch.device("cuda:0" if torch.cuda.is_available() else &qu…

Ubuntu22.04 系统安装Docker教程

1.更新系统软件包 #确保您的系统软件包是最新的。这有助于避免安装过程中可能遇到的问题 sudo apt update sudo apt upgrade -y 2.安装必要的依赖 sudo apt install apt-transport-https ca-certificates curl software-properties-common -y 3.替换软件源 原来/etc/apt/s…

深入解析前端 JSBridge:现代混合开发的通信基石与架构艺术

引言&#xff1a;被低估的通信革命 在移动互联网爆发式增长的十年间&#xff0c;Hybrid App&#xff08;混合应用&#xff09;始终占据着不可替代的地位。作为连接 Web 与 Native 的神经中枢&#xff0c;JSBridge 的设计质量直接决定了应用的性能上限与开发效率。本文将突破传…

ES 面试题系列「三」

1、在设计 Elasticsearch 索引时&#xff0c;如何考虑数据的建模和映射&#xff1f; 需要根据业务需求和数据特点来确定索引的结构。首先要分析数据的类型&#xff0c;对于结构化数据&#xff0c;如数字、日期等&#xff0c;要明确其数据格式和范围&#xff0c;选择合适的字段…

HTML5快速入门-常用标签及其属性(三)

HTML5快速入门-常用标签及其属性(三) 文章目录 HTML5快速入门-常用标签及其属性(三)音视频标签&#x1f3a7; <audio> 标签 — 插入音频使用 <source> 提供多格式备选&#xff08;提高兼容性&#xff09;&#x1f3a5; <video> 标签 — 插入视频&#x1f3b5…

Qt文件:XML文件

XML文件 1. XML文件结构1.1 基本结构1.2 XML 格式规则1.3 XML vs HTML 2. XML文件操作2.1 DOM 方式&#xff08;QDomDocument&#xff09;读取 XML写入XML 2.2 SAX 方式&#xff08;QXmlStreamReader/QXmlStreamWriter&#xff09;读取XML写入XML 2.3 对比分析 3. 使用场景3.1 …

day24Node-node的Web框架Express

1. Express 基础 1.1 什么是Express node的web框架有Express 和 Koa。常用Express 。 Express 是一个基于 Node.js 的快速、极简的 Web 应用框架,用于构建 服务器端应用(如网站后端、RESTful API 等)。它是 Node.js 生态中最流行的框架之一,以轻量、灵活和易用著称。 …

uniapp实现的简约美观的票据、车票、飞机票模板

采用 uniapp 实现的一款简约美观的票据模板&#xff0c;纯CSS、HTML实现&#xff0c;用户完全可根据自身需求进行更改、扩展&#xff1b;支持web、H5、微信小程序&#xff08;其他小程序请自行测试&#xff09;&#xff0c; 可到插件市场下载尝试&#xff1a; https://ext.dclo…

esp32+IDF V5.1.1版本编译freertos报错

error: portTICK_RATE_MS undeclared (first use in this function); did you mean portTICK_PERIOD_MS 解决方法: 使用命令 idf.py menuconfig 打开配置界面配置freeRtos 使能configENABLE_BACKWARD_COMPATIBLITY

vue 水印组件

Watermark.vue <script setup lang"ts"> import { ref, onMounted, onUnmounted, watch } from vue;interface Props {text?: string;fontSize?: number;color?: string;rotate?: number;zIndex?: number;gap?: number; }const props withDefaults(def…

hbuilder中h5转为小程序提交发布审核

【注意】 [HBuilder] 11:59:15.179 此应用 DCloud appid 为 __UNI__9F9CC77 &#xff0c;您不是这个应用的项目成员。1、联系这个应用的所有者&#xff0c;请求加入项目成员&#xff08;https://dev.dcloud.net.cn "成员管理"-"添加项目成员"&#xff09;…

QT之INI、JSON、XML处理

文章目录 INI文件处理写配置文件读配置文件 JSON 文件处理写入JSON读取JSON XML文件处理写XML文件读XML文件 INI文件处理 首先得引入QSettings QSettings 是用来存储和读取应用程序设置的一个类 #include "wrinifile.h"#include <QSettings> #include <QtD…

道德经总结

道德经 《道德经》是中国古代伟大哲学家老子所著&#xff0c;全书约五千字&#xff0c;共81章&#xff0c;分为“道经”&#xff08;1–37章&#xff09;和“德经”&#xff08;38–81章&#xff09;两部分。 《道德经》是一部融合哲学、政治、人生智慧于一体的经典著作。它提…

行为型:迭代器模式

目录 1、核心思想 2、实现方式 2.1 模式结构 2.2 实现案例 3、优缺点分析 4、适用场景 1、核心思想 目的&#xff1a;将遍历逻辑与数据存储结构解耦 概念&#xff1a;提供一种机制来按顺序访问集合中的各元素&#xff0c;而不需要知道集合内部的构造 举例&#xff1a;…

人脸识别技术合规备案最新政策详解

《人脸识别技术应用安全管理办法》将于2025年6月1日正式实施&#xff0c;该办法从技术应用、个人信息保护、技术替代、监管体系四方面构建了人脸识别技术的治理框架&#xff0c;旨在平衡技术发展与安全风险。 一、明确技术应用的边界 公共场所使用限制&#xff1a;仅在“维护公…

如何把vue项目部署在nginx上

1&#xff1a;在vscode中把vue项目打包会出现dist文件夹 按照图示内容即可把vue项目部署在nginx上

奇好 PDF安全加密 + 自由拆分合并批量处理 OCR 识别

各位办公小能手们&#xff0c;你们好呀&#xff01;今天我要给大家介绍一款超厉害的软件——奇好PDF。它就像是一个PDF文档处理的超级大管家&#xff0c;啥功能都有&#xff0c;格式转换、编辑、提取、安全保护这些统统不在话下&#xff0c;不管是办公、学习&#xff0c;还是设…

Docker-Harbor 私有镜像仓库使用指南

1.用户管理 为项目创建专用用户&#xff0c;并配置权限&#xff0c;确保该用户能够顺利推送镜像到 Harbor 仓库&#xff0c;确保镜像推送操作的安全性和便捷性。 创建完成后可以根据需要选择是否设置为管理员 角色 权限描述 适用场景 系统管理员 拥有系统的完全控制权限 运维…

HomeAssistant开源的智能家居docker快速部署实践笔记(CentOS7)

1. SGCC_Electricity 应用介绍 SGCC_Electricity 是一个用于将国家电网&#xff08;State Grid Corporation of China&#xff0c;简称 SGCC&#xff09;的电费和用电量数据接入 Home Assistant 的自定义集成组件。通过该应用&#xff0c;用户可以实时追踪家庭用电量情况&…

maven 3.0多线程编译提高编译速度

mvn package 默认只使用 单线程 来执行构建生命周期&#xff08;即顺序地构建每一个模块&#xff09;。 如果你使用的是多模块项目&#xff0c;Maven 从 3.0 开始提供了**并行构建&#xff08;parallel build&#xff09;**的能力&#xff0c;但它不是默认开启的。 如何启用多…