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
-
相关阅读:
【Python】类和对象的深入解析
navicat的安装和配置教程
第1章 初识AOP
docker mysql 主从配置
女生神经末梢最多的部位,女性身上哪里神经最多
代码质量与安全 | 想在发布竞赛中胜出?Sonar来帮你
Mybatis--动态SQL
c/c++内存管理详解
Python语言学习实战-内置函数property()的使用(附源码和实现效果)
spring中基础核心接口总结
-
原文地址:https://blog.csdn.net/jiemashizhen/article/details/126449271