317 lines
11 KiB
C++
317 lines
11 KiB
C++
#include "engine/format.hpp"
|
|
#include "engine/functions/helpers.hpp"
|
|
#include "engine/functions/registry.hpp"
|
|
#include "engine/workbook.hpp"
|
|
|
|
#include <cctype>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
|
|
namespace htab {
|
|
|
|
using namespace htab::fn;
|
|
|
|
namespace {
|
|
|
|
int days_in_month(int y, int m) {
|
|
static constexpr int kDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
if (m < 1 || m > 12) return 0;
|
|
if (m == 2) {
|
|
bool leap = (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
|
|
return leap ? 29 : 28;
|
|
}
|
|
return kDays[m - 1];
|
|
}
|
|
|
|
int parse_month_name(std::string_view s) {
|
|
static constexpr const char* kShort[] = {
|
|
"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
|
|
static constexpr const char* kFull[] = {
|
|
"january", "february", "march", "april", "may", "june",
|
|
"july", "august", "september", "october", "november", "december"};
|
|
for (int i = 0; i < 12; ++i) {
|
|
if (iequals_ascii(s, kShort[i]) || iequals_ascii(s, kFull[i])) return i + 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
bool parse_uint(std::string_view s, int& out) {
|
|
if (s.empty()) return false;
|
|
int64_t val = 0;
|
|
for (char ch : s) {
|
|
if (!std::isdigit(static_cast<unsigned char>(ch))) return false;
|
|
val = val * 10 + (ch - '0');
|
|
if (val > 1000000) return false;
|
|
}
|
|
out = static_cast<int>(val);
|
|
return true;
|
|
}
|
|
|
|
std::optional<int> parse_date_string(std::string_view raw) {
|
|
raw = trim_ascii(raw);
|
|
if (raw.empty()) return std::nullopt;
|
|
|
|
// Strip optional trailing time part if separated by space
|
|
// Wait: "Jan 5, 2024" has spaces, so only strip if contains ':'
|
|
size_t colon = raw.find(':');
|
|
if (colon != std::string_view::npos) {
|
|
size_t sp = raw.rfind(' ', colon);
|
|
if (sp != std::string_view::npos) {
|
|
raw = trim_ascii(raw.substr(0, sp));
|
|
}
|
|
}
|
|
|
|
// Try YYYY-MM-DD or YYYY/MM/DD or MM/DD/YYYY or DD-MM-YYYY
|
|
auto split_sep = [&](char sep) -> std::vector<std::string_view> {
|
|
std::vector<std::string_view> parts;
|
|
size_t start = 0;
|
|
while (true) {
|
|
size_t p = raw.find(sep, start);
|
|
if (p == std::string_view::npos) {
|
|
parts.push_back(trim_ascii(raw.substr(start)));
|
|
break;
|
|
}
|
|
parts.push_back(trim_ascii(raw.substr(start, p - start)));
|
|
start = p + 1;
|
|
}
|
|
return parts;
|
|
};
|
|
|
|
for (char sep : {'-', '/'}) {
|
|
if (raw.find(sep) != std::string_view::npos) {
|
|
auto p = split_sep(sep);
|
|
if (p.size() == 3) {
|
|
int a = 0, b = 0, c = 0;
|
|
if (parse_uint(p[0], a) && parse_uint(p[1], b) && parse_uint(p[2], c)) {
|
|
int y = 0, m = 0, d = 0;
|
|
if (p[0].size() == 4) {
|
|
y = a; m = b; d = c;
|
|
} else {
|
|
m = a; d = b; y = c;
|
|
if (y < 100) y += (y >= 30 ? 1900 : 2000);
|
|
}
|
|
if (m >= 1 && m <= 12 && d >= 1 && d <= days_in_month(y, m)) {
|
|
return ymd_to_serial(y, m, d);
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
// Try DD-Mon-YYYY
|
|
int mon = parse_month_name(p[1]);
|
|
if (mon > 0 && parse_uint(p[0], a) && parse_uint(p[2], c)) {
|
|
int y = c;
|
|
if (y < 100) y += (y >= 30 ? 1900 : 2000);
|
|
if (a >= 1 && a <= days_in_month(y, mon)) {
|
|
return ymd_to_serial(y, mon, a);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Try "Mon DD, YYYY" or "Mon DD YYYY"
|
|
std::vector<std::string_view> words;
|
|
size_t i = 0;
|
|
while (i < raw.size()) {
|
|
while (i < raw.size() && (raw[i] == ' ' || raw[i] == ',')) ++i;
|
|
size_t st = i;
|
|
while (i < raw.size() && raw[i] != ' ' && raw[i] != ',') ++i;
|
|
if (i > st) words.push_back(raw.substr(st, i - st));
|
|
}
|
|
if (words.size() == 3) {
|
|
int mon = parse_month_name(words[0]);
|
|
int d = 0, y = 0;
|
|
if (mon > 0 && parse_uint(words[1], d) && parse_uint(words[2], y)) {
|
|
if (d >= 1 && d <= days_in_month(y, mon)) {
|
|
return ymd_to_serial(y, mon, d);
|
|
}
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<double> parse_time_string(std::string_view raw) {
|
|
raw = trim_ascii(raw);
|
|
if (raw.empty()) return std::nullopt;
|
|
|
|
bool has_ampm = false;
|
|
bool is_pm = false;
|
|
if (raw.size() >= 2) {
|
|
std::string_view tail = raw.substr(raw.size() - 2);
|
|
if (iequals_ascii(tail, "AM") || iequals_ascii(tail, "PM")) {
|
|
has_ampm = true;
|
|
is_pm = iequals_ascii(tail, "PM");
|
|
raw = trim_ascii(raw.substr(0, raw.size() - 2));
|
|
}
|
|
}
|
|
|
|
// If date precedes space before time, take last space-separated token
|
|
size_t sp = raw.rfind(' ');
|
|
if (sp != std::string_view::npos) {
|
|
raw = trim_ascii(raw.substr(sp + 1));
|
|
}
|
|
|
|
std::vector<std::string_view> parts;
|
|
size_t start = 0;
|
|
while (true) {
|
|
size_t p = raw.find(':', start);
|
|
if (p == std::string_view::npos) {
|
|
parts.push_back(raw.substr(start));
|
|
break;
|
|
}
|
|
parts.push_back(raw.substr(start, p - start));
|
|
start = p + 1;
|
|
}
|
|
if (parts.size() < 2 || parts.size() > 3) return std::nullopt;
|
|
|
|
int h = 0, m = 0, s = 0;
|
|
if (!parse_uint(parts[0], h) || !parse_uint(parts[1], m)) return std::nullopt;
|
|
if (parts.size() == 3 && !parse_uint(parts[2], s)) return std::nullopt;
|
|
|
|
if (has_ampm) {
|
|
if (h < 1 || h > 12) return std::nullopt;
|
|
if (h == 12) h = 0;
|
|
if (is_pm) h += 12;
|
|
} else {
|
|
if (h > 23) return std::nullopt;
|
|
}
|
|
if (m > 59 || s > 59) return std::nullopt;
|
|
return static_cast<double>(h * 3600 + m * 60 + s) / 86400.0;
|
|
}
|
|
|
|
EvalResult fn_date(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 3) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto yd = eval_number_arg(args[0], ctx, err);
|
|
if (!yd) return err_res(err);
|
|
auto md = eval_number_arg(args[1], ctx, err);
|
|
if (!md) return err_res(err);
|
|
auto dd = eval_number_arg(args[2], ctx, err);
|
|
if (!dd) return err_res(err);
|
|
|
|
int y = static_cast<int>(std::trunc(*yd));
|
|
int m = static_cast<int>(std::trunc(*md));
|
|
int d = static_cast<int>(std::trunc(*dd));
|
|
if (y < 0 || y >= 10000) return err_res(ErrorCode::Num);
|
|
if (y < 1900) y += 1900;
|
|
int serial = ymd_to_serial(y, m, d);
|
|
if (serial < 0) return err_res(ErrorCode::Num);
|
|
return EvalResult::from_value(Value::make_number(static_cast<double>(serial)));
|
|
}
|
|
|
|
EvalResult fn_datevalue(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto s = eval_string_arg(args[0], ctx, err);
|
|
if (!s) return err_res(err);
|
|
auto serial = parse_date_string(*s);
|
|
if (!serial) return err_res(ErrorCode::Value);
|
|
return EvalResult::from_value(Value::make_number(static_cast<double>(*serial)));
|
|
}
|
|
|
|
EvalResult fn_time(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 3) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto hd = eval_number_arg(args[0], ctx, err);
|
|
if (!hd) return err_res(err);
|
|
auto md = eval_number_arg(args[1], ctx, err);
|
|
if (!md) return err_res(err);
|
|
auto sd = eval_number_arg(args[2], ctx, err);
|
|
if (!sd) return err_res(err);
|
|
|
|
long long total = static_cast<long long>(std::trunc(*hd)) * 3600LL +
|
|
static_cast<long long>(std::trunc(*md)) * 60LL +
|
|
static_cast<long long>(std::trunc(*sd));
|
|
if (total < 0) return err_res(ErrorCode::Num);
|
|
double frac = static_cast<double>(total % 86400LL) / 86400.0;
|
|
return EvalResult::from_value(Value::make_number(frac));
|
|
}
|
|
|
|
EvalResult fn_timevalue(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto s = eval_string_arg(args[0], ctx, err);
|
|
if (!s) return err_res(err);
|
|
auto t = parse_time_string(*s);
|
|
if (!t) return err_res(ErrorCode::Value);
|
|
return EvalResult::from_value(Value::make_number(*t));
|
|
}
|
|
|
|
EvalResult fn_now(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext&) {
|
|
if (!args.empty()) return err_res(ErrorCode::Value);
|
|
std::time_t now_t = std::time(nullptr);
|
|
std::tm local_tm{};
|
|
localtime_r(&now_t, &local_tm);
|
|
int serial = ymd_to_serial(local_tm.tm_year + 1900, local_tm.tm_mon + 1, local_tm.tm_mday);
|
|
double frac = static_cast<double>(local_tm.tm_hour * 3600 + local_tm.tm_min * 60 +
|
|
local_tm.tm_sec) /
|
|
86400.0;
|
|
return EvalResult::from_value(Value::make_number(static_cast<double>(serial) + frac));
|
|
}
|
|
|
|
EvalResult extract_ymd_part(const std::vector<std::shared_ptr<Expr>>& args,
|
|
const EvalContext& ctx,
|
|
int which) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto n = eval_number_arg(args[0], ctx, err);
|
|
if (!n) return err_res(err);
|
|
if (*n < 0.0) return err_res(ErrorCode::Num);
|
|
int y = 0, m = 0, d = 0;
|
|
serial_to_ymd(static_cast<int>(std::floor(*n)), y, m, d);
|
|
int val = (which == 0) ? y : (which == 1 ? m : d);
|
|
return EvalResult::from_value(Value::make_number(static_cast<double>(val)));
|
|
}
|
|
|
|
EvalResult extract_hms_part(const std::vector<std::shared_ptr<Expr>>& args,
|
|
const EvalContext& ctx,
|
|
int which) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto n = eval_number_arg(args[0], ctx, err);
|
|
if (!n) return err_res(err);
|
|
if (*n < 0.0) return err_res(ErrorCode::Num);
|
|
int h = 0, m = 0, s = 0;
|
|
serial_to_hms(*n, h, m, s);
|
|
int val = (which == 0) ? h : (which == 1 ? m : s);
|
|
return EvalResult::from_value(Value::make_number(static_cast<double>(val)));
|
|
}
|
|
|
|
EvalResult fn_year(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
|
return extract_ymd_part(a, c, 0);
|
|
}
|
|
EvalResult fn_month(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
|
return extract_ymd_part(a, c, 1);
|
|
}
|
|
EvalResult fn_day(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
|
return extract_ymd_part(a, c, 2);
|
|
}
|
|
EvalResult fn_hour(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
|
return extract_hms_part(a, c, 0);
|
|
}
|
|
EvalResult fn_minute(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
|
return extract_hms_part(a, c, 1);
|
|
}
|
|
EvalResult fn_second(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
|
return extract_hms_part(a, c, 2);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void register_datetime_functions(FunctionRegistry& reg) {
|
|
reg.register_fn("DATE", fn_date);
|
|
reg.register_fn("DATEVALUE", fn_datevalue);
|
|
reg.register_fn("DAY", fn_day);
|
|
reg.register_fn("HOUR", fn_hour);
|
|
reg.register_fn("MINUTE", fn_minute);
|
|
reg.register_fn("MONTH", fn_month);
|
|
reg.register_fn("NOW", fn_now);
|
|
reg.register_fn("SECOND", fn_second);
|
|
reg.register_fn("TIME", fn_time);
|
|
reg.register_fn("TIMEVALUE", fn_timevalue);
|
|
reg.register_fn("YEAR", fn_year);
|
|
}
|
|
|
|
} // namespace htab
|