remwharead  0.8.3
Functions
time.hpp File Reference
#include <string>
#include <chrono>

Go to the source code of this file.

Functions

const time_point remwharead::string_to_timepoint (const string &strtime, bool sqlite=false)
 Convert ISO 8601 or SQLite time-string to time_point. More...
 
const string remwharead::timepoint_to_string (const time_point &tp, bool sqlite=false)
 Convert time_point to ISO 8601 or SQLite time-string. More...
 

Function Documentation

◆ string_to_timepoint()

const time_point remwharead::string_to_timepoint ( const string &  strtime,
bool  sqlite = false 
)

Convert ISO 8601 or SQLite time-string to time_point.

The SQLite format is YY-MM-DD hh:mm:ss instead of YY-MM-DDThh:mm:ss.

Parameters
strtimeTime string in ISO 8601 or SQLite format.
sqliteIs the string in SQLite format?
26  {
27  std::stringstream sstime(strtime);
28  struct std::tm tm = {};
29  tm.tm_isdst = -1; // Detect daylight saving time.
30  if (sqlite)
31  {
32  sstime >> std::get_time(&tm, "%Y-%m-%d %T");
33  }
34  else
35  {
36  sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
37  }
38  std::time_t time = timelocal(&tm); // Assume time is local.
39  return system_clock::from_time_t(time);
40  }

◆ timepoint_to_string()

const string remwharead::timepoint_to_string ( const time_point &  tp,
bool  sqlite = false 
)

Convert time_point to ISO 8601 or SQLite time-string.

The SQLite format is YY-MM-DD hh:mm:ss instead of YY-MM-DDThh:mm:ss.

Parameters
time_pointThe std::chrono::system_clock::time_point.
sqliteIs the string in SQLite format?
43  {
44  constexpr std::uint16_t bufsize = 32;
45  std::time_t time = system_clock::to_time_t(tp);
46  std::tm *tm;
47  tm = std::localtime(&time);
48 
49  char buffer[bufsize];
50  if (sqlite)
51  {
52  std::strftime(buffer, bufsize, "%F %T", tm);
53  }
54  else
55  {
56  std::strftime(buffer, bufsize, "%FT%T", tm);
57  }
58 
59  return static_cast<const string>(buffer);
60  }