Convert std::chrono::milliseconds to ISO 8601 string - c++

I've a std::chrono::milliseconds representing epoch unix time in milliseconds. I need to convert it into a string that follows the ISO 8601 format, like 2020-02-25T00:02:43.000Z.
Using date library I was able to parse it, with following GetMillisecondsFromISO5601String method:
#include "TimeConversion.hpp"
#include <date/date.h>
using std::string;
using std::string_view;
using std::chrono::milliseconds;
string TimeConversion::GetISO8601TimeStringFrom(const milliseconds& ms) {
std::stringstream ss;
date::to_stream(ss, "%FT%T%z", ms);
return ss.str();
}
milliseconds TimeConversion::GetMillisecondsFromISO5601String(string_view s) {
std::istringstream in{ std::move(string(s)) };
in.exceptions(std::ios::failbit);
date::sys_time<milliseconds> tp;
if (*s.rbegin() == 'z') {
in >> date::parse("%FT%T%z", tp);
}
else if (*s.rbegin() == 'Z') {
in >> date::parse("%FT%T%Z", tp);
}
else {
in >> date::parse("%FT%T%", tp);
}
return tp.time_since_epoch();
}
How can I do the inverse? I was trying to_stream in the GetISO8601TimeStringFrom method as you can see but without any results (it returns me an empty string).

