Overview
Comment: | OFDate: Add support for parsing time zones
This adds %z for parsing time zones. Additionally, this also adds %a and |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
e4439b7ef8285617a481fc321c13da30 |
User & Date: | js on 2016-09-11 22:03:20 |
Other Links: | manifest | tags |
Context
2016-09-11
| ||
22:23 | OFDate: Throw on trailing garbage check-in: 804f222e18 user: js tags: trunk | |
22:03 | OFDate: Add support for parsing time zones check-in: e4439b7ef8 user: js tags: trunk | |
2016-09-07
| ||
21:15 | OFDataArray: Fix a missing underscore check-in: 3519c1e727 user: js tags: trunk | |
Changes
Modified src/OFDate.h from [3a586242e7] to [7fa1e30f2a].
︙ | ︙ | |||
62 63 64 65 66 67 68 | * * The time zone used is UTC. See @ref dateWithLocalDateString:format: if you * want local time. * * See the manpage for strftime for information on the format. * * @warning The format is currently limited to the following format specifiers: | | > | > | 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 | * * The time zone used is UTC. See @ref dateWithLocalDateString:format: if you * want local time. * * See the manpage for strftime for information on the format. * * @warning The format is currently limited to the following format specifiers: * %%a, %%b, %%d, %%e, %%H, %%m, %%M, %%S, %%y, %%Y, %%z, %%, %%n and * %%t. * * @param string The string describing the date * @param format The format of the string describing the date * @return A new, autoreleased OFDate with the specified date and time */ + (instancetype)dateWithDateString: (OFString*)string format: (OFString*)format; /*! * @brief Creates a new OFDate with the specified string in the specified * format. * * See the manpage for strftime for information on the format. * * @warning The format is currently limited to the following format specifiers: * %%a, %%b, %%d, %%e, %%H, %%m, %%M, %%S, %%y, %%Y, %%z, %%, %%n and * %%t. * * @param string The string describing the date * @param format The format of the string describing the date * @return A new, autoreleased OFDate with the specified date and time */ + (instancetype)dateWithLocalDateString: (OFString*)string format: (OFString*)format; |
︙ | ︙ |
Modified src/OFDate.m from [7b98bd3de0] to [6c84c9ff36].
︙ | ︙ | |||
140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 31 + 28 + 31 + 30 + 31 + 30, 31 + 28 + 31 + 30 + 31 + 30 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, }; @implementation OFDate #if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \ defined(OF_HAVE_THREADS) + (void)initialize { if (self == [OFDate class]) | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | 31 + 28 + 31 + 30 + 31 + 30, 31 + 28 + 31 + 30 + 31 + 30 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, }; static double tmAndTzToTime(struct tm *tm, int16_t *tz) { double seconds; /* Years */ seconds = (int64_t)(tm->tm_year - 70) * 31536000; /* Days of leap years, excluding the year to look at */ seconds += (((tm->tm_year + 1899) / 4) - 492) * 86400; seconds -= (((tm->tm_year + 1899) / 100) - 19) * 86400; seconds += (((tm->tm_year + 1899) / 400) - 4) * 86400; /* Leap day */ if (tm->tm_mon >= 2 && (((tm->tm_year + 1900) % 4 == 0 && (tm->tm_year + 1900) % 100 != 0) || (tm->tm_year + 1900) % 400 == 0)) seconds += 86400; /* Months */ if (tm->tm_mon < 0 || tm->tm_mon > 12) @throw [OFInvalidFormatException exception]; seconds += monthToDayOfYear[tm->tm_mon] * 86400; /* Days */ seconds += (tm->tm_mday - 1) * 86400; /* Hours */ seconds += tm->tm_hour * 3600; /* Minutes */ seconds += tm->tm_min * 60; /* Seconds */ seconds += tm->tm_sec; /* Time zone */ seconds += -(float)*tz * 60; return seconds; } @implementation OFDate #if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \ defined(OF_HAVE_THREADS) + (void)initialize { if (self == [OFDate class]) |
︙ | ︙ | |||
233 234 235 236 237 238 239 240 241 242 243 | - initWithDateString: (OFString*)string format: (OFString*)format { self = [super init]; @try { struct tm tm = { 0 }; tm.tm_isdst = -1; if (of_strptime([string UTF8String], [format UTF8String], | > | < < < < < < < < < < | < < < < < < < < < < < < > > > > > > | > | | | | > > | 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 | - initWithDateString: (OFString*)string format: (OFString*)format { self = [super init]; @try { struct tm tm = { 0 }; int16_t tz = 0; tm.tm_isdst = -1; if (of_strptime([string UTF8String], [format UTF8String], &tm, &tz) == NULL) @throw [OFInvalidFormatException exception]; _seconds = tmAndTzToTime(&tm, &tz); } @catch (id e) { [self release]; @throw e; } return self; } - initWithLocalDateString: (OFString*)string format: (OFString*)format { self = [super init]; @try { struct tm tm = { 0 }; /* * of_strptime() can never set this to INT16_MAX, no matter * what is passed to it, so this is a safe way to figure out if * the date contains a time zone. */ int16_t tz = INT16_MAX; tm.tm_isdst = -1; if (of_strptime([string UTF8String], [format UTF8String], &tm, &tz) == NULL) @throw [OFInvalidFormatException exception]; if (tz == INT16_MAX) { #ifndef OF_WINDOWS if ((_seconds = mktime(&tm)) == -1) @throw [OFInvalidFormatException exception]; #else if ((_seconds = _mktime64(&tm)) == -1) @throw [OFInvalidFormatException exception]; #endif } else _seconds = tmAndTzToTime(&tm, &tz); } @catch (id e) { [self release]; @throw e; } return self; } |
︙ | ︙ |
Modified src/of_strptime.h from [1101b728c5] to [3bd162fcec].
︙ | ︙ | |||
26 27 28 29 30 31 32 | #import "macros.h" OF_ASSUME_NONNULL_BEGIN #ifdef __cplusplus extern "C" { #endif | | > | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #import "macros.h" OF_ASSUME_NONNULL_BEGIN #ifdef __cplusplus extern "C" { #endif extern const char* of_strptime(const char*, const char*, struct tm *tm, int16_t *tz); #ifdef __cplusplus } #endif OF_ASSUME_NONNULL_END |
Modified src/of_strptime.m from [e62baac3c8] to [42f8137e1c].
︙ | ︙ | |||
19 20 21 22 23 24 25 | #include <string.h> #include <time.h> #include "macros.h" const char* | | | | | | | | | | > > > | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | #include <string.h> #include <time.h> #include "macros.h" const char* of_strptime(const char *buffer, const char *format, struct tm *tm, int16_t *tz) { enum { SEARCH_CONVERSION_SPECIFIER, IN_CONVERSION_SPECIFIER } state = SEARCH_CONVERSION_SPECIFIER; size_t j, bufferLen, formatLen; bufferLen = strlen(buffer); formatLen = strlen(format); j = 0; for (size_t i = 0; i < formatLen; i++) { if (j >= bufferLen) return NULL; switch (state) { case SEARCH_CONVERSION_SPECIFIER: if (format[i] == '%') state = IN_CONVERSION_SPECIFIER; else if (format[i] != buffer[j++]) return NULL; break; case IN_CONVERSION_SPECIFIER:; int k, maxLen, number = 0; switch (format[i]) { case 'd': case 'e': case 'H': case 'm': case 'M': case 'S': case 'y': maxLen = 2; break; case 'Y': maxLen = 4; break; case '%': case 'a': case 'b': case 'n': case 't': case 'z': maxLen = 0; break; default: return NULL; } if (maxLen > 0 && (buffer[j] < '0' || buffer[j] > '9')) return NULL; for (k = 0; k < maxLen && j < bufferLen && buffer[j] >= '0' && buffer[j] <= '9'; k++, j++) { number *= 10; number += buffer[j] - '0'; } switch (format[i]) { case 'a': if (bufferLen < j + 3) return NULL; if (memcmp(buffer + j, "Sun", 3) == 0) tm->tm_wday = 0; else if (memcmp(buffer + j, "Mon", 3) == 0) tm->tm_wday = 1; else if (memcmp(buffer + j, "Tue", 3) == 0) tm->tm_wday = 2; else if (memcmp(buffer + j, "Wed", 3) == 0) tm->tm_wday = 3; else if (memcmp(buffer + j, "Thu", 3) == 0) tm->tm_wday = 4; else if (memcmp(buffer + j, "Fri", 3) == 0) tm->tm_wday = 5; else if (memcmp(buffer + j, "Sat", 3) == 0) tm->tm_wday = 6; else return NULL; j += 3; break; case 'b': if (bufferLen < j + 3) return NULL; if (memcmp(buffer + j, "Jan", 3) == 0) tm->tm_mon = 1; else if (memcmp(buffer + j, "Feb", 3) == 0) tm->tm_mon = 2; else if (memcmp(buffer + j, "Mar", 3) == 0) tm->tm_mon = 3; else if (memcmp(buffer + j, "Apr", 3) == 0) tm->tm_mon = 4; else if (memcmp(buffer + j, "May", 3) == 0) tm->tm_mon = 5; else if (memcmp(buffer + j, "Jun", 3) == 0) tm->tm_mon = 6; else if (memcmp(buffer + j, "Jul", 3) == 0) tm->tm_mon = 7; else if (memcmp(buffer + j, "Aug", 3) == 0) tm->tm_mon = 8; else if (memcmp(buffer + j, "Sep", 3) == 0) tm->tm_mon = 9; else if (memcmp(buffer + j, "Oct", 3) == 0) tm->tm_mon = 10; else if (memcmp(buffer + j, "Nov", 3) == 0) tm->tm_mon = 11; else if (memcmp(buffer + j, "Dec", 3) == 0) tm->tm_mon = 12; else return NULL; j += 3; break; case 'd': case 'e': tm->tm_mday = number; break; case 'H': tm->tm_hour = number; break; |
︙ | ︙ | |||
106 107 108 109 110 111 112 113 114 115 116 117 118 119 | tm->tm_year = number; break; case 'Y': if (number < 1900) return NULL; tm->tm_year = number - 1900; break; case '%': if (buffer[j++] != '%') return NULL; break; case 'n': case 't': | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | tm->tm_year = number; break; case 'Y': if (number < 1900) return NULL; tm->tm_year = number - 1900; break; case 'z': if (buffer[j] == '-' || buffer[j] == '+') { const char *b = buffer + j; if (bufferLen < j + 5) return NULL; if (tz == NULL) break; *tz = (((int16_t)b[1] - '0') * 600 + ((int16_t)b[2] - '0') * 60 + ((int16_t)b[3] - '0') * 10 + ((int16_t)b[4] - '0')) * (b[0] == '-' ? -1 : 1); j += 5; } else if (buffer[j] == 'Z') { if (tz != NULL) *tz = 0; j++; } else if (buffer[j] == 'G') { if (bufferLen < j + 3) return NULL; if (buffer[j + 1] != 'M' || buffer[j + 2] != 'T') return NULL; if (tz != NULL) *tz = 0; j += 3; } else return NULL; break; case '%': if (buffer[j++] != '%') return NULL; break; case 'n': case 't': |
︙ | ︙ |
Modified tests/OFDateTests.m from [2f507797b6] to [ab5193ad47].
︙ | ︙ | |||
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 | * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFDate.h" #import "OFString.h" #import "OFAutoreleasePool.h" #import "TestsAppDelegate.h" static OFString *module = @"OFDate"; @implementation TestsAppDelegate (OFDateTests) - (void)dateTests { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFDate *d1, *d2; TEST(@"+[dateWithTimeIntervalSince1970:]", (d1 = [OFDate dateWithTimeIntervalSince1970: 0])) TEST(@"-[dateByAddingTimeInterval:]", (d2 = [d1 dateByAddingTimeInterval: 3600 * 25 + 5.000002])) TEST(@"-[description]", [[d1 description] isEqual: @"1970-01-01T00:00:00Z"] && [[d2 description] isEqual: @"1970-01-02T01:00:05Z"]) TEST(@"+[dateWithDateString:format:]", | > > > > > > > > > > > > | | | > > > > > > > > > | 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 | * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #include <time.h> #import "OFDate.h" #import "OFString.h" #import "OFAutoreleasePool.h" #import "of_strptime.h" #import "TestsAppDelegate.h" static OFString *module = @"OFDate"; @implementation TestsAppDelegate (OFDateTests) - (void)dateTests { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFDate *d1, *d2; struct tm tm; int16_t tz; const char *dstr = "Wed, 09 Jun 2021 +0200x"; TEST(@"of_strptime()", of_strptime(dstr, "%a, %d %b %Y %z", &tm, &tz) == dstr + 22 && tm.tm_wday == 3 && tm.tm_mday == 9 && tm.tm_mon == 6 && tm.tm_year == 2021 - 1900 && tz == 2 * 60) TEST(@"+[dateWithTimeIntervalSince1970:]", (d1 = [OFDate dateWithTimeIntervalSince1970: 0])) TEST(@"-[dateByAddingTimeInterval:]", (d2 = [d1 dateByAddingTimeInterval: 3600 * 25 + 5.000002])) TEST(@"-[description]", [[d1 description] isEqual: @"1970-01-01T00:00:00Z"] && [[d2 description] isEqual: @"1970-01-02T01:00:05Z"]) TEST(@"+[dateWithDateString:format:]", [[[OFDate dateWithDateString: @"2000-06-20T12:34:56+0200" format: @"%Y-%m-%dT%H:%M:%S%z"] description] isEqual: @"2000-06-20T10:34:56Z"]); /* * We can only test local dates that specify a time zone, as the local * time zone differs between systems. */ TEST(@"+[dateWithLocalDateString:format:]", [[[OFDate dateWithLocalDateString: @"2000-06-20T12:34:56-0200" format: @"%Y-%m-%dT%H:%M:%S%z"] description] isEqual: @"2000-06-20T14:34:56Z"]); TEST(@"-[isEqual:]", [d1 isEqual: [OFDate dateWithTimeIntervalSince1970: 0]] && ![d1 isEqual: [OFDate dateWithTimeIntervalSince1970: 0.0000001]]) TEST(@"-[compare:]", [d1 compare: d2] == OF_ORDERED_ASCENDING) |
︙ | ︙ |