gpt4 book ai didi

c++ - boost local_date_time : how to set a specific time information(hour, 分钟和秒)

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:18:23 25 4
gpt4 key购买 nike

我有一个与不同时区有关的问题。有一个文件存储本地开始和结束时间。例如第一个条目在纽约时区,而第二个条目在香港时区。

80000-150000
100000-180000

所以我尝试使用 boost::local_date_time 通过 local_sec_clock::local_time(poTimezone) 获取本地当前时间,其中 poTimezoneboost::local_time::time_zone_ptr。然后为这个新对象设置小时、分钟和秒。但是,我发现没有办法为 local_date_time 对象设置它。有没有人对此有任何想法?

最佳答案

local_data_time

我猜一个样本会说出一千多个单词:

#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/local_time/local_time_io.hpp>
#include <boost/make_shared.hpp>
#include <iostream>

int main() {

namespace lt = boost::local_time;
namespace pt = boost::posix_time;
using date = boost::gregorian::date;

lt::tz_database db;
db.load_from_file("/home/sehe/custom/boost/libs/date_time/data/date_time_zonespec.csv");

//for (auto region : db.region_list()) std::cout << region << "\n";

auto NY = db.time_zone_from_region("America/New_York");
auto HK = db.time_zone_from_region("Asia/Hong_Kong");

lt::local_date_time first (date {2015,1,1}, pt::time_duration{10,12,0}, NY, false);
lt::local_date_time second(date {2015,1,1}, pt::time_duration{10,12,0}, HK, false);

lt::local_time_period period(first, second);
std::cout << "period: " << period << "\n";
std::cout << "duration: " << period.length() << "\n";
}

打印

period: [2015-Jan-01 10:12:00 EST/2015-Jan-01 10:11:59.999999 HKT]
duration: -13:00:00

查看 Live On Coliru (没有时区数据库)


奖金

更新现有 ldt 上的时间字段的两种不同方法:

first  = lt::local_date_time(first.date(),  pt::hours(7) +  pt::seconds(59), first.zone(),  first.is_dst());
//
second = lt::local_date_time(second.date(), pt::time_duration(7, 0, 59), second.zone(), second.is_dst());

查看 Live as well

关于c++ - boost local_date_time : how to set a specific time information(hour, 分钟和秒),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30159228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com