C++資料格式化4 - 格式化時間戳

陌尘(MoChen)發表於2024-06-18
  • 1. 關鍵詞
  • 2. strfmt.h
  • 3. strfmt.cpp
  • 4. 測試程式碼
  • 5. 執行結果
  • 6. 原始碼地址

1. 關鍵詞

C++ 資料格式化 字串處理 std::string 時間戳 跨平臺

2. strfmt.h

#pragma once

#include <string>
#include <cstdint>
#include <sstream>
#include <iomanip>
#include "timeutil.h"

namespace cutl
{
    /**
     * @brief Format a timestamp to a human-readable string with a given format.
     *
     * @param second the timestamp in seconds.
     * @param local whether to use local time or UTC time, default is local time.
     * If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
     * @param fmt the format of the formatted string. useage like std::put_time, see https://en.cppreference.com/w/cpp/io/manip/put_time
     * @return std::string the formatted string.
     */
    std::string fmt_timestamp(uint64_t second, bool local, const std::string &fmt);
    /**
     * @brief Format a timestamp to a human-readable string.
     *
     * @param second the timestamp in seconds.
     * @param local whether to use local time or UTC time, default is local time.
     * If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
     * @return std::string the formatted string.
     */
    std::string fmt_timestamp_s(uint64_t second, bool local = true);
    /**
     * @brief Format a timestamp to a human-readable string.
     *
     * @param ms the timestamp in milliseconds.
     * @param local whether to use local time or UTC time, default is local time.
     * If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
     * @return std::string the formatted string.
     */
    std::string fmt_timestamp_ms(uint64_t ms, bool local = true);
    /**
     * @brief Format a timestamp to a human-readable string.
     *
     * @param us the timestamp in microseconds.
     * @param local whether to use local time or UTC time, default is local time.
     * If local is true, the function will format the timestamp to local time, otherwise, it will format the timestamp to UTC time.
     * @return std::string the formatted string.
     */
    std::string fmt_timestamp_us(uint64_t us, bool local = true);
} // namespace cutl

3. strfmt.cpp

#include <sstream>
#include <iomanip>
#include <bitset>
#include "strfmt.h"

namespace cutl
{
    // https://blog.csdn.net/u010087712/article/details/50731222
    std::string fmt_timestamp(uint64_t second, bool local, const std::string &fmt)
    {
        std::time_t t(second);
        struct tm datetime;
        if (local)
        {
            datetime = localtime_security(t);
        }
        else
        {
            datetime = gmtime_security(t);
        }

        std::stringstream ss;
        ss << std::put_time(&datetime, fmt.c_str());
        return ss.str();
    }

    // 格式化時間戳,second單位:秒
    std::string fmt_timestamp_by_unit(uint64_t t, timeunit unit, bool local)
    {
        uint64_t s = 0;
        std::string extension;
        switch (unit)
        {
        case timeunit::s:
            s = t;
            break;
        case timeunit::ms:
        {
            s = t / THOUSAND;
            auto ms = t % THOUSAND;
            extension += "." + fmt_uint(ms, 3);
        }
        break;
        case timeunit::us:
        {
            s = t / MILLION;
            auto us = t % MILLION;
            extension += "." + fmt_uint(us, 6);
        }
        break;
        default:
            break;
        }

        std::string fmt("%Y-%m-%d %H:%M:%S");
        auto time_str = fmt_timestamp(s, local, fmt);
        time_str += extension;
        return time_str;
    }

    std::string fmt_timestamp_s(uint64_t t, bool local)
    {
        return fmt_timestamp_by_unit(t, timeunit::s, local);
    }

    std::string fmt_timestamp_ms(uint64_t t, bool local)
    {
        return fmt_timestamp_by_unit(t, timeunit::ms, local);
    }

    std::string fmt_timestamp_us(uint64_t t, bool local)
    {
        return fmt_timestamp_by_unit(t, timeunit::us, local);
    }
} // namespace cutl

4. 測試程式碼

#include "common.hpp"
#include "strfmt.h"

void TestFormatTimestamp()
{
    PrintSubTitle("TestFormatTimestamp");
    // timestamp
    auto curTimeS = cutl::timestamp(cutl::timeunit::s);
    auto curTimeMS = cutl::timestamp(cutl::timeunit::ms);
    auto curTimeUS = cutl::timestamp(cutl::timeunit::us);
    std::cout << "current datetime s: " << cutl::fmt_timestamp_s(curTimeS) << std::endl;
    std::cout << "current datetime s in UTC: " << cutl::fmt_timestamp(curTimeS, false, "%Y/%m/%d %H:%M:%S") << std::endl;
    std::cout << "current datetime ms: " << cutl::fmt_timestamp_ms(curTimeMS) << std::endl;
    std::cout << "current datetime ms in UTC: " << cutl::fmt_timestamp_ms(curTimeMS, false) << std::endl;
    std::cout << "current datetime us: " << cutl::fmt_timestamp_us(curTimeUS) << std::endl;
}

5. 執行結果

----------------------------------------TestFormatTimestamp-----------------------------------------
current datetime s: 2024-06-18 23:22:46
current datetime s in UTC: 2024/06/18 15:22:46
current datetime ms: 2024-06-18 23:22:46.363
current datetime ms in UTC: 2024-06-18 15:22:46.363
current datetime us: 2024-06-18 23:22:46.363835

6. 原始碼地址

更多詳細程式碼,請檢視本人寫的C++ 通用工具庫: common_util, 本專案已開源,程式碼簡潔,且有詳細的文件和Demo。

相關文章