boost posix_time时区问题

本文介绍了如何使用Boost库中的posix_time模块将格林威治时间转换为本地时间。通过创建特定时区的时间区对象并应用时间格式化,可以将获取的全局时间显示为本地时间。

引言
使用boost::posix_time::microsec_clock::local_time()获取本地时间
boost::posix_time::microsec_clock::universal_time()获取格林威治时间
boost::aiso::timer的expries_at()返回的格林威治时间

问题
有时候获取到格林威治时间时间,想输出为本地时间,

解决办法

	using namespace std;

    // Get current time, as an example 
    boost::posix_time::ptime dt = boost::posix_time::microsec_clock::universal_time();

    // Create a time_zone_ptr for the desired time zone and use it to create a local_date_time 
    boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone("EST+08:00:00"));
    boost::local_time::local_date_time dt_with_zone(dt, zone);

    std::stringstream strm;

    // Set the formatting facet on the stringstream and print the local_date_time to it. 
    // Ownership of the boost::local_time::local_time_facet object goes to the created std::locale object. 
    strm.imbue(std::locale(std::cout.getloc(), new boost::local_time::local_time_facet("%Y-%m-%d %H:%M:%S UTC%Q")));
    strm << dt_with_zone;

    // Print the stream's content to the console 
    cout << strm.str() << endl;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值