I have a ridiculous problem but I can't figure out...
I juste want to convert a timestamp string to a human readable date but the only date I have is completely wrong.
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctime>
int main()
{
char buf[80];
std::time_t epoch = std::atol("1645128077111");
strftime(buf, sizeof(buf), "%d-%m-%Y %H:%M:%S %Z", std::localtime(&epoch));
std::cout << buf << std::endl;
// Wanted value is
// GMT: Thursday 17 February 2022 20:01:17.111
// OR
// Your time zone: Thursday 17 February 2022 21:01:17.111 GMT 01:00
}
But my output is 13-01-54102 06:25:11 CET
which is completely wrong ! I'm sure is something stupid... But I can't find it so if anybody has the answer I would be grateful.
Thanks for all :)
CodePudding user response:
Thanks to everyone.
std::time_t epoch = std::atol("1645128077111")/1000;
strftime(buf, sizeof(buf), "%d-%m-%Y %H:%M:%S %Z", std::localtime(&epoch));
std::cout << buf << std::endl;
Give me the correct answer. Dividing by 1000 convert my milliseconds to seconds.
CodePudding user response:
Fwiw, in C 20:
#include <chrono>
#include <cstdlib>
#include <format>
#include <iostream>
int
main()
{
using namespace std;
using namespace std::chrono;
sys_time epoch{milliseconds{atoll("1645128077111")}};
cout << format("{:%Z: %A %e %B %Y %T}", epoch) << '\n';
}
Output:
UTC: Thursday 17 February 2022 20:01:17.111
Not available on all platforms yet. Encourage your vendor to catch up. :-)