The problem is that you're treating a time duration (milliseconds) as a time_point (time_point<system_clock, milliseconds>). All you need to do is convert the duration to time_point with explicit conversion syntax. The date lib has a convenience type alias for this type: sys_time<milliseconds>:
string TimeConversion::GetISO8601TimeStringFrom(const milliseconds& ms) {
date::sys_time<milliseconds> tp{ms};
// continue using tp ...
You may also use the more convenient format function in place of to_stream:
string TimeConversion::GetISO8601TimeStringFrom(const milliseconds& ms) {
return date::format("%FT%T%z", date::sys_time<milliseconds>{ms});
}

Related

how to use localtime() correctly?

In one of my utility programs, localtime() is used to covert unix timestamps to human readable date time.
The following code used to work in VS2010 while it fails to work in VS2019:
std::string sec = "1234123456";
int nsec = atoi(sec.c_str());
tm* t = localtime((time_t*)&nsec); // return null pointer
If I change the code in the following way, it will work also in VS2019:
std::string sec = "1234123456";
int nsec = atoi(sec.c_str());
time_t tt = nsec;
tm* t = localtime(&tt); // works
I have no idea why the additional int to time_t conversion is needed, any suggestion would be appreciated.
On most (if not all) modern compilers time_t is now a 64-bit integer. (time_t*)&nsec is therefore undefined behaviour as you are casting from one pointer type to a different one.
You fixed version is well defined but you will run into the reason that time_t is now 64-bit as 32-bit numbers will only work for times up to 2038 (assuming time_t is using the Unix epoch).
Unfortunately c++ doesn't provide a simple method for converting a string to time_t, to do it properly you'd need something like this:
#include <iostream>
#include <charconv>
time_t str_to_time_t(const std::string& str)
{
auto begin = str.c_str();
auto end = begin + str.size();
time_t time;
auto result = std::from_chars(begin, end, time);
if (result.ec != std::errc())
{
throw std::system_error(std::make_error_code(result.ec));
}
if (result.ptr != end)
{
throw std::invalid_argument("invalid time_t string");
}
return time;
}
int main()
{
std::string sec = "1234123456";
auto nsec = str_to_time_t(sec);
tm* t = localtime((time_t*)&nsec);
if (t)
{
std::cout << "parsed OK\n";
}
}

How to convert chrono::seconds to string in HH:MM:SS format in C++?

I have a function which accepts second as argument and returns a string in HH:MM:SS format. Without std::chrono, I can implement it like this:
string myclass::ElapsedTime(long secs) {
uint32_t hh = secs / 3600;
uint32_t mm = (secs % 3600) / 60;
uint32_t ss = (secs % 3600) % 60;
char timestring[9];
sprintf(timestring, "%02d:%02d:%02d", hh,mm,ss);
return string(timestring);
}
Using std::chrono, I can convert the argument to std::chrono::seconds sec {seconds};.
But the how can I convert it to string with the format?
I saw the good video tutorial from Howard Hinnant in https://youtu.be/P32hvk8b13M. Unfortunately, there is no example for this case.
Using Howard Hinnant's header-only date.h library it looks like ths:
#include "date/date.h"
#include <string>
std::string
ElapsedTime(std::chrono::seconds secs)
{
return date::format("%T", secs);
}
If you want to write it yourself, then it looks more like:
#include <chrono>
#include <string>
std::string
ElapsedTime(std::chrono::seconds secs)
{
using namespace std;
using namespace std::chrono;
bool neg = secs < 0s;
if (neg)
secs = -secs;
auto h = duration_cast<hours>(secs);
secs -= h;
auto m = duration_cast<minutes>(secs);
secs -= m;
std::string result;
if (neg)
result.push_back('-');
if (h < 10h)
result.push_back('0');
result += to_string(h/1h);
result += ':';
if (m < 10min)
result.push_back('0');
result += to_string(m/1min);
result += ':';
if (secs < 10s)
result.push_back('0');
result += to_string(secs/1s);
return result;
}
In C++20, you'll be able to say:
std::string
ElapsedTime(std::chrono::seconds secs)
{
return std::format("{:%T}", secs);
}
Once C++20 implementations land, you'll be able to do the following (untested code):
std::chrono::hh_mm_ss<std::chrono::seconds> tod{std::chrono::seconds(secs)};
std::cout << tod;
See time.hms.overview for more info.

C++ Convert a string timestamp to std::chrono::system_clock::time_point

I am trying to convert a string timestamp expressed in the following format: "28.08.2017 03:59:55.0007" to a std::chrono::system_clock::time_point by preserving the microseconds precision.
Is there any way to achieve this by using the standard library or boost?
Thanks.
I'd make use of Howard Hinnant's date library https://howardhinnant.github.io/date/date.html
e.g.:
std::stringstream str( "28.08.2017 03:59:55.0007" );
str.imbue( std::locale() );
std::chrono::time_point< std::chrono::system_clock, std::chrono::microseconds > result;
date::from_stream( str, "%d.%m.%Y %H:%M:%S", result );
std::cout << result.time_since_epoch().count();
Thought I'd add an answer since there isn't one available that exclusively uses the standard.
Given the input: istringstream timestamp("28.08.2017 03:59:55.0007"), this could be converted to a tm via get_time, but for the fractional seconds. The fractional seconds would need to be converted manually (this could be done by constructing chrono::microseconds from the rounded remainder divided by the micro ratio.) All this could be combined into something like this:
tm tmb;
double r;
timestamp >> get_time(&tmb, "%d.%m.%Y %T") >> r;
const auto output = chrono::time_point_cast<chrono::microseconds>(chrono::system_clock::from_time_t(mktime(&tmb))) + chrono::microseconds(lround(r * micro::den));
Live Example
One implementation can be:
#include <ctime>
#include <cmath>
#include <chrono>
#include <string>
#include <cstdint>
#include <stdexcept>
std::chrono::system_clock::time_point parse_my_timestamp(std::string const& timestamp) {
auto error = [&timestamp]() { throw std::invalid_argument("Invalid timestamp: " + timestamp); };
std::tm tm;
auto fraction = ::strptime(timestamp.c_str(), "%d.%m.%Y %H:%M:%S", &tm);
if(!fraction)
error();
std::chrono::nanoseconds ns(0);
if('.' == *fraction) {
++fraction;
char* fraction_end = 0;
std::chrono::nanoseconds fraction_value(std::strtoul(fraction, &fraction_end, 10));
if(fraction_end != timestamp.data() + timestamp.size())
error();
auto fraction_len = fraction_end - fraction;
if(fraction_len > 9)
error();
ns = fraction_value * static_cast<std::int32_t>(std::pow(10, 9 - fraction_len));
}
else if(fraction != timestamp.data() + timestamp.size())
error();
auto seconds_since_epoch = std::mktime(&tm); // Assumes timestamp is in localtime. For UTC use timegm.
auto timepoint_ns = std::chrono::system_clock::from_time_t(seconds_since_epoch) + ns;
return std::chrono::time_point_cast<std::chrono::system_clock::duration>(timepoint_ns);
}

Milliseconds since epoch to dateformat C++

I have the milliseconds since epoch (windows/gregorian) for a specific time in long long int and would like to convert it to human time such as yy-mm-dd-hh-mm-ss-milli. (My platform: Windows 7, 64bit)
Unfortunately, all solutions I have found so far can't deal with the milli second (long long int) part.
C++11 API is incomplete, so I had to invent a bicycle:
static long getTs() {
struct timeval tp;
gettimeofday(&tp, NULL);
long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
return ms;
}
static string format(long ts,string fmt,int cutBack=0){
time_t tt = ts/1000;
int microsec = ts%1000;
struct std::tm * ptm = std::localtime(&tt);
string fmtms=std::regex_replace(fmt, std::regex("%ms"), to_string(microsec));
std::stringstream ss;
ss << std::put_time(ptm, fmtms.c_str());
string ret = ss.str();
return ret.substr(0,ret.size()-cutBack);
}
std::cout << CommonUtils::format(CommonUtils::getTs(), "%Y-%m-%dT%H:%M:%S.%ms%Z")<<endl;
gives me: 2020-01-24T11:55:14.375+07, cutBack parameter is optional, it specifies how many characters to remove from the output string. It is useful when timezone format like +0700 is to long, and you just need +07.
Basically, you should be able to take whatever it is that you have that writes the formatted time without milliseconds, and add the remainder of the division of the number of millisconds by 1000. This should work because leap time is always an integer number of seconds.
Assuming C++11, you can try this:
#include <chrono>
#include <iomanip>
using namespace std;
using namespace chrono;
long long int milliSecondsSinceEpoch = ... // this is your starting point
const auto durationSinceEpoch = std::chrono::milliseconds(milliSecondsSinceEpoch);
const time_point<system_clock> tp_after_duration(durationSinceEpoch);
time_t time_after_duration = system_clock::to_time_t(tp_after_duration);
std::tm* formattedTime = std::localtime(&time_after_duration);
long long int milliseconds_remainder = milliSecondsSinceEpoch % 1000;
cout <<put_time(std::localtime(&time_after_duration), "%y-%m-%d-%H-%M-%S-") << milliseconds_remainder << endl;

Convert string datetime in C++

I have a date represented as string in the format "2012-10-28" and I want to convert it in the string format of "28/10/2012". Is this possible in C++ MS Visual Studio using a predefined function ?
This will do it:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
string format_date(string s)
{
char buf[11];
int a, b, c;
sscanf(s.c_str(), "%d-%d-%d", &a, &b, &c);
sprintf(buf, "%02d/%02d/%d", c, b, a);
return buf;
}
int main()
{
cout << format_date("2012-09-28") << endl;
}
I worked it out that way:
Use sscan_f to break date into year, month and day.
Create struct tm with the data above.
Use strftime to convert from tm to string with the desired format.
Please look at COleDateTime::ParseDateTime.
If do not want to use COleDateTime the implementation of the ParseDateTime is just a thin wrapper around VarDateFromStr.
strptime unfortunately does not exist in windows. Seek help here: strptime() equivalent on Windows?
You can then write the date using strftime.
in Qt (some embedded system does not support new timer class yet, so here)
I here just give the idea how to convert a string without much mumbo jumbo.
the timer class has the epoch function anyway.
QString fromSecsSinceEpoch(qint64 epoch)
{
QTextStream ts;
time_t result = epoch;//std::time(NULL);
//std::cout << std::asctime(std::localtime(&result))
// << result << " seconds since the Epoch\n";
ts << asctime(gmtime(&result));
return ts.readAll();
}
qint64 toSecsSinceEpoch(QString sDate)//Mon Nov 25 00:45:23 2013
{
QHash <QString,int> monthNames;
monthNames.insert("Jan",0);
monthNames.insert("Feb",1);
monthNames.insert("Mar",2);
monthNames.insert("Apr",3);
monthNames.insert("May",4);
monthNames.insert("Jun",5);
monthNames.insert("Jul",6);
monthNames.insert("Aug",7);
monthNames.insert("Sep",8);
monthNames.insert("Oct",9);
monthNames.insert("Nov",10);
monthNames.insert("Dec",11);
QStringList l_date = sDate.split(" ");
if (l_date.count() != 5)
{
return 0;//has to be 5 cuz Mon Nov 25 00:45:23 2013
}
QStringList l_time = l_date[3].split(":");
if (l_time.count() != 3)
{
return 0;//has to be 3 cuz 00:45:23
}
struct tm result;
result.tm_mday=l_date[2].toInt();
result.tm_mon=monthNames[l_date[1]];
result.tm_year=l_date[4].toInt()-1900;;
result.tm_hour=l_time[0].toInt();
result.tm_min=l_time[1].toInt();
result.tm_sec=l_time[2].toInt();
time_t timeEpoch=mktime(&result);
qDebug()<<"epochhhh :"<<timeEpoch;
return timeEpoch;
}