-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgratetime.cpp
115 lines (97 loc) · 2.8 KB
/
gratetime.cpp
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
/*******************
*
*
* GRATE 8
*
* Custom Date Time object
*
*
*
*********************/
#include "gratetime.h"
#include <iostream>
#include <ctime>
#include <cmath>
GrateTime::GrateTime(int year, int month, int day, int hour, int minute, int second) {
timeinfo.tm_year = year - 1900;
timeinfo.tm_mon = month;
timeinfo.tm_mday = day;
timeinfo.tm_hour = hour;
timeinfo.tm_min = minute;
timeinfo.tm_sec = second;
timeinfo.tm_isdst = 0;
std::mktime(&timeinfo);
}
// sets the date
void GrateTime::setDate(int year, int month, int day) {
timeinfo.tm_year = year - 1900;
timeinfo.tm_mon = month;
timeinfo.tm_mday = day;
// normalise it
std::mktime(&timeinfo);
}
// sets the time
void GrateTime::setTime(int hour, int minute, int second) {
timeinfo.tm_hour = hour;
timeinfo.tm_min = minute;
timeinfo.tm_sec = second;
// normalise it
std::mktime(&timeinfo);
}
void GrateTime::setExcelTime(double nSerialDate)
{
int i,j,l;
double k,m,n;
// https://www.codeproject.com/Articles/2750/Excel-Serial-Date-to-Day-Month-Year-and-Vice-Versa
if (nSerialDate == 60)
{
timeinfo.tm_mday = 29;
timeinfo.tm_mon = 1;
timeinfo.tm_year = 0;
std::mktime(&timeinfo);
return;
}
else if (nSerialDate < 60)
{
// Because of the 29-02-1900 bug, any serial date
// under 60 is one off... Compensate.
nSerialDate++;
}
// Modified Julian to DMY calculation with an addition of 2415019
l = floor(nSerialDate + 68569 + 2415019);
n = int((4 * l) / 146097);
l = l - int((146097 * n + 3) / 4);
i = int((4000 * (l + 1)) / 1461001);
l = l - int((1461 * i) / 4) + 31;
j = int((80 * l) / 2447);
timeinfo.tm_mday = l - int((2447 * j) / 80);
l = int(j / 11);
timeinfo.tm_mon = j + 2 - (12 * l) - 1; // struct tm numbers the months 0 to 11, not 1 to 12
timeinfo.tm_year = ( 100 * (n - 49) + i + l) - 1900;
k = (nSerialDate - floor(nSerialDate)) * 24 + 1;
timeinfo.tm_hour = floor(k);
m = (k - timeinfo.tm_hour) * 60;
timeinfo.tm_min = floor(m);
n = (m - timeinfo.tm_min) * 60;
timeinfo.tm_sec = round(n);
// normalise it
std::mktime(&timeinfo);
}
// prints a string representation to stdout
void GrateTime::print() {
std::cout << std::asctime(&timeinfo) << std::endl;
}
// adds the given number of seconds in-place
void GrateTime::addSecs(int secondsToAdd) {
timeinfo.tm_sec += secondsToAdd;
// normalise it
std::mktime(&timeinfo);
}
// returns the date-time as time_t type
std::time_t GrateTime::getTime_t() {
return mktime(&timeinfo);
}
// calculates the number of seconds to the given GrateTime object
int GrateTime::secsTo(GrateTime futureTime) {
return std::difftime(futureTime.getTime_t(), getTime_t());
}