From 41d80af128cc74e538a2f5d2474ca49febc52c35 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Sat, 25 Dec 2021 02:37:31 -0500 Subject: mark more channels as unread properly --- src/util.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/util.cpp') diff --git a/src/util.cpp b/src/util.cpp index 34ca6d4..6796a3f 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,4 +1,5 @@ #include "util.hpp" +#include #include Semaphore::Semaphore(int count) @@ -215,3 +216,31 @@ bool util::IsFile(std::string_view path) { if (ec) return false; return status.type() == std::filesystem::file_type::regular; } + +constexpr bool IsLeapYear(int year) { + if (year % 4 != 0) return false; + if (year % 100 != 0) return true; + return (year % 400) == 0; +} + +constexpr static int SecsPerMinute = 60; +constexpr static int SecsPerHour = 3600; +constexpr static int SecsPerDay = 86400; +constexpr static std::array DaysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + +// may god smite whoever is responsible for the absolutely abominable api that is C time functions +// i shouldnt have to write this. mktime ALMOST works but it adds the current timezone offset. WHY??? +uint64_t util::TimeToEpoch(int year, int month, int day, int hour, int minute, int seconds) { + uint64_t secs = 0; + for (int y = 1970; y < year; ++y) + secs += (IsLeapYear(y) ? 366 : 365) * SecsPerDay; + for (int m = 1; m < month; ++m) { + secs += DaysOfMonth[m - 1] * SecsPerDay; + if (m == 2 && IsLeapYear(year)) secs += SecsPerDay; + } + secs += (day - 1) * SecsPerDay; + secs += hour * SecsPerHour; + secs += minute * SecsPerMinute; + secs += seconds; + return secs; +} -- cgit v1.2.3