10

如何根据任意 Unix 时间戳(秒)确定加利福尼亚(太平洋时间)的星期几?我四处搜索,但没有找到 C++ 的内置库。

UTC 通常比 PT 早 8 小时,但简单地从 Unix 时间戳中减去 8 小时并创建一个tm结构是行不通的,因为这会降低夏令时的细微差别。

4

3 回答 3

10

这是使用Howard Hinnant 的日期库的方法。这是一个免费的 MIT 许可的 C++11/14 库,基于<chrono>

#include "tz.h"
#include <iostream>

int
main()
{
    using namespace std::chrono_literals;
    using namespace date;
    auto unix_timestamp = sys_seconds{1479664467s};
    auto zt = make_zoned("America/Los_Angeles", unix_timestamp);
    auto wd = weekday{floor<days>(zt.get_local_time())};
    std::cout << wd << '\n';
}

第一行只是构造 unix 时间戳(您可能从其他来源获得)。在 C++11 中,我们没有 chrono-literals,因此这一行将改为:

auto unix_timestamp = sys_seconds{seconds{1479664467}};

make_zoned()zoned_time<seconds>基于unix_timestamptime_zone“America/Los_Angeles”创建一个。Azoned_time是 atime_zone和 unix 时间戳的集合。

zoned_time您可以从with中获取当地时间zt.get_local_time()。这是一个chrono::time_point但没有与之关联的时钟。此时间点的精度将等于原始时间戳的精度(在本例中为秒)。

local_time您可以通过将 截断local_time为 的精度来获得一周中的当地日期days

floor<days>(zt.get_local_time())

而这个 day-precisiontime_point将转换为 type weekday

Aweekday可以被打印出来,参与weekday算术和比较,或者显式转换为unsigned[0, 6] 范围内的 a。

上面的程序输出:

Sun

这个库的时区部分是IANA 时区数据库的忠实代表。从 1800 年代中期到现在,它将在本地时区和 UTC 之间进行正确调整。如果您在 1800 年代中期之前传递库时间戳,则已知最早的 UTC 偏移量将向后推算到 -32768 年。您还可以将时间戳传递到 32767 年,并且当前的偏移量和夏令时规则将向前传播。

由于此库是基于 构建的<chrono>,因此它将处理<chrono>提供的任何精度。请注意,某些chrono::durationsnanoseconds的范围更有限(对于 +/-292 年nanoseconds)。

库/进程是线程安全的。也就是说,该程序直接与您指定的任何时区一起工作,而无需在后台更改计算机的时区或环境变量。它也不涉及 C 计时 API 的一部分,由于非常量函数局部静态,该部分已知不是线程安全的。

移植到最新版本的 gcc、clang 和 VS。

更新 C++20

上面的程序可以很容易地移植到 C++20(一些供应商在我写的时候支持这个,有些还没有):

#include <chrono>
#include <iostream>

int
main()
{
    using namespace std::chrono;
    auto unix_timestamp = sys_seconds{1479664467s};
    auto zt = zoned_time{"America/Los_Angeles", unix_timestamp};
    auto wd = weekday{floor<days>(zt.get_local_time())};
    std::cout << wd << '\n';
}
于 2016-11-20T18:20:03.367 回答
2

使用标准 UNIX C(非线程安全版本)的黑客方式:

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void print_times() {
  char my_buf[100];
  time_t now = time(NULL);
  printf(ctime(&now));
  strftime(my_buf, 100, "DoW=%w Tz=%Z\n", localtime(&now));
  printf(my_buf);
}

int main( int argc, char * argv[] ) {
  print_times();
  setenv("TZ", "America/Los_Angeles", 1);
  print_times();
  unsetenv("TZ");
  print_times();
}

不幸的是,TZenv var 或更改文件/etc/localtime是影响 UNIX 时间函数的时区设置的唯一方法(在后台 tzset()使用这些源来初始化一组共享变量,这些变量对所有库函数的时区设置进行编码) .

一个问题是您需要为您需要的区域提供 UNIX TZ字符串。zoneinfo格式是最简单且推荐使用的格式。我曾经tzselect为太平洋时间生成那个 TZ 值。

另请参阅man 5 localtimeman tzselect了解有关标准 UNIX 时区设置的信息。

于 2016-11-25T11:15:50.933 回答
1

你可以使用 Boost 库吗?如果您能找出您的 Posix 时区字符串(IEEE Std 1003.1,请参阅http://www.boost.org/doc/libs/1_62_0/doc/html/date_time/local_time.html#date_time.local_time.posix_time_zone)时区 (PST),那么下面的示例可能会有所帮助。

#include <iostream>
#include <ctime>
#include <boost/date_time.hpp>

int main(int argc, char ** argv)
{
    std::string posix_time_zone_string = "EST-05:00:00";  // you need to change this to the posix time representation of your desired timezone

    time_t unix_time = 1479641855;  //1479641855 = Sun, 20 Nov 2016 11:37:35 GMT

    boost::posix_time::ptime pt = boost::posix_time::from_time_t(unix_time);
    std::cout << "time in UTC: " << boost::posix_time::to_iso_extended_string(pt) << std::endl;


    boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone(posix_time_zone_string));

    boost::local_time::local_date_time dt_with_zone(pt, zone);
    std::cout << "time in local timezone: " << boost::posix_time::to_iso_extended_string(dt_with_zone.local_time()) << std::endl;

    // Get the week day (alternative 1)
    std::cout << "local week day integer: " << boost::local_time::to_tm(dt_with_zone).tm_wday << std::endl;   // 0=sunday, 1=monday, etc.

    // Get the week day (alternative 2)
    std::cout << "local week day name: " << dt_with_zone.local_time().date().day_of_week() << std::endl;
    std::cout << "local week day integer: " << int(dt_with_zone.local_time().date().day_of_week()) << std::endl;
    return 0;
}
于 2016-11-20T12:42:01.947 回答