diff options
author | ouwou <26526779+ouwou@users.noreply.github.com> | 2022-01-08 20:11:52 -0500 |
---|---|---|
committer | ouwou <26526779+ouwou@users.noreply.github.com> | 2022-01-08 20:11:52 -0500 |
commit | f31d431517b27a88a95f6972de8fcf3206df1da1 (patch) | |
tree | 772ee0241f8f79c510aa158e0d5f1a04302214c9 /src/util.cpp | |
parent | f19dcc01145edd2e69a8cccbc59258b6b73bd704 (diff) | |
parent | 604f2ffe3dc8978aebd6aa819b73374aa32d2f0e (diff) | |
download | abaddon-portaudio-f31d431517b27a88a95f6972de8fcf3206df1da1.tar.gz abaddon-portaudio-f31d431517b27a88a95f6972de8fcf3206df1da1.zip |
Merge branch 'unread' into msys
Diffstat (limited to 'src/util.cpp')
-rw-r--r-- | src/util.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/util.cpp b/src/util.cpp index 1a7182d..0ebe73e 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,4 +1,5 @@ #include "util.hpp" +#include <array> #include <filesystem> #include <array> @@ -214,3 +215,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<int, 12> 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; +} |