mirror of
https://github.com/whoahq/whoa.git
synced 2026-03-18 05:31:07 +03:00
feat(util): add WowTime::AddDays
This commit is contained in:
parent
661b77091f
commit
aed2aebb66
@ -1,6 +1,7 @@
|
|||||||
#include "util/time/WowTime.hpp"
|
#include "util/time/WowTime.hpp"
|
||||||
#include <storm/Error.hpp>
|
#include <storm/Error.hpp>
|
||||||
#include <storm/String.hpp>
|
#include <storm/String.hpp>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
static const char* s_weekdays[] = {
|
static const char* s_weekdays[] = {
|
||||||
"Sun",
|
"Sun",
|
||||||
@ -157,6 +158,59 @@ char* WowTime::WowGetTimeString(WowTime* time, char* str, int32_t len) {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WowTime::AddDays(int32_t days, bool includeTime) {
|
||||||
|
// Validate date
|
||||||
|
|
||||||
|
if (this->m_year < 0 || this->m_month < 0 || this->m_monthday < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert WowTime to tm
|
||||||
|
|
||||||
|
tm t = {};
|
||||||
|
|
||||||
|
t.tm_year = this->m_year + 100; // WowTime year is years since 2000; tm_year is years since 1900
|
||||||
|
t.tm_mon = this->m_month; // WowTime month and tm_mon are both 0-based
|
||||||
|
t.tm_mday = this->m_monthday + 1; // WowTime monthday is 0-based; tm_mday is 1-based
|
||||||
|
t.tm_isdst = -1; // Let mktime determine DST
|
||||||
|
|
||||||
|
if (includeTime) {
|
||||||
|
t.tm_hour = this->m_hour;
|
||||||
|
t.tm_min = this->m_minute;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert tm to time_t and add the specified days
|
||||||
|
|
||||||
|
auto time = mktime(&t);
|
||||||
|
time += days * 86400;
|
||||||
|
|
||||||
|
|
||||||
|
if (!includeTime) {
|
||||||
|
time += 3600; // Tack on hour to ensure DST boundaries don't muck with days added
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert adjusted time back to tm
|
||||||
|
|
||||||
|
auto t_ = localtime(&time);
|
||||||
|
if (t_) {
|
||||||
|
t = *t_;
|
||||||
|
} else {
|
||||||
|
t = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert adjusted tm back to WowTime
|
||||||
|
|
||||||
|
this->m_year = t.tm_year - 100;
|
||||||
|
this->m_month = t.tm_mon;
|
||||||
|
this->m_monthday = t.tm_mday - 1;
|
||||||
|
this->m_weekday = t.tm_wday;
|
||||||
|
|
||||||
|
if (includeTime) {
|
||||||
|
this->m_hour = t.tm_hour;
|
||||||
|
this->m_minute = t.tm_min;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int32_t WowTime::GetHourAndMinutes() {
|
int32_t WowTime::GetHourAndMinutes() {
|
||||||
if (this->m_hour < 0 || this->m_minute < 0) {
|
if (this->m_hour < 0 || this->m_minute < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -23,6 +23,7 @@ class WowTime {
|
|||||||
int32_t m_holidayOffset = 0;
|
int32_t m_holidayOffset = 0;
|
||||||
|
|
||||||
// Member functions
|
// Member functions
|
||||||
|
void AddDays(int32_t days, bool includeTime);
|
||||||
int32_t GetHourAndMinutes();
|
int32_t GetHourAndMinutes();
|
||||||
void SetHourAndMinutes(int32_t minutes);
|
void SetHourAndMinutes(int32_t minutes);
|
||||||
int32_t SetHourAndMinutes(uint32_t hour, uint32_t minutes);
|
int32_t SetHourAndMinutes(uint32_t hour, uint32_t minutes);
|
||||||
|
|||||||
@ -129,6 +129,27 @@ TEST_CASE("WowTime::WowGetTimeString", "[util]") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("WowTime::AddDays", "[util]") {
|
||||||
|
SECTION("adds 1 day to 1/28/2026") {
|
||||||
|
WowTime time;
|
||||||
|
time.m_minute = 18;
|
||||||
|
time.m_hour = 11;
|
||||||
|
time.m_weekday = 3;
|
||||||
|
time.m_monthday = 27;
|
||||||
|
time.m_month = 0;
|
||||||
|
time.m_year = 26;
|
||||||
|
|
||||||
|
time.AddDays(1, false);
|
||||||
|
|
||||||
|
CHECK(time.m_minute == 18);
|
||||||
|
CHECK(time.m_hour == 11);
|
||||||
|
CHECK(time.m_weekday == 4);
|
||||||
|
CHECK(time.m_monthday == 28);
|
||||||
|
CHECK(time.m_month == 0);
|
||||||
|
CHECK(time.m_year == 26);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("WowTime::GetHourAndMinutes", "[util]") {
|
TEST_CASE("WowTime::GetHourAndMinutes", "[util]") {
|
||||||
SECTION("gets expected hour and minutes for default constructed time") {
|
SECTION("gets expected hour and minutes for default constructed time") {
|
||||||
WowTime time;
|
WowTime time;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user