forked from NIVANorge/Mobius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetime.h
201 lines (167 loc) · 4.72 KB
/
datetime.h
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
//NOTE: Just a very simple datetime library that has what we need for our purposes.
//NOTE: Apparently the C++ standard library can not do all the date processing we need until c++20, so we have to do it ourselves... (could use boost::ptime, but it has to be compiled separately, and that is asking a lot of the user...)
#if !defined(DATETIME_H)
inline bool
IsLeapYear(s32 Year)
{
if(Year % 4 != 0) return false;
if(Year % 100 != 0) return true;
if(Year % 400 != 0) return false;
return true;
}
inline s32
YearLength(s32 Year)
{
return 365 + IsLeapYear(Year);
}
inline s32
MonthLength(s32 Year, s32 Month)
{
//NOTE: Returns the number of days in a month. The months are indexed from 0 in this context.
s32 Length[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
s32 Days = Length[Month];
if(Month == 2 && IsLeapYear(Year)) Days += 1;
return Days;
}
struct datetime
{
//NOTE: It is very important that SecondsSinceEpoch is the only data member of datetime, because a datetime is a member of the parameter_value union (mobius_model.h). Changing this may change the size of the parameter_value, which could break things.
s64 SecondsSinceEpoch;
datetime() : SecondsSinceEpoch(0) {}
datetime(const char *DateString, bool *Success)
{
//NOTE: Does not account for leap seconds, but that should not be a problem.
// Takes a string of the form "yyyy-mm-dd" and puts the number of seconds since "1970-01-01" in the SecondsSinceEpoch. Note that a negative value is computed for dates before "1970-01-01". Returns a bool saying if a correct date was provided.
s32 Day, Month, Year;
int Found = sscanf(DateString, "%d-%d-%d", &Year, &Month, &Day);
if(Found != 3)
{
*Success = false;
return;
}
if(Day < 1 || Day > 31 || Month < 1 || Month > 12) //TODO: Should we test this more thoroughly depending on the month?
{
*Success = false;
return;
}
s64 Result = 0;
if(Year > 1970)
{
for(s32 Y = 1970; Y < Year; ++Y)
{
Result += YearLength(Y)*24*60*60;
}
}
else if(Year < 1970)
{
for(s32 Y = 1969; Y >= Year; --Y)
{
Result -= YearLength(Y)*24*60*60;
}
}
Result += MonthOffset(Year, Month-1)*24*60*60;
Result += (Day-1)*24*60*60;
SecondsSinceEpoch = Result;
*Success = true;
}
inline void
DayOfYear(s32 *DayOut, s32 *YearOut)
{
//Computes the day of year (Starting at january 1st = day 1)
s32 Year = 1970;
s32 DayOfYear = 0;
s64 SecondsLeft = SecondsSinceEpoch;
if(SecondsLeft > 0)
{
while(true)
{
s64 SecondsThisYear = YearLength(Year)*24*60*60;
if(SecondsLeft >= SecondsThisYear)
{
Year++;
SecondsLeft -= SecondsThisYear;
}
else break;
}
DayOfYear = SecondsLeft / (24*60*60);
}
else if(SecondsLeft < 0)
{
SecondsLeft = -SecondsLeft;
Year = 1969;
s64 SecondsThisYear;
while(true)
{
SecondsThisYear = YearLength(Year)*24*60*60;
if(SecondsLeft > SecondsThisYear)
{
Year--;
SecondsLeft -= SecondsThisYear;
}
else break;
}
s64 DaysThisYear = 365 + IsLeapYear(Year);
SecondsThisYear = DaysThisYear*24*60*60;
DayOfYear = (SecondsThisYear - SecondsLeft) / (24*60*60);
}
*YearOut = Year;
*DayOut = DayOfYear + 1;
}
inline void
YearMonthDay(s32 *YearOut, s32 *MonthOut, s32 *DayOut)
{
//Computes the year, month and day (of month) for a seconds since epoch timestamp.
s32 Day;
DayOfYear(&Day, YearOut);
for(s32 Month = 0; Month < 12; ++Month)
{
if(Day <= MonthOffset(*YearOut, Month+1))
{
*MonthOut = (Month+1);
if(Month == 0) *DayOut = Day;
else *DayOut = (s32)Day - (s32)MonthOffset(*YearOut, Month);
break;
}
}
}
inline void
AdvanceDays(s32 NumberOfDays)
{
SecondsSinceEpoch += 24*60*60*NumberOfDays;
}
inline char *
ToString()
{
//Important: note that this one is overwritten whenever you call it. So you should make a copy of the string if you want to keep it.
s32 Year, Month, Day;
YearMonthDay(&Year, &Month, &Day);
static char Buf[32];
sprintf(Buf, "%04d-%02d-%02d", Year, Month, Day);
return Buf;
}
inline s32
DaysUntil(datetime DateTime)
{
s64 OtherSeconds = DateTime.SecondsSinceEpoch;
if(OtherSeconds >= SecondsSinceEpoch)
{
return (OtherSeconds - SecondsSinceEpoch) / (24*60*60);
}
else
{
return (OtherSeconds - SecondsSinceEpoch - 1) / (24*60*60);
}
}
private:
inline s32
MonthOffset(s32 Year, s32 Month)
{
//NOTE: Returns the number of the day of year (starting at january 1st = day 0) that this month starts on. The months are indexed from 0 in this context.
s32 Offset[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
s32 Days = Offset[Month];
if(Month >= 2 && IsLeapYear(Year)) Days += 1;
return Days;
}
};
#define DATETIME_H
#endif