2024-08-16 21:35:12 +02:00
|
|
|
/*
|
|
|
|
* Mirai. Copyright (C) 2024 Vyn
|
|
|
|
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
|
|
|
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Utils.h"
|
2024-10-29 15:02:46 +01:00
|
|
|
#include <cctype>
|
|
|
|
#include <format>
|
|
|
|
#include <string>
|
2024-08-16 21:35:12 +02:00
|
|
|
|
2024-11-04 14:45:27 +01:00
|
|
|
void bindSlintUtils(const ui::Utils &utils)
|
|
|
|
{
|
|
|
|
utils.on_format_date([&](const ui::Date &date) {
|
|
|
|
std::chrono::year_month_day chronoDate{
|
|
|
|
std::chrono::year(date.year),
|
|
|
|
std::chrono::month(date.month),
|
|
|
|
std::chrono::day(date.day),
|
|
|
|
};
|
|
|
|
return std::format("{:%B %d}", chronoDate);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:35:12 +02:00
|
|
|
std::string formatZeroPadding(const int number)
|
|
|
|
{
|
|
|
|
if (number < 10) {
|
|
|
|
return "0" + std::to_string(number);
|
|
|
|
}
|
|
|
|
return std::to_string(number);
|
|
|
|
}
|
|
|
|
|
2024-10-29 15:02:46 +01:00
|
|
|
std::string formatDateRelative(const ui::Date &date)
|
|
|
|
{
|
|
|
|
auto todayDate = mirai::Date(std::chrono::system_clock::now());
|
|
|
|
auto relativeDaysDiff = std::chrono::duration_cast<std::chrono::days>(
|
|
|
|
std::chrono::sys_days(SlintDateToMiraiDate(date).toStdChrono()) -
|
|
|
|
std::chrono::sys_days(todayDate.toStdChrono())
|
|
|
|
)
|
|
|
|
.count();
|
|
|
|
|
|
|
|
if (relativeDaysDiff == 0) {
|
|
|
|
return std::string("today");
|
|
|
|
} else if (relativeDaysDiff == 1) {
|
|
|
|
return std::string("tomorrow");
|
|
|
|
}
|
|
|
|
return std::format("in {} days", relativeDaysDiff);
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string capitalize(std::string str)
|
|
|
|
{
|
|
|
|
str[0] = static_cast<char>(toupper(str[0]));
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2024-10-15 16:51:05 +02:00
|
|
|
std::string SlintDateToStdString(const ui::Date &date)
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
|
|
|
return std::to_string(date.year) + "-" + formatZeroPadding(date.month) + "-" +
|
|
|
|
formatZeroPadding(date.day);
|
|
|
|
}
|
|
|
|
|
2024-10-15 16:51:05 +02:00
|
|
|
mirai::Date SlintDateToMiraiDate(const ui::Date &date)
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
|
|
|
return mirai::Date(
|
|
|
|
date.year, static_cast<unsigned>(date.month), static_cast<unsigned>(date.day)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-15 16:51:05 +02:00
|
|
|
ui::Date MiraiDateToSlintDate(const mirai::Date &date)
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
|
|
|
return {
|
|
|
|
.year = date.year,
|
|
|
|
.month = static_cast<int>(date.month),
|
|
|
|
.day = static_cast<int>(date.day),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-10-15 16:51:05 +02:00
|
|
|
ui::Time MiraiTimeToSlintTime(const mirai::Time &time)
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
|
|
|
return {.hour = time.hour, .minute = time.minute, .second = 0};
|
|
|
|
}
|
|
|
|
|
2024-10-15 16:51:05 +02:00
|
|
|
mirai::Time SlintTimeToMiraiTime(const ui::Time &time)
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
|
|
|
return {.hour = time.hour, .minute = time.minute};
|
|
|
|
}
|