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;
}