forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpl_time.cpp
406 lines (359 loc) · 11.8 KB
/
cpl_time.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
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
/**********************************************************************
*
* Name: cpl_time.cpp
* Project: CPL - Common Portability Library
* Purpose: Time functions.
* Author: Even Rouault, <even dot rouault at spatialys.com>
*
**********************************************************************
*
* CPLUnixTimeToYMDHMS() is derived from timesub() in localtime.c from
* openbsd/freebsd/netbsd.
*
* CPLYMDHMSToUnixTime() has been implemented by Even Rouault and is in the
* public domain.
*
* c.f. http://svn.freebsd.org/viewvc/base/stable/7/lib/libc/stdtime/localtime.c?revision=178142&view=markup
* localtime.c comes with the following header :
*
* This file is in the public domain, so clarified as of
* 1996-06-05 by Arthur David Olson ([email protected]).
*/
#include "cpl_time.h"
#include "cpl_string.h"
#include <cstring>
#include <ctime>
#include "cpl_error.h"
CPL_CVSID("$Id$")
constexpr int SECSPERMIN = 60;
constexpr int MINSPERHOUR = 60;
constexpr int HOURSPERDAY = 24;
constexpr int SECSPERHOUR = SECSPERMIN * MINSPERHOUR;
constexpr int SECSPERDAY = SECSPERHOUR * HOURSPERDAY;
constexpr int DAYSPERWEEK = 7;
constexpr int MONSPERYEAR = 12;
constexpr int EPOCH_YEAR = 1970;
constexpr int EPOCH_WDAY = 4;
constexpr int TM_YEAR_BASE = 1900;
constexpr int DAYSPERNYEAR = 365;
constexpr int DAYSPERLYEAR = 366;
static bool isleap( int y )
{
return ((y % 4) == 0 && (y % 100) != 0) || (y % 400) == 0;
}
static int LEAPS_THROUGH_END_OF( int y )
{
return y / 4 - y / 100 + y / 400;
}
constexpr int mon_lengths[2][MONSPERYEAR] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
constexpr int year_lengths[2] = { DAYSPERNYEAR, DAYSPERLYEAR };
/************************************************************************/
/* CPLUnixTimeToYMDHMS() */
/************************************************************************/
/** Converts a time value since the Epoch (aka "unix" time) to a broken-down
* UTC time.
*
* This function is similar to gmtime_r().
* This function will always set tm_isdst to 0.
*
* @param unixTime number of seconds since the Epoch.
* @param pRet address of the return structure.
*
* @return the structure pointed by pRet filled with a broken-down UTC time.
*/
struct tm * CPLUnixTimeToYMDHMS(GIntBig unixTime, struct tm* pRet)
{
GIntBig days = unixTime / SECSPERDAY;
GIntBig rem = unixTime % SECSPERDAY;
constexpr GIntBig TEN_THOUSAND_YEARS =
static_cast<GIntBig>(10000) * SECSPERDAY * DAYSPERLYEAR;
if( unixTime < -TEN_THOUSAND_YEARS || unixTime > TEN_THOUSAND_YEARS )
{
CPLError(CE_Failure, CPLE_NotSupported,
"Invalid unixTime = " CPL_FRMT_GIB,
unixTime);
memset(pRet, 0, sizeof(*pRet));
return pRet;
}
while( rem < 0 )
{
rem += SECSPERDAY;
--days;
}
pRet->tm_hour = static_cast<int>( rem / SECSPERHOUR );
rem = rem % SECSPERHOUR;
pRet->tm_min = static_cast<int>( rem / SECSPERMIN );
/*
** A positive leap second requires a special
** representation. This uses "... ??:59:60" et seq.
*/
pRet->tm_sec = static_cast<int>( rem % SECSPERMIN );
pRet->tm_wday = static_cast<int>( (EPOCH_WDAY + days) % DAYSPERWEEK );
if( pRet->tm_wday < 0 )
pRet->tm_wday += DAYSPERWEEK;
int y = EPOCH_YEAR;
int yleap = 0;
int iters = 0;
while( iters < 1000 &&
(days < 0
|| days >= static_cast<GIntBig>( year_lengths[yleap = isleap(y)] )) )
{
int newy = y + static_cast<int>( days / DAYSPERNYEAR );
if( days < 0 )
--newy;
days -= (newy - y) * DAYSPERNYEAR +
LEAPS_THROUGH_END_OF(newy - 1) -
LEAPS_THROUGH_END_OF(y - 1);
y = newy;
iters ++;
}
if( iters == 1000 )
{
CPLError(CE_Failure, CPLE_NotSupported,
"Invalid unixTime = " CPL_FRMT_GIB,
unixTime);
memset(pRet, 0, sizeof(*pRet));
return pRet;
}
pRet->tm_year = static_cast<int>( y - TM_YEAR_BASE );
pRet->tm_yday = static_cast<int>( days );
const int* ip = mon_lengths[yleap];
for( pRet->tm_mon = 0;
days >= static_cast<GIntBig>( ip[pRet->tm_mon] );
++(pRet->tm_mon) )
days = days - static_cast<GIntBig>( ip[pRet->tm_mon] );
pRet->tm_mday = static_cast<int>( (days + 1) );
pRet->tm_isdst = 0;
return pRet;
}
/************************************************************************/
/* CPLYMDHMSToUnixTime() */
/************************************************************************/
/** Converts a broken-down UTC time into time since the Epoch (aka "unix" time).
*
* This function is similar to mktime(), but the passed structure is not
* modified. This function ignores the tm_wday, tm_yday and tm_isdst fields of
* the passed value. No timezone shift will be applied. This function
* returns 0 for the 1/1/1970 00:00:00
*
* @param brokendowntime broken-downtime UTC time.
*
* @return a number of seconds since the Epoch encoded as a value of type
* GIntBig, or -1 if the time cannot be represented.
*/
GIntBig CPLYMDHMSToUnixTime( const struct tm *brokendowntime )
{
if( brokendowntime->tm_mon < 0 || brokendowntime->tm_mon >= 12 )
return -1;
// Number of days of the current month.
GIntBig days = brokendowntime->tm_mday - 1;
// Add the number of days of the current year.
const int* ip =
mon_lengths[static_cast<int>(isleap(TM_YEAR_BASE +
brokendowntime->tm_year))];
for( int mon = 0; mon < brokendowntime->tm_mon; mon++ )
days += ip[mon];
// Add the number of days of the other years.
days +=
( TM_YEAR_BASE
+ static_cast<GIntBig>(brokendowntime->tm_year)
- EPOCH_YEAR ) * DAYSPERNYEAR
+ LEAPS_THROUGH_END_OF(static_cast<int>(TM_YEAR_BASE)
+ static_cast<int>( brokendowntime->tm_year)
- static_cast<int>(1))
- LEAPS_THROUGH_END_OF( EPOCH_YEAR - 1 );
// Now add the secondes, minutes and hours to the number of days
// since EPOCH.
return
brokendowntime->tm_sec +
brokendowntime->tm_min * SECSPERMIN +
brokendowntime->tm_hour * SECSPERHOUR +
days * SECSPERDAY;
}
/************************************************************************/
/* OGRParseRFC822DateTime() */
/************************************************************************/
static const char* const aszWeekDayStr[] = {
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
static const char* const aszMonthStr[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
/** Parse a RFC822 formatted date-time string.
*
* Such as [Fri,] 28 Dec 2007 05:24[:17] GMT
*
* @param pszRFC822DateTime formatted string.
* @param pnYear pointer to int receiving year (like 1980, 2000, etc...), or NULL
* @param pnMonth pointer to int receiving month (between 1 and 12), or NULL
* @param pnDay pointer to int receiving day of month (between 1 and 31), or NULL
* @param pnHour pointer to int receiving hour of day (between 0 and 23), or NULL
* @param pnMinute pointer to int receiving minute (between 0 and 59), or NULL
* @param pnSecond pointer to int receiving second (between 0 and 60, or -1 if unknown), or NULL
* @param pnTZFlag pointer to int receiving time zone flag (0=unknown, 100=GMT,
* 101=GMT+15minute, 99=GMT-15minute), or NULL
* @param pnWeekDay pointer to int receiving day of week (between 1 and 7, or 0 if invalid/unset), or NULL
* @return TRUE if parsing is successful
*
* @since GDAL 2.3
*/
int CPLParseRFC822DateTime( const char* pszRFC822DateTime,
int* pnYear,
int* pnMonth,
int* pnDay,
int* pnHour,
int* pnMinute,
int* pnSecond,
int* pnTZFlag,
int* pnWeekDay )
{
// Following
// https://www.w3.org/Protocols/rfc822/#z28 :
// [Fri,] 28 Dec 2007 05:24[:17] GMT
char** papszTokens =
CSLTokenizeStringComplex( pszRFC822DateTime, " ,:", TRUE, FALSE );
char** papszVal = papszTokens;
int nTokens = CSLCount(papszTokens);
if( nTokens < 5 )
{
CSLDestroy(papszTokens);
return false;
}
if( pnWeekDay )
*pnWeekDay = 0;
if( !((*papszVal)[0] >= '0' && (*papszVal)[0] <= '9') )
{
if( pnWeekDay )
{
for( size_t i = 0; i < CPL_ARRAYSIZE(aszWeekDayStr); ++i )
{
if( EQUAL(*papszVal, aszWeekDayStr[i]) )
{
*pnWeekDay = static_cast<int>(i+1);
break;
}
}
}
++papszVal;
}
int day = atoi(*papszVal);
if( day <= 0 || day >= 32 )
{
CSLDestroy(papszTokens);
return false;
}
if( pnDay )
*pnDay = day;
++papszVal;
int month = 0;
for( int i = 0; i < 12; ++i )
{
if( EQUAL(*papszVal, aszMonthStr[i]) )
{
month = i + 1;
break;
}
}
if( month == 0 )
{
CSLDestroy(papszTokens);
return false;
}
if( pnMonth )
*pnMonth = month;
++papszVal;
int year = atoi(*papszVal);
if( year < 100 && year >= 30 )
year += 1900;
else if( year < 30 && year >= 0 )
year += 2000;
if( pnYear )
*pnYear = year;
++papszVal;
int hour = atoi(*papszVal);
if( hour < 0 || hour >= 24 )
{
CSLDestroy(papszTokens);
return false;
}
if( pnHour )
*pnHour = hour;
++papszVal;
if( *papszVal == nullptr )
{
CSLDestroy(papszTokens);
return false;
}
int minute = atoi(*papszVal);
if( minute < 0 || minute >= 60 )
{
CSLDestroy(papszTokens);
return false;
}
if (pnMinute )
*pnMinute = minute;
++papszVal;
if( *papszVal != nullptr && (*papszVal)[0] >= '0' && (*papszVal)[0] <= '9' )
{
int second = atoi(*papszVal);
if( second < 0 || second >= 61 )
{
CSLDestroy(papszTokens);
return false;
}
if( pnSecond )
*pnSecond = second;
++papszVal;
}
else if( pnSecond )
*pnSecond = -1;
int TZ = 0;
if( *papszVal == nullptr )
{
}
else if( strlen(*papszVal) == 5 &&
((*papszVal)[0] == '+' || (*papszVal)[0] == '-') )
{
char szBuf[3] = { (*papszVal)[1], (*papszVal)[2], 0 };
const int TZHour = atoi(szBuf);
if( TZHour < 0 || TZHour >= 15 )
{
CSLDestroy(papszTokens);
return false;
}
szBuf[0] = (*papszVal)[3];
szBuf[1] = (*papszVal)[4];
szBuf[2] = 0;
const int TZMinute = atoi(szBuf);
TZ = 100 + (((*papszVal)[0] == '+') ? 1 : -1) *
((TZHour * 60 + TZMinute) / 15);
}
else
{
const char* aszTZStr[] = {
"GMT", "UT", "Z", "EST", "EDT", "CST", "CDT", "MST", "MDT",
"PST", "PDT"
};
const int anTZVal[] = { 0, 0, 0, -5, -4, -6, -5, -7, -6, -8, -7 };
TZ = -1;
for( int i = 0; i < 11; ++i )
{
if( EQUAL(*papszVal, aszTZStr[i]) )
{
TZ = 100 + anTZVal[i] * 4;
break;
}
}
if( TZ < 0 )
{
CSLDestroy(papszTokens);
return false;
}
}
if( pnTZFlag )
*pnTZFlag = TZ;
CSLDestroy(papszTokens);
return true;
}