【问题标题】:How to get the current time and date C++ UTC time not local如何获取当前时间和日期 C++ UTC 时间不是本地时间
【发布时间】:2021-09-26 04:45:25
【问题描述】:

我想知道如何在 C++ Linux 中获取 UTC 时间或任何其他时区(只是本地时间)。

我想做类似的事情:int Minutes = time.now(Minutes) 以获取并存储该确切时间的年、月、日、小时、分钟和秒。
我该怎么做?

我需要多次重复这个过程;我想知道最新最好的方法。

【问题讨论】:

  • 您可能正在寻找time_t now = time(nullptr); tm* now_details = gmtime(&now); 然后例如now_details->tm_min 会给你几分钟。
  • 您好@IgorTandetnik,我不知道该代码的含义或如何使用它,您能解释一下吗? time_ttime(nullptr)tm* now_details 是什么意思?我认为 gmtime(&now) 会使用 UTC/GMT 时间吗?我很新,希望能得到很多帮助。
  • 它们是标准库中的类型和函数。在您的编译器文档或您最喜欢的搜索引擎中查找它们。

标签: c++ linux datetime time


【解决方案1】:

您正在time.h 库中寻找gmtime 函数,它为您提供UTC 时间。这是一个例子:

#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, gmtime */

int main ()
{
  time_t rawtime;
  struct tm * ptm;
  // Get number of seconds since 00:00 UTC Jan, 1, 1970 and store in rawtime
  time ( &rawtime );
  // UTC struct tm
  ptm = gmtime ( &rawtime );
  // print current time in a formatted way
  printf ("UTC time: %2d:%02d\n", ptm->tm_hour, ptm->tm_min);

  return 0;
}

查看这些来源:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 2012-10-29
    • 2019-07-28
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多