#include "T2Archive.h" #include "T2DateTime.h" #include "T2TowerDoc.h" #include "T2WorldPlugin.h" T2DateTime::T2DateTime() { mYear = 0; mMonth = 0; mRawMinutes = 0; mSeconds = 0; } T2DateTime::T2DateTime(int year, int month, int hours, int minutes, int seconds) { mYear = year; mMonth = month; mRawMinutes = hours * 60 + minutes; mSeconds = seconds; } T2DateTime::T2DateTime(T2Archive& archive) { unsigned short v; archive >> v; mYear = v; archive >> v; mMonth = v; archive >> v; mRawMinutes = v; archive >> v; mSeconds = v; } /*virtual*/ T2DateTime::~T2DateTime() { } int T2DateTime::GetTimeZone() { int h = Get24Hour(); if (h < 2) return 0; if (h < 6) return 1; if (h < 9) return 2; if (h < 10) return 3; if (h < 12) return 4; if (h < 13) return 5; if (h < 15) return 6; if (h < 18) return 7; if (h < 22) return 8; return 9; } /*static*/ BOOL T2DateTime::IsEqualDate(T2DateTime* a, T2DateTime* b) { return (a->mYear == b->mYear) && (a->mMonth == b->mMonth); } /*static*/ BOOL T2DateTime::IsEqualDateTime(T2DateTime* a, T2DateTime* b) { return IsEqualDate(a, b) && (a->mRawMinutes == b->mRawMinutes); } void T2DateTime::AddMinutes(unsigned int m) { mRawMinutes += m; Validate(); } BOOL T2DateTime::IsIncDateTime() { BOOL result = false; mSeconds++; if (mSeconds >= 4) { result = true; mSeconds = 0; mRawMinutes++; if (mRawMinutes >= 1440) { mMonth++; mRawMinutes -= 1440; } if (mMonth >= 12) { mYear++; mMonth -= 12; } } return result; } void T2DateTime::IncDate() { mMonth++; if (mMonth >= 12) { mYear++; mMonth -= 12; } } unsigned int T2DateTime::CalcLapseDays(unsigned int d) const { return CalcTotalDays() - d; } unsigned int T2DateTime::CalcLapseYears(unsigned int d) const { return (CalcTotalDays() - d) / 12; } BOOL T2DateTime::WithinHour(int a, int b) const { return WithinMinutes(a * 60, b * 60); } BOOL T2DateTime::WithinMinutes(int a, int b) const { BOOL isWithin = true; int minL = AdjustMinutes(a); int minR = AdjustMinutes(b); if (minL < minR) { if ((mRawMinutes < minL) || (mRawMinutes >= minR)) isWithin = false; } else if (minL > minR) { if ((mRawMinutes >= minR) && (mRawMinutes < minL)) isWithin = false; } return isWithin; } int T2DateTime::AdjustMinutes(int m) const { int result = m; if (result < 0) result += 1440; else if (result >= 1440) result -= 1440; return result; } /*virtual*/ void T2DateTime::Write(T2Archive& archive) { unsigned short v = mYear; archive << v; v = mMonth; archive << v; v = mRawMinutes; archive << v; v = mSeconds; archive << v; } BOOL T2DateTime::IsHoliday(T2TowerDoc* doc) { T2WorldPlugin *thePlugin = doc->mWorldPlugin; return thePlugin->IsHoliday(this); } void T2DateTime::Validate() { mRawMinutes += (mSeconds / 4); mSeconds = mSeconds % 4; mMonth += (mRawMinutes / 1440); mRawMinutes %= 1440; mYear += (mMonth / 12); mMonth %= 12; }