aboutsummaryrefslogtreecommitdiff
path: root/C++/Time Header.wiki
blob: 11ace1336942605f1b6fa2e00967e724ebe50965 (plain)
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
= Time Header =

A silly header which allows timestamps to be written in english.

{{{c++
#pragma once

#include <ostream>

enum timetype {
	_day, _hour, _minute, _second
};

class Time {
private:

	int _days = 0;
	int _hours = 0;
	int _minutes = 0;
	int _seconds = 0;

public:
	Time() { };
	Time(int n, timetype type);

	int to_seconds() const;

	friend std::ostream& operator<<(std::ostream&, const Time&);
	friend Time operator * (int, Time);

	Time operator +  (const Time&);
	Time operator ,  (const Time& t) { return *this + t; }
	Time operator && (const Time& t) { return *this + t; }
};

#define days * Time(1, _day)
#define day days
#define hours * Time(1, _hour)
#define hour hours
#define minutes * Time(1, _minute)
#define minute minutes
#define seconds * Time(1, _second)
#define second seconds

Time::Time(int n, timetype type) {
	switch (type) {
		case _day:    this->_days    = n; break;
		case _hour:   this->_hours   = n; break;
		case _minute: this->_minutes = n; break; break;
		case _second: this->_seconds = n; break; break;
	}
}

Time operator *  (int s, Time t) {
	Time u;
	u._days    = t._days    * s;
	u._hours   = t._hours   * s;
	u._minutes = t._minutes * s;
	u._seconds = t._seconds * s;
	return u;
}

Time Time::operator + (const Time& t) {
	Time u;
	u._days    = this->_days    + t._days   ;
	u._hours   = this->_hours   + t._hours  ;
	u._minutes = this->_minutes + t._minutes;
	u._seconds = this->_seconds + t._seconds;
	return u;
}

int Time::to_seconds() const {
	return
		60 * 60 * 24 * this->_days +
		60 * 60 * this->_hours +
		60 * this->_minutes +
		this->_seconds;
}


std::ostream& operator<<(std::ostream& out, const Time& time) {
	out << time.to_seconds() << " seconds";
	return out;
}

/* -------------------------------------------------- */

int main() {
    std::cout
    << 5 days                            << std::endl
    << 7 minutes                         << std::endl
    << (5 days and 3 seconds)            << std::endl
    << (7 days, 7 minutes and 3 seconds) << std::endl
    << (9 hours and 1 second)            << std::endl
    ;
}
}}}