英文:
Convert normal date and time to epoch time and vice versa using clang compiler
问题
我想使用clang编译器将普通日期和时间转换为epoch时间,反之亦然。
我有一段C++代码,它在g++下正常工作,但在clang++下不起作用。
我认为是strptime
和strftime
在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 <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 << "Deffernce 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++ 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(&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(&tm, 0, sizeof(tm));
But C++ zero-initialization looks better.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论