mirai/lib/mirai-core/DateTime.h
2024-08-29 11:18:09 +02:00

95 lines
2 KiB
C++

/*
* 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
*/
#pragma once
#include <bits/chrono.h>
#include <chrono>
#include <ctime>
#include <optional>
#include <string>
namespace mirai
{
struct Date {
explicit Date(int year, unsigned month, unsigned day) : year(year), month(month), day(day)
{
}
explicit Date(std::chrono::time_point<std::chrono::system_clock> tp)
{
auto chronoDate = std::chrono::year_month_day(std::chrono::floor<std::chrono::days>(tp));
year = static_cast<int>(chronoDate.year());
month = static_cast<unsigned>(chronoDate.month());
day = static_cast<unsigned>(chronoDate.day());
}
explicit Date(std::chrono::year_month_day chronoDate)
{
year = static_cast<int>(chronoDate.year());
month = static_cast<unsigned>(chronoDate.month());
day = static_cast<unsigned>(chronoDate.day());
}
int year;
unsigned month;
unsigned day;
bool operator==(const Date &other) const
{
return other.year == year && other.month == month && other.day == day;
}
bool operator<(const Date &other) const
{
if (year < other.year) {
return true;
}
if (year == other.year && month < other.month) {
return true;
}
if (year == other.year && month == other.month && day < other.day) {
return true;
}
return false;
}
bool operator>(const Date &other) const
{
return !(*this < other) && !(*this == other);
}
};
struct Time {
int hour;
int minute;
bool operator==(const Time &other) const
{
return other.hour == hour && other.minute == minute;
}
bool operator<(const Time &other) const
{
if (hour < other.hour) {
return true;
}
if (hour == other.hour && minute < other.minute) {
return true;
}
return false;
}
bool operator>(const Time &other) const
{
return !(*this < other) && !(*this == other);
}
};
std::optional<mirai::Date> stringToDate(const std::string &dateStr);
} // namespace mirai