summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2DateTime.cpp
diff options
context:
space:
mode:
authorAsh Wolf <ninji@wuffs.org>2023-06-14 00:50:34 +0100
committerAsh Wolf <ninji@wuffs.org>2023-06-14 00:50:34 +0100
commit37e364b2c6cc7487a1c888d256a73e5337bb7189 (patch)
treeeaf6e857382eef16c2dd940eb4125536fbe068bd /src/T2DLL/T2DateTime.cpp
downloadt2win-37e364b2c6cc7487a1c888d256a73e5337bb7189.tar.gz
t2win-37e364b2c6cc7487a1c888d256a73e5337bb7189.zip
initial commit
Diffstat (limited to '')
-rw-r--r--src/T2DLL/T2DateTime.cpp153
1 files changed, 153 insertions, 0 deletions
diff --git a/src/T2DLL/T2DateTime.cpp b/src/T2DLL/T2DateTime.cpp
new file mode 100644
index 0000000..5cb704d
--- /dev/null
+++ b/src/T2DLL/T2DateTime.cpp
@@ -0,0 +1,153 @@
+#include "T2Archive.h"
+#include "T2DateTime.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*/ int T2DateTime::IsEqualDate(T2DateTime* a, T2DateTime* b) {
+ return (a->mYear == b->mYear) && (a->mMonth == b->mMonth);
+}
+
+/*static*/ int T2DateTime::IsEqualDateTime(T2DateTime* a, T2DateTime* b) {
+ return IsEqualDate(a, b) && (a->mRawMinutes == b->mRawMinutes);
+}
+
+void T2DateTime::AddMinutes(unsigned int m) {
+ mRawMinutes += m;
+ Validate();
+}
+
+int T2DateTime::IsIncDateTime() {
+ int 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;
+}
+
+int T2DateTime::WithinHour(int a, int b) const {
+ return WithinMinutes(a * 60, b * 60);
+}
+
+int T2DateTime::WithinMinutes(int a, int b) const {
+ int ret = true;
+
+ int minA = AdjustMinutes(a);
+ int minB = AdjustMinutes(b);
+
+ if (minA < minB) {
+ if ((mRawMinutes < minA) || (mRawMinutes >= minB))
+ ret = false;
+ } else if (minA > minB) {
+ if ((mRawMinutes >= minB) && (mRawMinutes < minA))
+ ret = false;
+ }
+
+ return ret;
+}
+
+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;
+}
+
+int T2DateTime::IsHoliday(T2TowerDoc* doc) {
+ // TODO virt
+ return false;
+}
+
+void T2DateTime::Validate() {
+ mRawMinutes += (mSeconds / 4);
+ mSeconds = mSeconds % 4;
+
+ mMonth += (mRawMinutes / 1440);
+ mRawMinutes %= 1440;
+
+ mYear += (mMonth / 12);
+ mMonth %= 12;
+}