99#include < pqxx/pqxx>
1010#include < cstdio>
1111#include < charconv>
12+ #include < format>
1213#include < stdexcept>
1314#include < boost/decimal.hpp>
1415
16+ class InvalidTimestampFormatError : public std ::runtime_error {
17+ public:
18+ using std::runtime_error::runtime_error;
19+ };
20+
1521static std::chrono::system_clock::time_point fastParseTimestamp (const char * ts) {
1622 int year = 0 ;
1723 int month = 0 ;
@@ -23,14 +29,15 @@ static std::chrono::system_clock::time_point fastParseTimestamp(const char* ts)
2329 const int parsedFields =
2430 std::sscanf (ts, " %4d-%2d-%2d %2d:%2d:%2d.%d" , &year, &month, &day, &hour, &min, &sec, &usec);
2531 if (parsedFields != 6 && parsedFields != 7 ) {
26- throw std::runtime_error (" Invalid timestamp format" );
32+ throw InvalidTimestampFormatError (" Invalid timestamp format: " + std::string (ts) );
2733 }
2834
2935 // Cache timegm per date — tick data is time-ordered so date changes rarely
30- static char cachedDate[ 11 ] = {} ;
36+ static std::string cachedDate;
3137 static time_t cachedEpoch = 0 ;
32- if (std::memcmp (ts, cachedDate, 10 ) != 0 ) {
33- std::memcpy (cachedDate, ts, 10 );
38+ const std::string_view date (ts, 10 );
39+ if (cachedDate != date) {
40+ cachedDate.assign (date);
3441 std::tm tm = {};
3542 tm.tm_year = year - 1900 ;
3643 tm.tm_mon = month - 1 ;
@@ -46,13 +53,9 @@ static std::chrono::system_clock::time_point fastParseTimestamp(const char* ts)
4653DatabaseConnection::DatabaseConnection (const std::string& endpoint, int port,
4754 const std::string& dbname, const std::string& user,
4855 const std::string& password) {
49- connection_string =
50- " host=" + endpoint + " "
51- " port=" + std::to_string (port) + " "
52- " dbname=" + dbname + " "
53- " user=" + user + " "
54- " password=" + password + " "
55- " connect_timeout=3" ;
56+ connection_string = std::format (
57+ " host={} port={} dbname={} user={} password={} connect_timeout=3" ,
58+ endpoint, port, dbname, user, password);
5659
5760}
5861
0 commit comments