文章目錄
- 顯示當前日期時間
- 得到當前日期時間的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;
}