185 lines
6.3 KiB
C++
185 lines
6.3 KiB
C++
#include "engine/format.hpp"
|
|||
|
|
|
||
|
|
#include <cmath>
|
||
|
|
#include <cstdint>
|
||
|
|
#include <cstdio>
|
||
|
|
|
||
|
|
namespace htab {
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
// Howard Hinnant civil-from-days / days-from-civil (epoch 1970-01-01)
|
||
|
|
constexpr int64_t kEpochOffset = 25569; // days between 1899-12-30 and 1970-01-01
|
||
|
|
|
||
|
|
int64_t days_from_civil(int64_t y, unsigned m, unsigned d) {
|
||
|
|
y -= m <= 2;
|
||
|
|
const int64_t era = (y >= 0 ? y : y - 399) / 400;
|
||
|
|
const unsigned yoe = static_cast<unsigned>(y - era * 400);
|
||
|
|
const unsigned doy = (153 * (m + (m > 2 ? static_cast<unsigned>(-3) : 9)) + 2) / 5 + d - 1;
|
||
|
|
const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
|
||
|
|
return era * 146097 + static_cast<int64_t>(doe) - 719468;
|
||
|
|
}
|
||
|
|
|
||
|
|
void civil_from_days(int64_t z, int& y_out, int& m_out, int& d_out) {
|
||
|
|
z += 719468;
|
||
|
|
const int64_t era = (z >= 0 ? z : z - 146096) / 146097;
|
||
|
|
const unsigned doe = static_cast<unsigned>(z - era * 146097);
|
||
|
|
const unsigned yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
|
||
|
|
const int64_t y = static_cast<int64_t>(yoe) + era * 400;
|
||
|
|
const unsigned doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||
|
|
const unsigned mp = (5 * doy + 2) / 153;
|
||
|
|
const unsigned d = doy - (153 * mp + 2) / 5 + 1;
|
||
|
|
const unsigned m = mp + (mp < 10 ? 3 : static_cast<unsigned>(-9));
|
||
|
|
y_out = static_cast<int>(y + (m <= 2));
|
||
|
|
m_out = static_cast<int>(m);
|
||
|
|
d_out = static_cast<int>(d);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string insert_thousands_commas(const std::string& int_digits) {
|
||
|
|
if (int_digits.size() <= 3) return int_digits;
|
||
|
|
std::string out;
|
||
|
|
out.reserve(int_digits.size() + int_digits.size() / 3);
|
||
|
|
size_t rem = int_digits.size() % 3;
|
||
|
|
size_t idx = 0;
|
||
|
|
if (rem > 0) {
|
||
|
|
out.append(int_digits, 0, rem);
|
||
|
|
idx = rem;
|
||
|
|
}
|
||
|
|
while (idx < int_digits.size()) {
|
||
|
|
if (!out.empty()) out.push_back(',');
|
||
|
|
out.append(int_digits, idx, 3);
|
||
|
|
idx += 3;
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string format_fixed_num(double v, int decimals, bool thousands) {
|
||
|
|
if (!std::isfinite(v)) return "#NUM!";
|
||
|
|
bool neg = v < 0.0;
|
||
|
|
double av = std::fabs(v);
|
||
|
|
char buf[128];
|
||
|
|
std::snprintf(buf, sizeof(buf), "%.*f", decimals, av);
|
||
|
|
std::string s(buf);
|
||
|
|
// Prevent "-0.00" when rounded value is zero.
|
||
|
|
bool all_zero = true;
|
||
|
|
for (char ch : s) {
|
||
|
|
if (ch >= '1' && ch <= '9') {
|
||
|
|
all_zero = false;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (all_zero) neg = false;
|
||
|
|
|
||
|
|
if (thousands) {
|
||
|
|
size_t dot = s.find('.');
|
||
|
|
std::string int_part = (dot == std::string::npos) ? s : s.substr(0, dot);
|
||
|
|
std::string frac_part = (dot == std::string::npos) ? "" : s.substr(dot);
|
||
|
|
s = insert_thousands_commas(int_part) + frac_part;
|
||
|
|
}
|
||
|
|
if (neg) s.insert(s.begin(), '-');
|
||
|
|
return s;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
int ymd_to_serial(int year, int month, int day) {
|
||
|
|
int64_t total_months = static_cast<int64_t>(year) * 12 + static_cast<int64_t>(month - 1);
|
||
|
|
int64_t norm_y = total_months >= 0 ? (total_months / 12) : ((total_months - 11) / 12);
|
||
|
|
int64_t norm_m = total_months - norm_y * 12 + 1;
|
||
|
|
int64_t days = days_from_civil(norm_y, static_cast<unsigned>(norm_m), 1) +
|
||
|
|
static_cast<int64_t>(day - 1) + kEpochOffset;
|
||
|
|
return static_cast<int>(days);
|
||
|
|
}
|
||
|
|
|
||
|
|
void serial_to_ymd(int serial, int& year, int& month, int& day) {
|
||
|
|
civil_from_days(static_cast<int64_t>(serial) - kEpochOffset, year, month, day);
|
||
|
|
}
|
||
|
|
|
||
|
|
void serial_to_hms(double serial, int& hour, int& minute, int& second) {
|
||
|
|
double frac = serial - std::floor(serial);
|
||
|
|
long long total = std::llround(frac * 86400.0);
|
||
|
|
total = ((total % 86400) + 86400) % 86400;
|
||
|
|
hour = static_cast<int>(total / 3600);
|
||
|
|
minute = static_cast<int>((total % 3600) / 60);
|
||
|
|
second = static_cast<int>(total % 60);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string format_value(const Value& val, const std::string& fmt) {
|
||
|
|
if (val.is_empty()) return "";
|
||
|
|
if (val.is_error()) return std::string(error_to_string(val.as_error()));
|
||
|
|
if (val.is_bool()) return val.as_bool() ? "TRUE" : "FALSE";
|
||
|
|
if (val.is_string()) return val.as_string();
|
||
|
|
if (val.is_array()) {
|
||
|
|
const auto& arr = val.as_array();
|
||
|
|
if (arr && !arr->data.empty()) {
|
||
|
|
return format_value(arr->at(0, 0), fmt);
|
||
|
|
}
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
|
||
|
|
double d = val.as_number();
|
||
|
|
if (fmt.empty() || iequals_ascii(fmt, "General")) {
|
||
|
|
auto s = to_string(val);
|
||
|
|
return std::holds_alternative<std::string>(s) ? std::get<std::string>(s) : "#VALUE!";
|
||
|
|
}
|
||
|
|
|
||
|
|
if (iequals_ascii(fmt, "Fixed") || fmt == "0.00") {
|
||
|
|
return format_fixed_num(d, 2, false);
|
||
|
|
}
|
||
|
|
if (iequals_ascii(fmt, "Integer") || fmt == "#,##0" || fmt == "0") {
|
||
|
|
return format_fixed_num(d, 0, fmt != "0");
|
||
|
|
}
|
||
|
|
if (iequals_ascii(fmt, "Currency") || fmt == "$#,##0.00") {
|
||
|
|
bool neg = d < 0.0;
|
||
|
|
std::string body = format_fixed_num(std::fabs(d), 2, true);
|
||
|
|
bool all_zero = (body == "0.00");
|
||
|
|
if (neg && !all_zero) return "-$" + body;
|
||
|
|
return "$" + body;
|
||
|
|
}
|
||
|
|
if (iequals_ascii(fmt, "Percentage") || fmt == "0.00%" || fmt == "0%") {
|
||
|
|
int dec = (fmt == "0%") ? 0 : 2;
|
||
|
|
return format_fixed_num(d * 100.0, dec, false) + "%";
|
||
|
|
}
|
||
|
|
if (iequals_ascii(fmt, "Date") || iequals_ascii(fmt, "yyyy-mm-dd")) {
|
||
|
|
int y = 0, m = 0, day = 0;
|
||
|
|
serial_to_ymd(static_cast<int>(std::floor(d)), y, m, day);
|
||
|
|
char buf[32];
|
||
|
|
std::snprintf(buf, sizeof(buf), "%04d-%02d-%02d", y, m, day);
|
||
|
|
return std::string(buf);
|
||
|
|
}
|
||
|
|
if (iequals_ascii(fmt, "Time") || iequals_ascii(fmt, "hh:mm:ss")) {
|
||
|
|
int hh = 0, mm = 0, ss = 0;
|
||
|
|
serial_to_hms(d, hh, mm, ss);
|
||
|
|
char buf[32];
|
||
|
|
std::snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hh, mm, ss);
|
||
|
|
return std::string(buf);
|
||
|
|
}
|
||
|
|
if (iequals_ascii(fmt, "DateTime") || iequals_ascii(fmt, "yyyy-mm-dd hh:mm")) {
|
||
|
|
int y = 0, m = 0, day = 0, hh = 0, mm = 0, ss = 0;
|
||
|
|
long long rounded_sec = std::llround((d - std::floor(d)) * 86400.0);
|
||
|
|
int day_serial = static_cast<int>(std::floor(d));
|
||
|
|
if (rounded_sec >= 86400) {
|
||
|
|
day_serial += 1;
|
||
|
|
}
|
||
|
|
serial_to_ymd(day_serial, y, m, day);
|
||
|
|
serial_to_hms(d, hh, mm, ss);
|
||
|
|
char buf[48];
|
||
|
|
std::snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d", y, m, day, hh, mm);
|
||
|
|
return std::string(buf);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Fallback for custom decimal formats like "0.0" or "0.000%".
|
||
|
|
if (fmt.rfind("0.", 0) == 0) {
|
||
|
|
bool pct = fmt.back() == '%';
|
||
|
|
size_t dec = fmt.size() - 2 - (pct ? 1 : 0);
|
||
|
|
if (dec <= 8) {
|
||
|
|
return format_fixed_num(pct ? d * 100.0 : d, static_cast<int>(dec), false) +
|
||
|
|
(pct ? "%" : "");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
auto s = to_string(val);
|
||
|
|
return std::holds_alternative<std::string>(s) ? std::get<std::string>(s) : "#VALUE!";
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace htab
|