Skip to content

Commit 87362a9

Browse files
committed
Granularity
1 parent c8dea72 commit 87362a9

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/api/retirement_api.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020
using namespace budget;
2121

2222
void budget::retirement_countdown_api(const httplib::Request& req, httplib::Response& res) {
23-
if (auto fi_expenses = budget::get_fi_expenses()){
23+
if (auto fi_expenses = budget::get_fi_expenses()) {
2424
data_cache cache;
2525

2626
const auto nw = get_fi_net_worth(cache);
2727
const auto savings_rate = running_savings_rate(cache);
2828
const auto income = running_income(cache);
2929

30-
const auto currency = get_default_currency();
31-
const auto wrate = to_number<double>(internal_config_value("withdrawal_rate"));
32-
const auto roi = to_number<double>(internal_config_value("expected_roi"));
33-
const auto years = double(100.0 / wrate);
34-
const auto fi_goal = years * fi_expenses;
30+
const auto currency = get_default_currency();
31+
const auto wrate = to_number<double>(internal_config_value("withdrawal_rate"));
32+
const auto roi = to_number<double>(internal_config_value("expected_roi"));
33+
const auto years = double(100.0 / wrate);
34+
const auto fi_goal = years * fi_expenses;
3535

3636
date_type fi_base_months = 0;
3737
{
@@ -47,12 +47,30 @@ void budget::retirement_countdown_api(const httplib::Request& req, httplib::Resp
4747
if (nw > fi_goal) {
4848
api_success_content(req, res, "You are FI!");
4949
} else {
50-
// TODO Add days granularity
51-
52-
const auto missing_years = unsigned(fi_base_months / 12);
50+
// Years and months, we get from the base months
51+
const auto missing_years = unsigned(fi_base_months / 12);
5352
const auto missing_months = unsigned(fi_base_months - (missing_years * 12));
5453

55-
auto result = std::format("{} years, {} months", missing_years, missing_months);
54+
// Days until the end of the month
55+
const auto missing_days = budget::local_day().end_of_month().day().value - budget::local_day().day().value;
56+
57+
// Hours/Minutes/Seconds until the end of the day
58+
const auto now = std::chrono::system_clock::now();
59+
const auto zone = std::chrono::current_zone();
60+
const auto time = zone->to_local(now);
61+
const std::chrono::hh_mm_ss hms{time - std::chrono::floor<std::chrono::days>(time)};
62+
63+
const auto missing_hours = 24 - hms.hours().count();
64+
const auto missing_minutes = 60 - hms.minutes().count();
65+
const auto missing_seconds = 60 - hms.seconds().count();
66+
67+
auto result = std::format("{} years, {} months, {} days, {} hours, {} minutes, {} seconds",
68+
missing_years,
69+
missing_months,
70+
missing_days,
71+
missing_hours,
72+
missing_minutes,
73+
missing_seconds);
5674

5775
api_success_content(req, res, result);
5876
}

0 commit comments

Comments
 (0)