首页 > 解决方案 > 如何导入 2 个不同的时区以检查两者是否都是星期一?

问题描述

希望这是有道理的,但我基本上希望程序检查它是否是 2 个不同时区的星期一。如果是,它应该打印一些东西,如果不打印其他东西。像这样的东西:

#include <iostream>
#include <sometimething>
using namespace std;

int a = timezone A;
int b = timezone B;

int main() 
{
  if (a == mday() || b == mday())
  {
    cout << "Something1\n";
  } 
  else
  {
    cout << "Something2\n";
  }
  return 0;
}

标签: c++

解决方案


你在什么环境下工作?

阅读本页了解本地时间功能。

他们提供了一个很好的例子:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h> // defines putenv in POSIX

int main()
{
    std::time_t t = std::time(nullptr);
    std::cout << "UTC:       " << std::put_time(std::gmtime(&t), "%c %Z") << '\n';
    std::cout << "local:     " << std::put_time(std::localtime(&t), "%c %Z") << '\n';
    // POSIX-specific:
    std::string tz = "TZ=Asia/Singapore";
    putenv(tz.data());
    std::cout << "Singapore: " << std::put_time(std::localtime(&t), "%c %Z") << '\n';
}

哪个输出:

UTC:       Fri Sep 15 14:16:29 2017 GMT
local:     Fri Sep 15 14:16:29 2017 UTC
Singapore: Fri Sep 15 22:16:29 2017 SGT

您可以定义自己的时区来检索其本地时间。


推荐阅读