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
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/82303.shtml
英文地址,請注明出處:http://en.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;但它不是默認開啟的。 如何啟用多…