208 lines
9 KiB
C++
208 lines
9 KiB
C++
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
module Nostro.App:Format_impl;
|
||
|
|
import std;
|
||
|
|
import Nostro;
|
||
|
|
import :Format;
|
||
|
|
|
||
|
|
using namespace Nostro;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
constexpr std::array<std::string_view, 12> months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
|
||
|
|
constexpr std::array<std::string_view, 7> weekdays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
|
||
|
|
|
||
|
|
// A moment as the user's clock and calendar show it.
|
||
|
|
std::tm Local(std::chrono::system_clock::time_point time) {
|
||
|
|
const std::time_t seconds = std::chrono::system_clock::to_time_t(time);
|
||
|
|
return *std::localtime(&seconds);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::int64_t DayOf(const std::tm& local) {
|
||
|
|
return static_cast<std::int64_t>(local.tm_year + 1900) * 1000 + local.tm_yday;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string_view Month(const std::tm& local) {
|
||
|
|
return months[static_cast<std::size_t>(local.tm_mon)];
|
||
|
|
}
|
||
|
|
|
||
|
|
// The bank's capitals and underscores as words: "PIN_TRIES_EXCEEDED" is "Pin tries exceeded", and "sandbox" is
|
||
|
|
// "Sandbox".
|
||
|
|
std::string Words(std::string_view shouted) {
|
||
|
|
std::string words;
|
||
|
|
for(const char c : shouted) {
|
||
|
|
if(c == '_') {
|
||
|
|
words += ' ';
|
||
|
|
} else if(c >= 'A' && c <= 'Z' && !words.empty()) {
|
||
|
|
words += static_cast<char>(c - 'A' + 'a');
|
||
|
|
} else if(c >= 'a' && c <= 'z' && words.empty()) {
|
||
|
|
words += static_cast<char>(c - 'a' + 'A');
|
||
|
|
} else {
|
||
|
|
words += c;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return words;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::GroupIban(std::string_view iban) {
|
||
|
|
const auto letter = [](char c) { return c >= 'A' && c <= 'Z'; };
|
||
|
|
const auto digit = [](char c) { return c >= '0' && c <= '9'; };
|
||
|
|
// A country, two check digits, and letters and digits after them: an e-mail address or a name is left alone.
|
||
|
|
if(iban.size() < 5 || !letter(iban[0]) || !letter(iban[1]) || !digit(iban[2]) || !digit(iban[3]) || !std::ranges::all_of(iban, [&letter, &digit](char c) { return letter(c) || digit(c); })) {
|
||
|
|
return std::string(iban);
|
||
|
|
}
|
||
|
|
std::string grouped;
|
||
|
|
for(std::size_t i = 0; i < iban.size(); i++) {
|
||
|
|
if(i > 0 && i % 4 == 0) {
|
||
|
|
grouped += ' ';
|
||
|
|
}
|
||
|
|
grouped += iban[i];
|
||
|
|
}
|
||
|
|
return grouped;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::AccountKey(const Account& account) {
|
||
|
|
return account.iban.empty() ? account.name : account.iban;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::PartyName(const BunqLabel& party) {
|
||
|
|
if(!party.name.empty()) {
|
||
|
|
return party.name;
|
||
|
|
}
|
||
|
|
return party.iban.empty() ? std::string("Unknown") : GroupIban(party.iban);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::PartyName(const BunqCounterparty& party) {
|
||
|
|
return party.name.empty() ? GroupIban(party.value) : party.name;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::Money(std::int64_t minor, Currency currency) {
|
||
|
|
std::string money = FormatMoney(minor, currency);
|
||
|
|
// A comma before every three digits of the whole part, from its end.
|
||
|
|
const std::size_t point = std::min(money.find('.'), money.size());
|
||
|
|
std::size_t first = point;
|
||
|
|
while(first > 0 && money[first - 1] >= '0' && money[first - 1] <= '9') {
|
||
|
|
first--;
|
||
|
|
}
|
||
|
|
for(std::size_t at = point; at > first + 3;) {
|
||
|
|
at -= 3;
|
||
|
|
money.insert(at, 1, ',');
|
||
|
|
}
|
||
|
|
return money;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::SignedMoney(std::int64_t minor, Currency currency) {
|
||
|
|
return minor > 0 ? std::format("+{}", Money(minor, currency)) : Money(minor, currency);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::int64_t App::LocalDay(std::chrono::system_clock::time_point time) {
|
||
|
|
return DayOf(Local(time));
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::DayLabel(std::chrono::system_clock::time_point time, std::chrono::system_clock::time_point now) {
|
||
|
|
const std::tm local = Local(time);
|
||
|
|
const std::tm today = Local(now);
|
||
|
|
if(DayOf(local) == DayOf(today)) {
|
||
|
|
return "Today";
|
||
|
|
}
|
||
|
|
// The day before by the calendar, from its noon: a day is 23 or 25 hours long where the clocks change.
|
||
|
|
std::tm before = today;
|
||
|
|
before.tm_mday -= 1;
|
||
|
|
before.tm_hour = 12;
|
||
|
|
before.tm_isdst = -1;
|
||
|
|
std::mktime(&before);
|
||
|
|
if(DayOf(local) == DayOf(before)) {
|
||
|
|
return "Yesterday";
|
||
|
|
}
|
||
|
|
const std::string day = std::format("{} {} {}", weekdays[static_cast<std::size_t>(local.tm_wday)], local.tm_mday, Month(local));
|
||
|
|
return local.tm_year == today.tm_year ? day : std::format("{} {}", day, local.tm_year + 1900);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::ShortDate(std::chrono::system_clock::time_point time, std::chrono::system_clock::time_point now) {
|
||
|
|
const std::tm local = Local(time);
|
||
|
|
const std::string day = std::format("{} {}", local.tm_mday, Month(local).substr(0, 3));
|
||
|
|
return local.tm_year == Local(now).tm_year ? day : std::format("{} {}", day, local.tm_year + 1900);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::ClockTime(std::chrono::system_clock::time_point time) {
|
||
|
|
const std::tm local = Local(time);
|
||
|
|
return std::format("{:02}:{:02}", local.tm_hour, local.tm_min);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::FullTime(std::chrono::system_clock::time_point time) {
|
||
|
|
const std::tm local = Local(time);
|
||
|
|
return std::format("{} {} {}, {:02}:{:02}", local.tm_mday, Month(local), local.tm_year + 1900, local.tm_hour, local.tm_min);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::DateInput(std::chrono::system_clock::time_point time) {
|
||
|
|
const std::tm local = Local(time);
|
||
|
|
return std::format("{:04}-{:02}-{:02}", local.tm_year + 1900, local.tm_mon + 1, local.tm_mday);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::KindLabel(std::string_view kind) {
|
||
|
|
// A type and a sub type, "MASTERCARD/PAYMENT", as the bank's connector stored them: the type says how the money
|
||
|
|
// moved. These are the kinds the app has words for; any other is said as the bank says it.
|
||
|
|
const std::string_view type = kind.substr(0, kind.find('/'));
|
||
|
|
static constexpr std::array<std::pair<std::string_view, std::string_view>, 6> known = {{
|
||
|
|
{"MASTERCARD", "Card payment"}, {"EBA_SDD", "Direct debit"}, {"EBA_SCT", "Bank transfer"},
|
||
|
|
{"IDEAL", "iDEAL"}, {"SOFORT", "Sofort"}, {"SWIFT", "International transfer"},
|
||
|
|
}};
|
||
|
|
const auto found = std::ranges::find(known, type, &std::pair<std::string_view, std::string_view>::first);
|
||
|
|
if(found != known.end()) {
|
||
|
|
return std::string(found->second);
|
||
|
|
}
|
||
|
|
// bunq's own type, for what stays inside it: a payment between two of its accounts, or of a request.
|
||
|
|
if(type == "BUNQ") {
|
||
|
|
return kind == "BUNQ/REQUEST" ? "Paid request" : "Payment within the bank";
|
||
|
|
}
|
||
|
|
return std::string(kind);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::StatusLabel(std::string_view status) {
|
||
|
|
return Words(status);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::CardProduct(std::string_view productType) {
|
||
|
|
return Words(productType);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::CardExpiry(std::string_view expiry) {
|
||
|
|
// The bank's day, YYYY-MM-DD: a card's face has the month and the last two digits of the year.
|
||
|
|
static constexpr std::array<std::size_t, 8> digits = {0, 1, 2, 3, 5, 6, 8, 9};
|
||
|
|
const bool day = expiry.size() == 10 && expiry[4] == '-' && expiry[7] == '-' && std::ranges::all_of(digits, [expiry](std::size_t at) { return expiry[at] >= '0' && expiry[at] <= '9'; });
|
||
|
|
return day ? std::format("{}/{}", expiry.substr(5, 2), expiry.substr(2, 2)) : std::string(expiry);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::ScheduleLabel(const BunqSchedule& schedule, std::chrono::system_clock::time_point now) {
|
||
|
|
// The day, and the time on it when that is not its midnight.
|
||
|
|
const auto moment = [now](std::chrono::system_clock::time_point time) {
|
||
|
|
const std::tm local = Local(time);
|
||
|
|
return local.tm_hour == 0 && local.tm_min == 0 ? ShortDate(time, now) : std::format("{}, {}", ShortDate(time, now), ClockTime(time));
|
||
|
|
};
|
||
|
|
if(schedule.unit == "ONCE") {
|
||
|
|
return std::format("Once on {}", moment(schedule.start));
|
||
|
|
}
|
||
|
|
static constexpr std::array<std::pair<std::string_view, std::string_view>, 5> units = {{
|
||
|
|
{"HOURLY", "hour"}, {"DAILY", "day"}, {"WEEKLY", "week"}, {"MONTHLY", "month"}, {"YEARLY", "year"},
|
||
|
|
}};
|
||
|
|
const auto found = std::ranges::find(units, schedule.unit, &std::pair<std::string_view, std::string_view>::first);
|
||
|
|
// A unit the bank has and this does not know yet is said as the bank says it.
|
||
|
|
const std::string unit = found != units.end() ? std::string(found->second) : Words(schedule.unit);
|
||
|
|
std::string label = schedule.every == 1 ? std::format("Every {} from {}", unit, moment(schedule.start)) : std::format("Every {} {}s from {}", schedule.every, unit, moment(schedule.start));
|
||
|
|
if(schedule.end) {
|
||
|
|
label += std::format(" until {}", moment(*schedule.end));
|
||
|
|
}
|
||
|
|
return label;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::Fingerprint(std::string_view secret) {
|
||
|
|
// Too short to show a part of without showing most of it.
|
||
|
|
if(secret.size() < 16) {
|
||
|
|
return "••••";
|
||
|
|
}
|
||
|
|
// Up to its first underscore, which is where a bunq key says what it is for ("sandbox_"), or its first four.
|
||
|
|
const std::size_t mark = secret.find('_');
|
||
|
|
const std::string_view start = mark != std::string_view::npos && mark < 12 ? secret.substr(0, mark + 1) : secret.substr(0, 4);
|
||
|
|
return std::format("{}…{}", start, secret.substr(secret.size() - 4));
|
||
|
|
}
|