1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
#include "time.h"
// Look. listen here. There's no way I'm going to start taking DST into account.
// DST transition times are decided using skull dice, by the grand wizards of the state.
// The fact that DST is designed this way, though,
// makes it so you are unlikely to be on the clock during a DST transition.
//
// --- OPERATOR OVERLOADS ---
//
long long sortable_time(const moment input_moment) {
return stoll(std::to_string(input_moment.year)+
padint(input_moment.month,2)+
padint(input_moment.day,2)+
padint(input_moment.hours,2)+
padint(input_moment.minutes,2));
}
bool moment::operator<(const moment& other) const{
return bool(sortable_time(*this) < sortable_time(other));
}
bool moment::operator>(const moment& other) const{
return bool(sortable_time(*this) > sortable_time(other));
}
bool moment::operator==(const moment& other) const {
return bool(year==other.year &&
month==other.month &&
day==other.day &&
hours==other.hours &&
minutes==other.minutes);
}
bool moment::operator<=(const moment& other) const {
return bool((*this < other) || *this == other);
}
bool moment::operator>=(const moment& other) const {
return bool((*this > other) || *this == other);
}
bool moment::operator!=(const moment& other) const {
return bool(year!=other.year ||
month!=other.month ||
day!=other.day ||
hours!=other.hours ||
minutes!=other.minutes);
}
moment moment::operator+(const delta& other) const {
moment output{*this};
wind(output, other.minutes, other.hours, other.days);
return output;
}
moment moment::operator-(const delta& other) const {
moment output{*this};
wind(output, other.minutes*-1, other.hours*-1, other.days*-1);
return output;
}
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 count is 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(accumulator.hours > 23) {
accumulator.hours -= 24;
accumulator.days++;
}
while(decumulator != benchmark) {
wind(decumulator, -1, 0, 0);
accumulator.minutes++;
}
while(accumulator.minutes > 59) {
accumulator.minutes -= 60;
accumulator.hours++;
}
return accumulator;
}
std::ostream& operator<<(std::ostream& stream, const delta& other) {
if(other.days==0 && other.hours==0 && other.minutes==0){
stream << "None.";
return stream;
}
if(other.days) stream << other.days << " days, ";
if(other.hours) stream << other.hours << " hours, ";
if(other.minutes) stream << other.minutes << " minutes.";
return stream;
}
moment timemax(moment a, moment b) {
if (a > b) return a;
return b;
}
//
// --- CONSTRUCTORS ---
//
workday::workday(const moment& previous_wrap,
const moment& calltime,
const moment& wraptime,
const moment& planned_wraptime) {
call = calltime;
wrap = wraptime;
planned_wrap = planned_wraptime;
timeblock initial_block{call,
std::max(
// Paragraph 6.7 says that up to 2 hours of unused warned overtime counts as worktime,
// though so that at least one hour of the unused overtime is not counted.
// (It's unclear if an 8-hour day that ends 3 hours in counts as having 5 hours of unused overtime)
std::clamp(planned_wrap-(delta){0, 1, 0}, wraptime, planned_wraptime+(delta){0, 2, 0}),
call+(delta){0, 4, 0})};
// ^ Minimum 4 hour day ^
const int sp_length = 11;
moment splitpoints[sp_length]{ // --$-- Points where the price may change --$-- //
// NOTE: Maybe this should also contain the valuefactor associated with the split.
// Probably the valuefactor leading up to the splitpoint, not the one after.
// Maybe this should be a struct?
// Or maybe I should just implement this badly at first just to get it working, and replace it later?
previous_wrap+(delta){0, 10, 0}, // Sleepbreach, 10 hours after previous wrap 0x
(moment){0, 5, call.day, call.month, call.year}, // 2 hours before 7, aka 5 1x
(moment){0, 6, call.day, call.month, call.year}, // 6 in the morning 2x
call+(delta){0, 8, 0}, // Normal 8 hours of work 3x
call+(delta){0, 9, 0}, // 1st hour of overtime is over 4x
call+(delta){0, 11, 0}, // 3st hour of overtime is over 5x
planned_wraptime, // End of warned overtime 6x
call+(delta){0, 14, 0}, // The 14-hour mark 7x
(moment){0, 22, call.day, call.month, call.year}, // 22:00 in the evening 8x
(moment){0, 23, call.day, call.month, call.year}+(delta){0, 1, 0}, // Midnight 9x
(moment){0, 23, call.day, call.month, call.year}+(delta){0, 7, 0}, // 6, next morning 10x
};
// Eliminate planned wrap, if it occurs within normal 8-hour period.
// This is to make sure the first period of time becomes a pure 8 hours,
// which makes detecting the main section of the workday easier.
if(splitpoints[6] < splitpoints[3]){
splitpoints[6] = splitpoints[3];
}
moment splitpoints_sorted[sp_length];
std::copy(splitpoints, splitpoints+sp_length, splitpoints_sorted);
std::sort(splitpoints_sorted, splitpoints_sorted + sp_length);
int j = 0;
for(int i = 0; i<sp_length; i++) {
const moment* each_moment = &splitpoints_sorted[i];
//std::cout << "Splitting: " << timeprint(*each_moment) << "\t\tJ: " << j << "\t\tI: " << i << std::endl;
// If each splitpoint moment is within the workday, and is not equal to the start of the current block
if(*each_moment > call && *each_moment < timemax(wrap, call+(delta){0,4,0}) && *each_moment != initial_block.start) {
blocks[j++] = timesplit(initial_block, *each_moment);
}
}
blocks[j++] = initial_block;
total_timeblocks = j;
// THE VALUE-FACTOR CALCULATION PART
// TODO: Consider replacing splitpoints[x] with redoing the math for code flexibility?
for(int ii=0; ii < total_timeblocks; ii++){
timeblock& each_block = blocks[ii];
//std::cout << "pricing: " << timeprint(each_block) << std::endl;
if(each_block.end <= splitpoints[0]) each_block.upvalue(3, "Sleep-breach"); // +200% for sleep-breach
if(each_block.start.hours >= 22) each_block.upvalue(2, "Night"); // Work between 22:00
if((each_block.end.hours == 6 && each_block.end.minutes == 0) ||// And 06:00
(each_block.end.hours <= 5)) each_block.upvalue(2, "Night"); // is +100%
if(each_block.start >= splitpoints[3]) {each_block.upvalue(1.5, "Overtime"); // Overtime
if(each_block.start.getweekday() == saturday) each_block.upvalue(2, "Saturday overtime");// on saturdays
}
if(each_block.start >= splitpoints[5]) each_block.upvalue(2, "Overtime"); // End of 3-hour cheap planned overtime
if(each_block.start >= planned_wraptime && // Unwarned overtime
each_block.start >= splitpoints[4]) each_block.upvalue(2, "Overtime"); // +100% after first hour
if(each_block.start >= splitpoints[7]) each_block.upvalue(3, "Far overtime"); // +200% beyond 14-hour mark
if(each_block.start.getweekday() == saturday) each_block.upvalue(1.5, "Saturday");// Saturdays are +50%
if(each_block.start.getweekday() == sunday) each_block.upvalue(2, "Sunday"); // Sundays are +100%
if(!(call < (moment){0, 7, call.day, call.month, call.year} && // On an offset day...
std::min(call+(delta){0,8,0}, wrap) < (moment){0,17,call.day,call.month,call.year})) {
// This was added for rule 6.11c, but in a world without a defined normal workday,
// that rule is already covered already by 6.11g, so this is empty.
}
// Holidays!
if(each_block.start.day==1 && each_block.start.month==1) each_block.upvalue(2, "New year");
if(each_block.start.day==1 && each_block.start.month==5) each_block.upvalue(2, "1st of May");
if(each_block.start.day==17 && each_block.start.month==5) each_block.upvalue(2, "17st of May");
if((each_block.start.day==25 || each_block.start.day==26) && each_block.start.month==12)
each_block.upvalue(2, "Christmas");
moment easter = gaussEaster(each_block.start.year);
if(each_block.start.day == (easter-(delta){0,0,3}).day &&
each_block.start.month == (easter-(delta){0,0,3}).month) each_block.upvalue(2, "Maundy Thursday");
if(each_block.start.day == (easter-(delta){0,0,2}).day &&
each_block.start.month == (easter-(delta){0,0,2}).month) each_block.upvalue(2, "Good Friday");
if(each_block.start.day == easter.day && each_block.start.month == easter.month) each_block.upvalue(2, "Easter");
if(each_block.start.day == (easter+(delta){0,0,1}).day &&
each_block.start.month == (easter+(delta){0,0,1}).month) each_block.upvalue(2, "Easter");
if(each_block.start.day == (easter+(delta){0,0,39}).day &&
each_block.start.month == (easter+(delta){0,0,39}).month) each_block.upvalue(2, "Feast of the Ascension");
if(each_block.start.day == (easter+(delta){0,0,49}).day &&
each_block.start.month == (easter+(delta){0,0,49}).month) each_block.upvalue(2, "Pentecost");
if(each_block.start.day == (easter+(delta){0,0,50}).day &&
each_block.start.month == (easter+(delta){0,0,50}).month) each_block.upvalue(2, "Pentecost Monday");
}
}
void workday::lunch(const moment& lunch_start, const moment& lunch_end) {
if(lunch_start > lunch_end){
std::cout << "ERROR: Lunch ends before it began." << std::endl;
}
for(int ii=0; ii < total_timeblocks; ii++){
timeblock& each_block = blocks[ii];
timeblock& next_block = blocks[ii+1]; // FIXME: On final loop, next_block will be garbage data
if(each_block.start < lunch_start && each_block.end > lunch_end){
// If lunch simply occurs within a timeblock
// Split out the section, discarding the middle
timeblock first_half = timesplit(each_block, lunch_start);
each_block.start = lunch_end;
// Move all points after lunch out by 1
for(int x=total_timeblocks; x>=ii; x--) {
blocks[x+1] = blocks[x];
}
total_timeblocks++;
// Re-insert second half of split section into ii+1
blocks[ii] = first_half;
return;
} else if(ii!=total_timeblocks-1 && each_block.start < lunch_start && lunch_start < each_block.end &&
next_block.start < lunch_end && lunch_end < next_block.end) {
// If we're not on the final block AND
// If lunch occurs between two timeblocks
each_block.end = lunch_start;
next_block.start = lunch_end;
return;
} else if(each_block.start == lunch_start) {
// If lunch starts at the beginning of a timeblock
each_block.start = lunch_end;
return;
} else if(each_block.end == lunch_end) {
// If lunch ends at the end of a timeblock
each_block.start = lunch_end;
return;
}
// FIXME: If lunch spans across more than 1 border between timeblocks, bad stuff will happen.
// Maybe there is a more principled way of solving this, that doesn't require writing code
// for a bunch of edge-cases. If not, write the code.
}
return;
}
//
// --- METHODS ---
//
double timeblock::hourcount() {
delta timedelta = end-start;
return (timedelta.minutes/60.0f +
timedelta.hours +
timedelta.days*24);
}
float timeblock::upvalue(float suggestion, std::string reason){
if(suggestion>valuefactor) {
valuefactor = suggestion;
price_reason = reason;
}
return valuefactor;
}
weekday moment::getweekday() {
// Based on implementation from NProg on StackOverflow. Thanks.
int y = year;
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= month < 3;
return static_cast<weekday>((y + y / 4 - y / 100 + y / 400 + t[month - 1] + day - 1) % 7);
}
bool moment::isEaster() {
moment easter = gaussEaster(year);
return (easter.month==month && easter.day==day);
}
//
// --- FUNCTIONS ---
//
moment gaussEaster(int year) {
// Thanks to Carl Friedrich Gauss for the algorythm
// Thanks rahulhegde97, bansal_rtk_, code_hunt, sanjoy_62, simranarora5sos
// and aashutoshparoha on GeeksForGeeks for the implementation I based this on.
float A, B, C, P, Q, M, N, D, E;
int easter_month = 0;
int easter_day = 0;
// All calculations done
// on the basis of
// Gauss Easter Algorithm
A = year % 19;
B = year % 4;
C = year % 7;
P = std::floor((float)year / 100.0);
Q = std::floor((float)(13 + 8 * P) / 25.0);
M = (int)(15 - Q + P - (std::floor)(P / 4)) % 30;
N = (int)(4 + P - (std::floor)(P / 4)) % 7;
D = (int)(19 * A + M) % 30;
E = (int)(2 * B + 4 * C + 6 * D + N) % 7;
int days = (int)(22 + D + E);
easter_day = days;
// A corner case,
// when D is 29
if ((D == 29) && (E == 6)) {
easter_month = 4;
easter_day = 19;
}
// Another corner case,
// when D is 28
else if ((D == 28) && (E == 6)) {
easter_month = 4;
easter_day = 18;
}
else {
// If days > 31, move to April
// April = 4th Month
if (days > 31) {
easter_month = 04;
easter_day = days-31;
}
else {
// Otherwise, stay on March
// March = 3rd Month
easter_month = 03;
}
}
return (moment){0,0, easter_day, easter_month, year};
}
std::string padint(const int input, const int minimum_signs) {
std::ostringstream output;
output << std::internal << std::setfill('0') << std::setw(minimum_signs) << input;
return output.str();
}
timeblock timesplit(timeblock& input_block, const moment splitpoint) {
// Splits a timeblock at splitpoint.
// It changes the input_block to start at splitpoint, and returns a new timeblock
// that lasts from where the input_block used to start, to splitpoint.
// BASICALLY: input_block becomes first half, output is second half.
if(splitpoint <= input_block.start || splitpoint >= input_block.end) {
std::cerr << "ERROR: Splitpoint outside of timeblock!\n";
std::cerr << "Timeblock: " << timeprint(input_block) << std::endl;
std::cerr << "Splitpoint: " << timeprint(splitpoint) << std::endl;
}
timeblock output{input_block.start, splitpoint};
output.valuefactor = input_block.valuefactor;
output.price_reason = input_block.price_reason;
input_block.start = splitpoint; // Note: Now, reversed.
return output;
}
void wind(moment& input_moment, const int minutes, const int hours, const int days) {
// Adding minutes
input_moment.minutes += minutes;
while(input_moment.minutes > 59) {
input_moment.minutes -= 60;
input_moment.hours++;
}
while(input_moment.minutes < 0) {
input_moment.minutes += 60;
input_moment.hours--;
}
// Adding hours
input_moment.hours += hours;
while(input_moment.hours > 23) {
input_moment.hours -= 24;
input_moment.day++;
}
while(input_moment.hours < 0) {
input_moment.hours += 24;
input_moment.day--;
}
// Adding days
input_moment.day += days;
int current_month_length = days_in(input_moment.month, input_moment.year);
while(input_moment.day > current_month_length) {
input_moment.day -= current_month_length;
input_moment.month++;
if(input_moment.month > 12) {
input_moment.month -= 12;
input_moment.year++;
}
current_month_length = days_in(input_moment.month, input_moment.year);
}
while(input_moment.day < 1) {
input_moment.month--;
if(input_moment.month < 1) {
input_moment.month += 12;
input_moment.year--;
}
current_month_length = days_in(input_moment.month, input_moment.year);
input_moment.day += current_month_length;
}
}
void wind(moment& input_moment, const delta& time_delta) {
wind(input_moment, time_delta.minutes, time_delta.hours, time_delta.days);
}
std::string timeprint(moment input_moment) {
using namespace std;
string output =
padint(input_moment.hours, 2) + ":"
+ padint(input_moment.minutes, 2) + " "
+ to_string(input_moment.year) + "-"
+ padint(input_moment.month, 2) + "-"
+ padint(input_moment.day, 2);
return output;
}
std::string timeprint(moment input_moment, bool clockonly) {
using namespace std;
string output;
if(clockonly) {
output =
padint(input_moment.hours, 2) + ":"
+ padint(input_moment.minutes, 2);
} else {
output =
to_string(input_moment.year) + "-"
+ padint(input_moment.month, 2) + "-"
+ padint(input_moment.day, 2);
}
return output;
}
std::string timeprint(timeblock input_timeblock) {
std::string output{timeprint(input_timeblock.start) + " --> " + timeprint(input_timeblock.end)};
return output;
}
std::string timeprint(timeblock input_timeblock, bool clockonly) {
std::string output{timeprint(input_timeblock.start, clockonly) + " --> " + timeprint(input_timeblock.end, clockonly)};
return output;
}
int days_in(int month, int year) {
// Kind of a stupid and slow way to do this
// But it's nice to have it as a function
// because of the leap year arithmatic
switch (month) {
case 1:
return 31;
case 2:
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
return 29;
}
return 28;
case 3:
return 31;
case 4:
return 30;
case 5:
return 31;
case 6:
return 30;
case 7:
return 31;
case 8:
return 31;
case 9:
return 30;
case 10:
return 31;
case 11:
return 30;
case 12:
return 31;
}
std::cout << "Something just went very wrong. You found month #" << std::to_string(month) << '\n';
return 5;
}
// TODO: Add checks for correct formatting, and ask for new input if wrong
moment timeinput(moment input_moment) {
char input_string[6];
std::cout << "HH MM (24-hour format, use space)" << std::endl;
std::cin.getline(input_string, 6);
// This uglyness is just how you use strtok() to split a string, apparently
const char* p;
int split_input[2];
int i{0};
p = strtok(input_string, " ");
while (p != NULL) {
split_input[i] = int(atoi(p));
i++;
p = strtok(NULL, " ");
}
if((moment){split_input[1], split_input[0],
input_moment.day, input_moment.month, input_moment.year} < input_moment)
{
wind(input_moment, 0, 0, 1);
}
moment output{split_input[1], split_input[0],
input_moment.day, input_moment.month, input_moment.year};
return output;
}
moment timeinput() {
char input_string[17];
std::cout << "YEAR MM DD hh mm (24-hour format, use spaces)\n";
std::cin.getline(input_string, 17);
// This uglyness is just how you use strtok() to split a string, apparently
const char* p;
int split_input[5];
int i{0};
p = strtok(input_string, " ");
while (p != NULL) {
split_input[i] = int(atoi(p));
i++;
p = strtok(NULL, " ");
}
moment output{split_input[4],
split_input[3],
split_input[2],
split_input[1],
split_input[0]};
return output;
}
|