• Boost获取当前时间并格式化为字符串


    格式化为字符串

    时间转字符串有两种方法

    #include 
    #include 
    
    std::string getCurrentTime() {
        boost::posix_time::ptime currentTime = boost::posix_time::microsec_clock::local_time();    
        std::string formattedTime = boost::posix_time::to_iso_extended_string(currentTime);
        return formattedTime;
    }
    
    int main() {
        std::string currentTime = getCurrentTime();
        std::cout << "Current time: " << currentTime << std::endl;
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    格式化为指定格式字符串

    #include 
    #include 
    #include 
    
    std::string formatTime(const boost::posix_time::ptime& time, const std::string& format) {
        std::ostringstream oss;
        boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
        facet->format(format.c_str());
        oss.imbue(std::locale(std::cout.getloc(), facet));
        oss << time;
        return oss.str();
    }
    
    int main() {
        boost::posix_time::ptime currentTime = boost::posix_time::microsec_clock::local_time();
        std::string formattedTime = formatTime(currentTime, "%Y-%m-%d %H:%M:%S.%f");
        std::cout << "Formatted time: " << formattedTime << std::endl;
        
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    字符串转时间

    #include 
    #include 
    
    int main() {
        std::string formattedString = "2021-09-30 10:30:00";
        boost::posix_time::ptime time = boost::posix_time::time_from_string(formattedString);
    
        std::cout << "Converted time: " << time << std::endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    指定格式字符串转为时间

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main() {
        std::string formattedString = "2021-09-30 10:30:00";
        std::istringstream iss(formattedString);
        iss.imbue(std::locale(iss.getloc(), new boost::posix_time::time_input_facet("%Y-%m-%d %H:%M:%S")));
    
        boost::posix_time::ptime time;
        iss >> boost::posix_time::time_input(time);
    
        std::cout << "Converted time: " << time << std::endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    php在数字前面补0得到固定长度数字的两种方法
    MQ通道接收端绑定步骤
    道可云元宇宙每日资讯|2023焦作市文旅元宇宙产业发展座谈会举行
    Nginx
    Verilog刷题[hdlbits] :Always nolatches
    803_div2(Rising Sand, 接受军训!
    探索SPI:深入理解原理、源码与应用场景
    SFTP工具类-远程读取Linux服务器目录下所有文件
    最全HTTP/HTTPS面试题整理(二)
    A-Level陆续放榜,这些重要事宜需要关注
  • 原文地址:https://blog.csdn.net/izwmain/article/details/134556902