aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/time.cpp57
-rwxr-xr-xsrc/time.h9
2 files changed, 66 insertions, 0 deletions
diff --git a/src/time.cpp b/src/time.cpp
index 384eb83..29af5be 100755
--- a/src/time.cpp
+++ b/src/time.cpp
@@ -2,6 +2,8 @@
// Look. listen here. There's no way I'm going to start taking DST into account.
// I have to draw the line somewhere, and frankly, once you start doing "Change an hour on the 4th moon of the 2nd week of March in France, but only if the tulips haven't sprung... etc... etc.." I'm out.
+// The fact that DST is designed this way, though, luckily makes it so you are unlikely to be on the clock during a DST transition.
+
//
// --- OPERATOR OVERLOADS ---
@@ -31,6 +33,61 @@ bool moment::operator==(const moment& other) const {
minutes==other.minutes);
}
+bool moment::operator!=(const moment& other) const {
+ return bool(year!=other.year ||
+ month!=other.month ||
+ day!=other.day ||
+ hours!=other.hours ||
+ minutes!=other.minutes);
+}
+
+delta moment::operator-(const moment& other) const {
+ // Uses what I call an accumulator-decumulator design
+ // Count how long it takes to approach a benchmark,
+ // and that's the difference
+ //
+ if(*this==other) return{0,0,0};
+ delta accumulator{0,0,0};
+
+ // smallest operand becomes benchmark to approach
+ const bool reverse{*this<other};
+ const moment& benchmark = reverse? *this : other;
+ moment decumulator = reverse? other : *this;
+
+ // It is possible to write something that does this in months at a time, instead of days,
+ // which would be faster, but I am not expecting to have to do this with such
+ // long periods of time, so screw that.
+ while(decumulator.year - benchmark.year > 1 ||
+ decumulator.month - benchmark.month > 1 ||
+ decumulator.day - benchmark.day > 1) {
+ wind(decumulator, 0, 0, 1);
+ accumulator.days++;
+ }
+ while(decumulator.hours - benchmark.hours > 1) {
+ wind(decumulator, 0, 1, 0);
+ accumulator.hours++;
+ }
+ while(decumulator != benchmark) {
+ wind(decumulator, 1, 0, 0);
+ accumulator.minutes++;
+ }
+ return accumulator;
+}
+
+
+
+//
+// --- METHODS ---
+//
+
+double timeblock::hourcount() {
+ delta timedelta = end-start;
+ return (timedelta.minutes/60.0f +
+ timedelta.hours +
+ timedelta.days*24);
+}
+
+
//
// --- FUNCTIONS ---
diff --git a/src/time.h b/src/time.h
index 213615f..b512e91 100755
--- a/src/time.h
+++ b/src/time.h
@@ -6,6 +6,12 @@
#include <string.h>
#include <vector>
+struct delta{
+ signed int minutes;
+ signed int hours;
+ signed int days;
+};
+
struct moment{
signed int minutes;
signed int hours; // 24-hour format. 23:59 progresses to 00:00
@@ -16,11 +22,14 @@ struct moment{
bool operator<(const moment& other) const;
bool operator>(const moment& other) const;
bool operator==(const moment& other) const;
+ bool operator!=(const moment& other) const;
+ delta operator-(const moment& other) const;
};
struct timeblock{
moment start;
moment end;
+ double hourcount();
};
struct workday{