使用 clang 编译器将正常日期和时间转换为纪元时间,反之亦然

问题描述 投票:0回答:1

我想使用 clang 编译器将正常日期和时间转换为纪元时间,反之亦然。

我的 C++ 代码可以与 g++ 一起正常工作,但不能与 clang++ 一起工作。

我认为

strptime
strftime
在 clang++ 中不起作用 我的系统上安装了 clang 14.0.0 版本。

// #include <chrono>
#include <iostream>
#include <cstring>

uint64_t timeToEpoch(uint64_t day, uint64_t month, uint64_t year, uint64_t hour, uint64_t min, uint64_t second, uint64_t microsecond){
    // std::cout << day << "-" << month << "-" << year << " " << hour << ":" << min << ":" << second << ":" << microsecond << std::endl;
    std::string timeT = std::to_string(day) + "-" + std::to_string(month) + "-" + std::to_string(year) + " " + std::to_string(hour) + ":" + std::to_string(min) + ":" + std::to_string(second);
    // std::cout << timeT << std::endl;
    char *time_T = new char[(timeT.length() + 1)];
    strcpy(time_T, timeT.c_str());
    // std::cout << time_T << std::endl;
    struct tm tm;
    strptime(time_T, "%d-%m-%Y %H:%M:%S", &tm);
    // std::cout << mktime(&tm) << std::endl;
    uint64_t t = mktime(&tm);
    t = (t * 1000000) + microsecond;
    return t;
}

std::string epochToTime(uint64_t epochTime){
    std::string timeT;
    uint64_t microsecond = epochTime % 1000000;
    time_t epochTimeInSec = (epochTime - microsecond) / 1000000;
    char time_T[21];
    strftime(time_T, 20, "%d-%m-%Y %H:%M:%S", localtime(&epochTimeInSec));
    timeT = time_T;
    timeT = timeT + ":" + std::to_string(microsecond);
    return timeT; 
}

int main()
{
    std::cout << "Year changed 2004 to 2005\n";
    uint64_t time_info_1 = timeToEpoch(31, 12, 2004, 23, 59, 59, 999999);
    uint64_t time_info_2 = timeToEpoch(1, 1, 2005, 0, 0, 0, 9);
    std::cout << "First timestamp in epoch :" << time_info_1 << "\n";
    std::cout << "Second timestamp in epoch: " << time_info_2 << "\n\n";
    std::cout << "Difference between both timestamp in microsecond: " << (time_info_2 - time_info_1) << "\n\n";
    std::cout << "First timestamp converted from epoch: " << epochToTime(time_info_1) << "\n";
    std::cout << "Second timestamp converted from epoch: " << epochToTime(time_info_2) << "\n";
    return 0;
}

g++ 输出:

Year changed 2004 to 2005
First timestamp in epoch :1104517799999999
Second timestamp in epoch: 1104517800000009

Difference between both timestamp in microsecond: 10

First timestamp converted from epoch: 31-12-2004 23:59:59:999999
Second timestamp converted from epoch: 01-01-2005 00:00:00:9

clang++ 输出:

Year changed 2004 to 2005
First timestamp in epoch :18446744073709551615
Second timestamp in epoch: 18446744073708551625

Difference between both timestamp in microsecond: 18446744073708551626

First timestamp converted from epoch: 19-01-586524 13:31::551615
Second timestamp converted from epoch: 19-01-586524 13:31::551625
c++ g++ clang++ epoch
1个回答
1
投票

阅读并遵循手册创造奇迹。

此函数不会初始化

tm
,而是仅存储指定的值。

struct tm tm;  // wrong!

一定是

tm tm{};  // struct is unnecessary in C++

手册有例子,也值得一看

memset(&tm, 0, sizeof(tm));

但是 C++ 零初始化看起来更好。

© www.soinside.com 2019 - 2024. All rights reserved.