boost1.62+openssl1.1.1g+C++11跨平台部分时间格式转换


    std::string current_date_time()
    {
        std::ostringstream ss;
        ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S")));
        ss << boost::posix_time::second_clock::local_time();

        return ss.str();
    }

    time_t date_time_to_time_t(const std::string &date_time)
    {
        boost::posix_time::ptime time;
        std::istringstream is(date_time);
        is.imbue(std::locale(is.getloc(), new boost::posix_time::time_input_facet("%Y-%m-%d %H:%M:%S")));
        is >> time;
        struct tm st = boost::posix_time::to_tm(time);
        return mktime(&st);
    }

    time_t asn1_utc_time_to_time_t(const ASN1_UTCTIME *asn1_utc_time)
    {
        return date_time_to_time_t(asn1_utc_time_to_date_time(asn1_utc_time));
    }

    std::string asn1_utc_time_to_date_time(const ASN1_UTCTIME *asn1_utc_time)
    {
        if (nullptr == asn1_utc_time)
            //exception here
            return std::string();

        struct tm st = {0};
        ASN1_TIME_to_tm(asn1_utc_time, &st);
        boost::posix_time::ptime ptime = boost::posix_time::ptime_from_tm(st);
        std::ostringstream ss;
        ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S")));
        ss << boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(ptime);

        return ss.str();
    }

    std::string time_t_to_date_time(const time_t &time)
    {
        boost::posix_time::ptime ptime = boost::posix_time::from_time_t(time);

        std::ostringstream ss;
        ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S")));
        ss << boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(ptime);

        return ss.str();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值