You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++中如何将整数形式的ISO时间戳转换为相对秒数?

整数形式ISO时间戳转相对时间的C++实现方案

针对整数ISO时间戳(如202207032201对应2022-07-03 22:01)无法直接用于绘图X轴的问题,核心解决思路是先解析时间戳的年月日时分字段,再计算其与参考时间的时间差(秒/分钟数),具体实现如下:

1. 解析整数时间戳到时间结构体

通过数学运算拆分整数中的年、月、日、时、分字段,填充到C标准库的tm结构体中:

#include <ctime>
#include <stdint.h>

// 解析YYYYMMDDHHMM格式的整数时间戳到tm结构体
bool parseTimestamp(int64_t timestamp, std::tm& tm_out) {
    // 拆分各时间字段
    const int year = timestamp / 100000000;
    const int month = (timestamp / 1000000) % 100;
    const int day = (timestamp / 10000) % 100;
    const int hour = (timestamp / 100) % 100;
    const int minute = timestamp % 100;

    // 基础合法性校验
    if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 || 
        hour < 0 || hour > 23 || minute < 0 || minute > 59) {
        return false;
    }

    // 填充tm结构体(注意tm_mon从0开始,tm_year是1900年起的偏移量)
    tm_out.tm_year = year - 1900;
    tm_out.tm_mon = month - 1;
    tm_out.tm_mday = day;
    tm_out.tm_hour = hour;
    tm_out.tm_min = minute;
    tm_out.tm_sec = 0;
    tm_out.tm_isdst = -1; // 交由系统自动判断夏令时

    return true;
}

2. 计算相对参考时间的秒数

利用mktime将tm结构体转换为epoch时间戳(秒数),再通过difftime计算时间差:

// 计算目标时间戳与参考时间戳的秒数差
int64_t getRelativeSeconds(int64_t target_ts, int64_t reference_ts) {
    std::tm target_tm{}, ref_tm{};
    if (!parseTimestamp(target_ts, target_tm) || !parseTimestamp(reference_ts, ref_tm)) {
        return -1; // 解析失败返回错误标记
    }

    const time_t target_time = mktime(&target_tm);
    const time_t ref_time = mktime(&ref_tm);

    if (target_time == -1 || ref_time == -1) {
        return -1; // 时间转换失败
    }

    return static_cast<int64_t>(difftime(target_time, ref_time));
}

3. 计算相对参考时间的分钟数(C++11+)

如果需要分钟数,可使用C++11引入的chrono库,类型更安全:

#include <chrono>
#include <stdexcept>

// 将整数时间戳转换为chrono时间点
std::chrono::system_clock::time_point timestampToTimePoint(int64_t timestamp) {
    std::tm tm{};
    if (!parseTimestamp(timestamp, tm)) {
        throw std::invalid_argument("Invalid timestamp format");
    }

    const time_t t = mktime(&tm);
    return std::chrono::system_clock::from_time_t(t);
}

// 计算目标时间戳与参考时间戳的分钟数差
int64_t getRelativeMinutes(int64_t target_ts, int64_t reference_ts) {
    const auto target_tp = timestampToTimePoint(target_ts);
    const auto ref_tp = timestampToTimePoint(reference_ts);

    const auto diff = std::chrono::duration_cast<std::chrono::minutes>(target_tp - ref_tp);
    return diff.count();
}

使用示例

#include <iostream>

int main() {
    const int64_t ref_ts = 202207032201; // 参考基准时间
    const int64_t target_ts = 202207032215; // 需要计算的目标时间

    const int64_t sec_diff = getRelativeSeconds(target_ts, ref_ts);
    std::cout << "相对秒数: " << sec_diff << std::endl; // 输出840(14分钟)

    try {
        const int64_t min_diff = getRelativeMinutes(target_ts, ref_ts);
        std::cout << "相对分钟数: " << min_diff << std::endl; // 输出14
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}

注意事项

  • 时区处理:mktime默认使用本地时区,若时间戳为UTC标准时间,可改用非标准但常用的timegm函数(替代mktime),或手动调整时区偏移。
  • 增强校验:可在parseTimestamp中加入月份天数校验(如闰年2月的天数判断),避免无效时间戳引发错误。
  • 整数范围:若时间戳包含秒(格式YYYYMMDDHHMMSS),需确保用int64_t存储,避免32位整数溢出。

内容的提问来源于stack exchange,提问作者Sj L

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:42:25