Linux下可以通过:
struct tm * gmtime (const time_t * timer); //获得gmt(UTC)时间
struct tm * localtime (const time_t * timer); //获得本地时间
- #include
- #include
- #include
- using namespace std;
-
- string getTimeStr(struct tm* t)
- {
- char tStr[50] = {0};
- snprintf(tStr, sizeof(tStr), "%04d-%02d-%02d %02d:%02d:%02d", (1900 + t->tm_year), ( 1 + t->tm_mon), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
- return string(tStr);
- }
-
- int main(){
- time_t ts = time(nullptr);
- struct tm* tUTC = gmtime(&ts);
- cout<<"UTC time:"<<getTimeStr(tUTC)<
- struct tm* tLocal = localtime(&ts);
- cout<<"Local time:"<<getTimeStr(tLocal)<
- return 0;
- }
运行程序输出:
UTC time:2022-08-21 03:54:53
Local time:2022-08-21 11:54:53
-
相关阅读:
基于openEuler系统执行Linux内核性能测试脚本(lkp-tests)的流程
Nginx 平滑升级-拒绝服务漏洞
C++ Core Guidelines 中文版 GSL
Sentinel基本使用与源码分析
Fiddler Everywhere 3.2.1 Crack
面试官:什么是三色标记
TheGraph 教程
HTML5期末作业:明星网站设计与实现——明星薛之谦介绍网页设计7个页面HTML+CSS+JavaScript
【微服务】微服务之Feign 与 Ribbon
微信小程序异步回调函数恶梦和解决办法
-
原文地址:https://blog.csdn.net/jiemashizhen/article/details/126449271