boost local_date_time:如何设置特定的时间信息(小时,分钟和秒)

时间:2015-05-11 03:29:18

标签: c++ datetime boost

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

80000-150000
100000-180000

所以我尝试使用boost :: local_date_time来获取local_sec_clock::local_time(poTimezone) poTimezoneboost::local_time::time_zone_ptr的本地当前时间。var msgs = { shelter: 'shelter for {count} people', tent: 'tent for {count} people', injured: '{count} people injured', displaced: '{count} people displaced' }; tems = ["shelter", "tent", "shelter", "injured", "displaced", "displaced"]; magnitude = [5, 12, 6, 9, 13, 2]; var tmp = {}; for (var i = 0; i < tems.length; i++) { tmp[tems[i]] = (tmp[tems[i]] || 0) + 1; } var array = []; for (var key in tmp) { array.push(msgs[key].replace('{count}', tmp[key])) } console.log(array) face_data。然后将小时,分钟和秒设置为此新对象。但是,我发现没有办法为local_date_time对象设置它。有没有人对此有任何想法?

1 个答案:

答案 0 :(得分:1)

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