全球作弊论坛  
  
查看: 58|回复: 0

C++实现跨平台时间转换及格式化

[复制链接]

10

主题

1

回帖

50

积分

鬼仙

Rank: 2

积分
50
发表于 2024-10-10 10:19:05 | 显示全部楼层 |阅读模式
  1. #include <cstdio>
  2. #include <chrono>
  3. #include <iostream>

  4. using namespace std;

  5. time_t GetTime()
  6. {
  7.     chrono::system_clock::time_point now = chrono::system_clock::now();
  8.     return chrono::system_clock::to_time_t(now);
  9. }
  10. tm* GetUtcTm()
  11. {
  12.     time_t t = GetTime();
  13.     return gmtime(&t);
  14. }
  15. tm* GetLocalTm()
  16. {
  17.     time_t t = GetTime();
  18.     return localtime(&t);
  19. }
  20. string GetUtcDateTime()
  21. {
  22.     auto t = GetTime();
  23.     auto localTm = gmtime(&t);
  24.     char buff[32];
  25.     strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm);
  26.     return string(buff);
  27. }
  28. string GetUtcDate()
  29. {
  30.     auto t = GetTime();
  31.     auto localTm = gmtime(&t);
  32.     char buff[32];
  33.     strftime(buff, 32, "%Y%m%d", localTm);
  34.     return string(buff);
  35. }
  36. string GetUtcTime()
  37. {
  38.     auto t = GetTime();
  39.     auto localTm = gmtime(&t);
  40.     char buff[32];
  41.     strftime(buff, 32, "%H:%M:%S", localTm);
  42.     return string(buff);
  43. }
  44. string GetUtcDateTimeWithMilliSecond()
  45. {
  46.     auto now = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
  47.     time_t t = chrono::system_clock::to_time_t(now);
  48.     int milliSecond = now.time_since_epoch().count() % 1000;
  49.     auto localTm = gmtime(&t);
  50.     char buff[32];
  51.     int len = strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm);
  52.     sprintf(buff + len, ".%03u", milliSecond);
  53.     return string(buff);
  54. }
  55. string GetLocalDateTime()
  56. {
  57.     auto t = GetTime();
  58.     auto localTm = localtime(&t);
  59.     char buff[32];
  60.     strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm);
  61.     return string(buff);
  62. }
  63. string GetLocalDate()
  64. {
  65.     auto t = GetTime();
  66.     auto localTm = localtime(&t);
  67.     char buff[32];
  68.     strftime(buff, 32, "%Y%m%d", localTm);
  69.     return string(buff);
  70. }
  71. string GetLocalTime()
  72. {
  73.     auto t = GetTime();
  74.     auto localTm = localtime(&t);
  75.     char buff[32];
  76.     strftime(buff, 32, "%H:%M:%S", localTm);
  77.     return string(buff);
  78. }
  79. string GetLocalDateTimeWithMilliSecond()
  80. {
  81.     auto now = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
  82.     time_t t = chrono::system_clock::to_time_t(now);
  83.     int milliSecond = now.time_since_epoch().count() % 1000;
  84.     auto localTm = localtime(&t);
  85.     char buff[32];
  86.     int len = strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm);
  87.     sprintf(buff + len, ".%03u", milliSecond);
  88.     return string(buff);
  89. }

  90. string GetLocalDateFromUnixTimeStamp(long long timeStamp)
  91. {
  92.     time_t time = timeStamp / 1000000000LL;
  93.     static char buff[16];
  94.     int len = strftime(buff, 16, "%Y%m%d", localtime(&time));
  95.     return string(buff);
  96. }
  97. string GetLocalTimeFromUnixTimeStamp(long long timeStamp)
  98. {
  99.     time_t time = timeStamp / 1000000000LL;
  100.     static char buff[16];
  101.     int len = strftime(buff, 16, "%H:%M:%S", localtime(&time));
  102.     return string(buff);
  103. }

  104. time_t GetTimeFromString(string dateTime, string format = "%04d%02d%02d-%02d:%02d:%02d")
  105. {
  106.     tm t;
  107.     int len = sscanf(dateTime.c_str(), format.c_str(), &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec);
  108.     t.tm_year -= 1900;
  109.     t.tm_mon -= 1;

  110.     return mktime(&t);
  111. }
  112. string ToUtcDateTime(time_t* time)
  113. {
  114.     char buff[32];
  115.     strftime(buff, 32, "%Y%m%d-%H:%M:%S", gmtime(time));
  116.     return string(buff);
  117. }
  118. string ToUtcDate(time_t* time)
  119. {
  120.     char buff[32];
  121.     strftime(buff, 32, "%Y%m%d", gmtime(time));
  122.     return string(buff);
  123. }
  124. string ToUtcTime(time_t* time)
  125. {
  126.     char buff[32];
  127.     strftime(buff, 32, "%H:%M:%S", gmtime(time));
  128.     return string(buff);
  129. }
  130. string ToLocalDateTime(time_t* time)
  131. {
  132.     char buff[32];
  133.     strftime(buff, 32, "%Y%m%d-%H:%M:%S", localtime(time));
  134.     return string(buff);
  135. }
  136. string ToLocalDate(time_t* time)
  137. {
  138.     char buff[32];
  139.     strftime(buff, 32, "%Y%m%d", localtime(time));
  140.     return string(buff);
  141. }
  142. string ToLocalTime(time_t* time)
  143. {
  144.     char buff[32];
  145.     strftime(buff, 32, "%H:%M:%S", localtime(time));
  146.     return string(buff);
  147. }

  148. int main()
  149. {
  150.     char buff[32];
  151.     strftime(buff, 32, "%Y%m%d %H:%M:%S", GetUtcTm());
  152.     cout << buff << endl;
  153.     strftime(buff, 32, "%Y%m%d %H:%M:%S", GetLocalTm());
  154.     cout << buff << endl << endl;

  155.     cout << GetUtcDateTime() << endl;
  156.     cout << GetUtcDate() << endl;
  157.     cout << GetUtcTime() << endl;
  158.     cout << GetUtcDateTimeWithMilliSecond() << endl << endl;

  159.     cout << GetLocalDateTime() << endl;
  160.     cout << GetLocalDate() << endl;
  161.     cout << GetLocalTime() << endl;
  162.     cout << GetLocalDateTimeWithMilliSecond() << endl << endl;

  163.     cout << GetLocalDateFromUnixTimeStamp(1635754321199409000L) << endl;
  164.     cout << GetLocalTimeFromUnixTimeStamp(1635754321199409000L) << endl << endl;

  165.     auto time = GetTimeFromString("20211101-08:12:01.224");
  166.     cout << ToUtcDateTime(&time) << endl;
  167.     cout << ToUtcDate(&time) << endl;
  168.     cout << ToUtcTime(&time) << endl;
  169.     cout << ToLocalDateTime(&time) << endl;
  170.     cout << ToLocalDate(&time) << endl;
  171.     cout << ToLocalTime(&time) << endl << endl;

  172.     return 0;
  173. }
复制代码
输出结果

  1. 20211103 05:46:16
  2. 20211103 13:46:16

  3. 20211103-05:46:16
  4. 20211103
  5. 05:46:16
  6. 20211103-05:46:16.218

  7. 20211103-13:46:16
  8. 20211103
  9. 13:46:16
  10. 20211103-13:46:16.219

  11. 20211101
  12. 16:12:01

  13. 20211101-00:12:01
  14. 20211101
  15. 00:12:01
  16. 20211101-08:12:01
  17. 20211101
  18. 08:12:01
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表