remwharead  0.9.1
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?
30 {
31  std::stringstream sstime(strtime);
32  struct std::tm tm = {};
33  tm.tm_isdst = -1; // Don't convert to/from daylight saving time.
34  if (sqlite)
35  {
36  sstime >> std::get_time(&tm, "%Y-%m-%d %T");
37  }
38  else
39  {
40  sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
41  }
42  std::time_t time = mktime(&tm);
43  return system_clock::from_time_t(time);
44 }

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