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

Go to the source code of this file.

Functions

time_point remwharead::string_to_timepoint (const string &strtime, bool sqlite=false)
 Convert ISO 8601 or SQLite time-string to time_point. More...
 
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()

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?
29 {
30  std::stringstream sstime(strtime);
31  struct std::tm tm = {};
32  tm.tm_isdst = -1; // Detect daylight saving time.
33  if (sqlite)
34  {
35  sstime >> std::get_time(&tm, "%Y-%m-%d %T");
36  }
37  else
38  {
39  sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
40  }
41  std::time_t time = timelocal(&tm); // Assume time is local.
42  return system_clock::from_time_t(time);
43 }

◆ timepoint_to_string()

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?
46 {
47  constexpr std::uint16_t bufsize = 32;
48  std::time_t time = system_clock::to_time_t(tp);
49  std::tm *tm;
50  tm = std::localtime(&time);
51 
52  array<char, bufsize> buffer = {};
53 
54  if (sqlite)
55  {
56  std::strftime(buffer.begin(), bufsize, "%F %T", tm);
57  }
58  else
59  {
60  std::strftime(buffer.begin(), bufsize, "%FT%T", tm);
61  }
62 
63  return buffer.begin();
64 }