Upgrade ccapi to v6.12.0

This commit is contained in:
John Colvin committed 2025-02-23 15:33:55 +00:00
1 parent 8069cc893f
commit 90492733fd
109 files changed
+4172 -8930

No files matched your search

+105 -25
View File
@@ -1,5 +1,8 @@
#ifndef INCLUDE_CCAPI_CPP_CCAPI_UTIL_PRIVATE_H_
#define INCLUDE_CCAPI_CPP_CCAPI_UTIL_PRIVATE_H_
#ifdef _WIN32
#define timegm _mkgmtime
#endif
#include <unistd.h>
#include <algorithm>
@@ -9,6 +12,7 @@
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <random>
@@ -21,8 +25,7 @@
#include <unordered_set>
#include <vector>
#include "ccapi_cpp/ccapi_date.h"
#include "ccapi_cpp/ccapi_logger.h"
#include "ccapi_cpp/ccapi_macro.h"
#include "ccapi_cpp/ccapi_util.h"
#include "openssl/evp.h"
namespace ccapi {
@@ -52,9 +55,12 @@ class UtilString CCAPI_FINAL {
output += ".";
output += c.substr(c.size() + exponent);
} else {
output = std::string(-exponent - c.size() + 1, '0');
output += ".";
output += c;
// output = std::string(-exponent - c.size() + 1, '0');
// output += ".";
// output += c; // use these three code, roundInputBySignificantFigure(0.00123456, 3, 1), output is "000.124"
output = "0.";
output += std::string(-exponent - c.size(), '0');
output += c; // use these three code, roundInputBySignificantFigure(0.00123456, 3, 1), output is "0.00124"
}
return output;
}
@@ -302,7 +308,8 @@ class UtilTime CCAPI_FINAL {
output += millisecondStr;
return output;
}
static void timePointToParts(TimePoint tp, int& year, int& month, int& day, int& hour, int& minute, int& second, int& millisecond) {
template <typename T = std::chrono::milliseconds>
static void timePointToParts(TimePoint tp, int& year, int& month, int& day, int& hour, int& minute, int& second, int& fractionalSecond) {
auto epoch_sec = std::chrono::time_point_cast<std::chrono::seconds>(tp).time_since_epoch().count();
auto day_sec = epoch_sec - (epoch_sec % 86400);
auto days_since_epoch = day_sec / 86400;
@@ -317,28 +324,46 @@ class UtilTime CCAPI_FINAL {
day = doy - (153 * mp + 2) / 5 + 1;
month = mp + (mp < 10 ? 3 : -9);
year += month <= 2;
auto in_day = tp - std::chrono::seconds(day_sec);
millisecond = std::chrono::time_point_cast<std::chrono::milliseconds>(in_day).time_since_epoch().count();
hour = millisecond / (60 * 60 * 1000);
millisecond -= hour * 60 * 60 * 1000;
minute = millisecond / (60 * 1000);
millisecond -= minute * 60 * 1000;
second = millisecond / 1000;
millisecond -= second * 1000;
auto in_day = tp - std::chrono::duration_cast<T>(std::chrono::seconds(day_sec));
auto in_day_sec_original = std::chrono::time_point_cast<std::chrono::seconds>(in_day).time_since_epoch().count();
auto in_day_sec = in_day_sec_original;
hour = in_day_sec / 3600;
in_day_sec -= hour * 3600;
minute = in_day_sec / 60;
second = in_day_sec - minute * 60;
auto in_day_fractional_second = in_day - std::chrono::duration_cast<T>(std::chrono::seconds(in_day_sec_original));
fractionalSecond = std::chrono::time_point_cast<T>(in_day_fractional_second).time_since_epoch().count();
}
static TimePoint now() {
auto now = std::chrono::system_clock::now();
return TimePoint(now);
}
static TimePoint parse(const std::string& s, const std::string& fmt = "%FT%TZ") {
TimePoint tp;
std::istringstream ss{s};
ss >> date::parse(fmt, tp);
if (ss.fail()) {
CCAPI_LOGGER_FATAL("unable to parse time string");
static TimePoint parse(const std::string& input) {
std::tm time{};
time.tm_year = std::strtol(&input[0], nullptr, 10) - 1900;
time.tm_mon = std::strtol(&input[5], nullptr, 10) - 1;
time.tm_mday = std::strtol(&input[8], nullptr, 10);
if (input.length() > 10) {
time.tm_hour = std::strtol(&input[11], nullptr, 10);
time.tm_min = std::strtol(&input[14], nullptr, 10);
time.tm_sec = std::strtol(&input[17], nullptr, 10);
}
return tp;
time.tm_isdst = 0;
long nanoseconds = 0;
if (input.length() > 20) {
std::string trail = input.substr(20);
if (trail.back() == 'Z') {
trail.pop_back();
}
if (!trail.empty()) {
if (trail.length() > 9) {
throw std::invalid_argument("input too long");
}
nanoseconds = std::stoll(UtilString::rightPadTo(trail, 9, '0'));
}
}
return TimePoint(std::chrono::system_clock::from_time_t(timegm(&time))) + std::chrono::nanoseconds(nanoseconds);
}
static TimePoint makeTimePoint(const std::pair<long long, long long>& timePair) {
auto tp = TimePoint(std::chrono::duration<int64_t>(timePair.first));
@@ -381,12 +406,65 @@ class UtilTime CCAPI_FINAL {
return std::make_pair(std::stoll(milliseconds), 0);
}
}
static std::string convertMillisecondsStrToSecondsStr(const std::string& milliseconds) {
std::string output;
if (milliseconds.length() >= 4) {
output = milliseconds;
output.insert(milliseconds.length() - 3, 1, '.');
} else {
output = "0.";
output += std::string(3 - milliseconds.length(), '0');
output += milliseconds;
output = UtilString::normalizeDecimalString(output);
}
return output;
}
static std::pair<long long, long long> divideNanoWhole(const std::string& nanoseconds) {
return std::make_pair(std::stoll(nanoseconds.substr(0, nanoseconds.length() - 9)), std::stoll(nanoseconds.substr(nanoseconds.length() - 9)));
}
template <typename T = std::chrono::nanoseconds>
static std::string getISOTimestamp(const TimePoint& tp, const std::string& fmt = "%FT%TZ") {
return date::format(fmt.c_str(), date::floor<T>(tp));
static std::string getISOTimestamp(const TimePoint& tp) {
int year, month, day, hour, minute, second, fractionalSecond;
timePointToParts<T>(tp, year, month, day, hour, minute, second, fractionalSecond);
std::string output;
output += std::to_string(year);
output += "-";
auto monthStr = std::to_string(month);
output += std::string(2 - monthStr.length(), '0');
output += monthStr;
output += "-";
auto dayStr = std::to_string(day);
output += std::string(2 - dayStr.length(), '0');
output += dayStr;
output += "T";
auto hourStr = std::to_string(hour);
output += std::string(2 - hourStr.length(), '0');
output += hourStr;
output += ":";
auto minuteStr = std::to_string(minute);
output += std::string(2 - minuteStr.length(), '0');
output += minuteStr;
output += ":";
auto secondStr = std::to_string(second);
output += std::string(2 - secondStr.length(), '0');
output += secondStr;
if (!std::is_same<T, std::chrono::seconds>::value) {
output += ".";
auto fractionalSecondStr = std::to_string(fractionalSecond);
int padToLength;
if (std::is_same<T, std::chrono::nanoseconds>::value) {
padToLength = 9;
} else if (std::is_same<T, std::chrono::microseconds>::value) {
padToLength = 6;
} else if (std::is_same<T, std::chrono::milliseconds>::value) {
padToLength = 3;
}
output += std::string(padToLength - fractionalSecondStr.length(), '0');
// UtilString::rtrimInPlace(fractionalSecondStr, '0');
output += fractionalSecondStr;
}
output += "Z";
return output;
}
static int getUnixTimestamp(const TimePoint& tp) {
auto then = tp.time_since_epoch();
@@ -415,7 +493,9 @@ class UtilAlgorithm CCAPI_FINAL {
EVP_DigestInit_ex(context, EVP_sha512(), NULL);
break;
default:
CCAPI_LOGGER_FATAL(CCAPI_UNSUPPORTED_VALUE);
// Release the context, adding an extra line here to avoid potential memory leaks that may occur in computeHash
EVP_MD_CTX_free(context);
throw std::invalid_argument("invalid shaVersion");
}
EVP_DigestUpdate(context, unhashed.c_str(), unhashed.length());
unsigned char hash[EVP_MAX_MD_SIZE];
@@ -657,7 +737,7 @@ int ceilSearch(const std::vector<T>& c, int low, int high, T x) {
if (x <= c[low]) {
return low;
}
for (i = low; i < high; i++) {
for (i = low; i < high - 1; i++) {
if (c[i] == x) {
return i;
}