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

huangapple go评论83阅读模式
英文:

Convert normal date and time to epoch time and vice versa using clang compiler

问题

我想使用clang编译器将普通日期和时间转换为epoch时间,反之亦然。
我有一段C++代码,它在g++下正常工作,但在clang++下不起作用。
我认为是strptimestrftime在clang++下不起作用。
我在我的系统上安装了clang 14.0.0版本。

#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::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);
    char *time_T = new char[(timeT.length() + 1)];
    strcpy(time_T, timeT.c_str());
    struct tm tm;
    strptime(time_T, "%d-%m-%Y %H:%M:%S", &tm);
    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 timestamps in microseconds: " << (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 timestamps in microseconds: 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 timestamps in microseconds: 18446744073708551626
First timestamp converted from epoch: 19-01-586524 13:31::551615
Second timestamp converted from epoch: 19-01-586524 13:31::551625
英文:

I want to convert normal date and time to epoch time and vice versa using clang compiler.
I have C++ code which works properly with g++ but doesn't work with clang++.
I think it strptime and strftime is not working in clang++
I have clang 14.0.0 version install on my system.

// #include &lt;chrono&gt;
#include &lt;iostream&gt;
#include &lt;cstring&gt;
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 &lt;&lt; day &lt;&lt; &quot;-&quot; &lt;&lt; month &lt;&lt; &quot;-&quot; &lt;&lt; year &lt;&lt; &quot; &quot; &lt;&lt; hour &lt;&lt; &quot;:&quot; &lt;&lt; min &lt;&lt; &quot;:&quot; &lt;&lt; second &lt;&lt; &quot;:&quot; &lt;&lt; microsecond &lt;&lt; std::endl;
std::string timeT = std::to_string(day) + &quot;-&quot; + std::to_string(month) + &quot;-&quot; + std::to_string(year) + &quot; &quot; + std::to_string(hour) + &quot;:&quot; + std::to_string(min) + &quot;:&quot; + std::to_string(second);
// std::cout &lt;&lt; timeT &lt;&lt; std::endl;
char *time_T = new char[(timeT.length() + 1)];
strcpy(time_T, timeT.c_str());
// std::cout &lt;&lt; time_T &lt;&lt; std::endl;
struct tm tm;
strptime(time_T, &quot;%d-%m-%Y %H:%M:%S&quot;, &amp;tm);
// std::cout &lt;&lt; mktime(&amp;tm) &lt;&lt; std::endl;
uint64_t t = mktime(&amp;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, &quot;%d-%m-%Y %H:%M:%S&quot;, localtime(&amp;epochTimeInSec));
timeT = time_T;
timeT = timeT + &quot;:&quot; + std::to_string(microsecond);
return timeT; 
}
int main()
{
std::cout &lt;&lt; &quot;Year changed 2004 to 2005\n&quot;;
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 &lt;&lt; &quot;First timestamp in epoch :&quot; &lt;&lt; time_info_1 &lt;&lt; &quot;\n&quot;;
std::cout &lt;&lt; &quot;Second timestamp in epoch: &quot; &lt;&lt; time_info_2 &lt;&lt; &quot;\n\n&quot;;
std::cout &lt;&lt; &quot;Deffernce between both timestamp in microsecond: &quot; &lt;&lt; (time_info_2 - time_info_1) &lt;&lt; &quot;\n\n&quot;;
std::cout &lt;&lt; &quot;First timestamp converted from epoch: &quot; &lt;&lt; epochToTime(time_info_1) &lt;&lt; &quot;\n&quot;;
std::cout &lt;&lt; &quot;Second timestamp converted from epoch: &quot; &lt;&lt; epochToTime(time_info_2) &lt;&lt; &quot;\n&quot;;
return 0;
}

g++ output:

Year changed 2004 to 2005
First timestamp in epoch :1104517799999999
Second timestamp in epoch: 1104517800000009
Deffernce 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++ output:

Year changed 2004 to 2005
First timestamp in epoch :18446744073709551615
Second timestamp in epoch: 18446744073708551625
Deffernce 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

答案1

得分: 1

Reading and following manuals make magic.

> this function does not initialize tm but stores only the values specified.

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

The manual has the example, that are also worth to look at

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

But C++ zero-initialization looks better.

英文:

Reading and following manuals make magic.

> this function does not initialize tm but stores only the values specified.

struct tm tm;  // wrong!

Must be

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

The manual has the example, that are also worth to look at

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

But C++ zero-initialization looks better.

huangapple
  • 本文由 发表于 2023年7月13日 14:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定