-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtimestamp.inc
More file actions
293 lines (245 loc) · 8.15 KB
/
timestamp.inc
File metadata and controls
293 lines (245 loc) · 8.15 KB
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
// Timestamp.inc by Gammix
// Version: 2.0 - 29 Jan, 2019 - 9:14:32 PM
// Special thanks: Crayder (his Time Conversion library was used as a reference)
#if defined _inc_timestamp_
#endinput
#endif
#define _inc_timestamp_
#define EPOCH_YEAR 1970
#define MAX_TIMESTAMP_FORMAT_LENGTH 128
#if defined _inc_timestamp_natives_
native Timestamp(year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, hourGMT = 0, minuteGMT = 0);
native ParseTimestamp(timestamp, &year = 0, &month = 0, &day = 0, &hour = 0, &minute = 0, &second = 0, hourGMT = 0, minuteGMT = 0);
native FormatTimestamp(timestamp, const format[], hourGMT = 0, minuteGMT = 0);
native FormatTimeleft(startTimestamp, endTimestamp);
native ConvertFromSeconds(TimeUnit:type, seconds);
native ConvertToSeconds(TimeUnit:type, value);
#endif
enum TimeUnit {
Year,
Month,
Day,
Hour,
Minute
};
static const MONTH_DAYS[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static const MONTH_NAMES[12][] = {
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
};
static stock bool:IsLeapYear(year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
static stock GetMonthDays(month, year) {
if (month == 1 && IsLeapYear(year) == true) {
return 29;
}
return MONTH_DAYS[month - 1];
}
static stock ReplaceString(string[], start, end, const replacement[], size) {
strdel(string, start, end);
strins(string, replacement, start, size);
}
stock Timestamp(year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, hourGMT = 0, minuteGMT = 0) {
static const
SECONDS_PER_MINUTE = 60,
SECONDS_PER_HOUR = 3600,
SECONDS_PER_DAY = 86400;
new timestamp = 0;
if (year == 0 || month == 0 || day == 0) {
getdate(year, month, day);
gettime(hour, minute, second);
}
for (new i = EPOCH_YEAR; i < year; i++)
day += (IsLeapYear(i) == true) ? 365 : 366;
for (new i = 1; i < month; i++)
day += MONTH_DAYS[i - 1];
if (IsLeapYear(year) == true && month > 2)
day += 1;
timestamp += ((day - 1) * SECONDS_PER_DAY);
timestamp += ((hour + hourGMT) * SECONDS_PER_HOUR);
timestamp += ((minute + minuteGMT) * SECONDS_PER_MINUTE);
timestamp += second;
return timestamp;
}
stock ParseTimestamp(timestamp, &year = 0, &month = 0, &day = 0, &hour = 0, &minute = 0, &second = 0, hourGMT = 0, minuteGMT = 0) {
static const
SECONDS_PER_MINUTE = 60,
SECONDS_PER_HOUR = 3600,
SECONDS_PER_DAY = 86400;
new days = 0, seconds = 0;
for (year = EPOCH_YEAR; ; year++) {
days = (IsLeapYear(year) == true) ? 365 : 366;
seconds = days * SECONDS_PER_DAY;
if (seconds > timestamp)
break;
timestamp -= seconds;
}
for (month = 1; ; month++) {
days = MONTH_DAYS[month - 1];
seconds = days * SECONDS_PER_DAY;
if (seconds > timestamp)
break;
timestamp -= seconds;
}
for (day = 1; timestamp >= SECONDS_PER_DAY; day++)
timestamp -= SECONDS_PER_DAY;
for (hour = hourGMT; timestamp >= SECONDS_PER_HOUR; hour++)
timestamp -= SECONDS_PER_HOUR;
for (minute = minuteGMT; timestamp >= SECONDS_PER_MINUTE; minute++)
timestamp -= SECONDS_PER_MINUTE;
second = timestamp;
}
stock FormatTimestamp(timestamp, const format[], hourGMT = 0, minuteGMT = 0) {
new string[MAX_TIMESTAMP_FORMAT_LENGTH];
strcat(string, format);
new year, month, day, hour, minute, second;
ParseTimestamp(timestamp, year, month, day, hour, minute, second, hourGMT, minuteGMT);
new pos = -1;
do {
// year abbreviated (last 2 chars)
if ((pos = strfind(string, "%y")) != -1) {
new replacement[4];
valstr(replacement, year % 100);
ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
}
// full year
else if ((pos = strfind(string, "%Y")) != -1) {
new replacement[5];
valstr(replacement, year);
ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
}
// month number
else if ((pos = strfind(string, "%m")) != -1) {
new replacement[4];
valstr(replacement, month);
ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
}
// month name abbreviated
else if ((pos = strfind(string, "%b")) != -1) {
new replacement[4];
strcat(replacement, MONTH_NAMES[month - 1]);
ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
}
// full month name
else if ((pos = strfind(string, "%B")) != -1) {
ReplaceString(string, pos, pos + 2, MONTH_NAMES[month - 1], MAX_TIMESTAMP_FORMAT_LENGTH);
}
// day
else if ((pos = strfind(string, "%d")) != -1) {
new replacement[4];
valstr(replacement, day);
ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
}
// hour 24H
else if ((pos = strfind(string, "%H")) != -1) {
new replacement[4];
valstr(replacement, hour);
ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
}
// hour 12H
else if ((pos = strfind(string, "%I")) != -1) {
new replacement[4];
valstr(replacement, (hour > 12) ? (hour - 12) : (hour));
ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
}
// hour 12H AM or PM
else if ((pos = strfind(string, "%p")) != -1) {
ReplaceString(string, pos, pos + 2, (hour > 12) ? ("PM") : ("AM"), MAX_TIMESTAMP_FORMAT_LENGTH);
}
// minute
else if ((pos = strfind(string, "%M")) != -1) {
new replacement[4];
valstr(replacement, minute);
ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
}
// second
else if ((pos = strfind(string, "%S")) != -1) {
new replacement[4];
valstr(replacement, second);
ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
}
} while (pos != -1);
return string;
}
stock FormatTimeleft(startTimestamp, endTimestamp) {
static const
SECONDS_PER_MINUTE = 60,
SECONDS_PER_HOUR = 3600,
SECONDS_PER_DAY = 86400,
SECONDS_PER_MONTH = 2592000;
new string[MAX_TIMESTAMP_FORMAT_LENGTH];
new seconds = endTimestamp - startTimestamp;
if (seconds == 1)
format(string, sizeof(string), "a second");
else if (seconds < SECONDS_PER_MINUTE)
format(string, sizeof(string), "%i seconds", seconds);
else if (seconds < (2 * SECONDS_PER_MINUTE))
format(string, sizeof(string), "a minute");
else if (seconds < (45 * SECONDS_PER_MINUTE))
format(string, sizeof(string), "%i minutes", (seconds / SECONDS_PER_MINUTE));
else if (seconds < (90 * SECONDS_PER_MINUTE))
format(string, sizeof(string), "an hour");
else if (seconds < (24 * SECONDS_PER_HOUR))
format(string, sizeof(string), "%i hours", (seconds / SECONDS_PER_HOUR));
else if (seconds < (48 * SECONDS_PER_HOUR))
format(string, sizeof(string), "a day");
else if (seconds < (30 * SECONDS_PER_DAY))
format(string, sizeof(string), "%i days", (seconds / SECONDS_PER_DAY));
else if (seconds < (12 * SECONDS_PER_MONTH)) {
new months = floatround(seconds / SECONDS_PER_DAY / 30);
if (months <= 1)
format(string, sizeof(string), "a month");
else
format(string, sizeof(string), "%i months", months);
}
else {
new years = floatround(seconds / SECONDS_PER_DAY / 365);
if (years <= 1)
format(string, sizeof(string), "a year");
else
format(string, sizeof(string), "%i years", years);
}
return string;
}
stock ConvertFromSeconds(TimeUnit:type, seconds) {
static const
SECONDS_PER_MINUTE = 60,
SECONDS_PER_HOUR = 3600,
SECONDS_PER_DAY = 86400,
SECONDS_PER_YEAR = 31556952;
switch (type) {
case Year:
return seconds / SECONDS_PER_YEAR;
case Month:
return seconds / (SECONDS_PER_DAY * 31);
case Day:
return seconds / SECONDS_PER_DAY;
case Hour:
return seconds / SECONDS_PER_HOUR;
case Minute:
return seconds / SECONDS_PER_MINUTE;
}
return 0;
}
stock ConvertToSeconds(TimeUnit:type, value) {
static const
SECONDS_PER_MINUTE = 60,
SECONDS_PER_HOUR = 3600,
SECONDS_PER_DAY = 86400,
SECONDS_PER_YEAR = 31556952;
switch (type) {
case Year:
return SECONDS_PER_YEAR * value;
case Month:
return SECONDS_PER_DAY * (31 * value);
case Day:
return SECONDS_PER_DAY * value;
case Hour:
return SECONDS_PER_HOUR * value;
case Minute:
return SECONDS_PER_MINUTE * value;
}
return 0;
}