10

任意の Unix タイムスタンプ (秒) に基づいてカリフォルニア (太平洋時間) の曜日を特定するにはどうすればよいですか? 検索しましたが、C++ の組み込みライブラリは見つかりませんでした。

通常、UTC は PT より 8 時間進んでいますが、単純に Unix タイムスタンプから 8 時間を引いてtm構造体を作成しても、夏時間のニュアンスが割り引かれるため機能しません。

4

3 に答える 3

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 に答える