Differences From Artifact [1c4647d564]:
- File src/OFDate.m — part of check-in [bce1118807] at 2020-05-16 22:00:56 on branch trunk — OFDate: Don't declare _mktime64() with WINAPI (user: js, size: 15972) [annotate] [blame] [check-ins using] [more...]
To Artifact [7bc28080b9]:
- File
src/OFDate.m
— part of check-in
[33a133cda0]
at
2020-07-25 14:17:14
on branch tagged-pointers
— OFDate: Add a designated initializer
This means all other initializers use this initializer, which means all
other initializers can be reused for tagged pointers. (user: js, size: 18234) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
10 11 12 13 14 15 16 17 18 19 20 21 22 23 | * the packaging of this file. * * 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 <limits.h> #include <time.h> #include <math.h> | > > | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | * the packaging of this file. * * 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. */ #define OF_DATE_M #include "config.h" #include <limits.h> #include <time.h> #include <math.h> |
︙ | ︙ | |||
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #import "of_strptime.h" #ifdef OF_AMIGAOS_M68K /* amiga-gcc does not have trunc() */ # define trunc(x) ((int64_t)(x)) #endif #if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \ defined(OF_HAVE_THREADS) static OFMutex *mutex; #endif #ifdef OF_WINDOWS static __time64_t (*func__mktime64)(struct tm *); #endif #ifdef HAVE_GMTIME_R # define GMTIME_RET(field) \ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | > | | > | | > | | > | | > | | | 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 | #import "of_strptime.h" #ifdef OF_AMIGAOS_M68K /* amiga-gcc does not have trunc() */ # define trunc(x) ((int64_t)(x)) #endif @interface OFDate () + (instancetype)of_alloc; @end @interface OFDateSingleton: OFDate @end @interface OFDatePlaceholder: OFDateSingleton @end static struct { Class isa; } placeholder; static OFDateSingleton *distantFuture, *distantPast; static void initDistantFuture(void) { distantFuture = [[OFDateSingleton alloc] initWithTimeIntervalSince1970: 64060588800.0]; } static void initDistantPast(void) { distantPast = [[OFDateSingleton alloc] initWithTimeIntervalSince1970: -62167219200.0]; } static of_time_interval_t now(void) { struct timeval tv; of_time_interval_t seconds; OF_ENSURE(gettimeofday(&tv, NULL) == 0); seconds = tv.tv_sec; seconds += (of_time_interval_t)tv.tv_usec / 1000000; return seconds; } #if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \ defined(OF_HAVE_THREADS) static OFMutex *mutex; #endif #ifdef OF_WINDOWS static __time64_t (*func__mktime64)(struct tm *); #endif #ifdef HAVE_GMTIME_R # define GMTIME_RET(field) \ of_time_interval_t timeInterval = self.timeIntervalSince1970; \ time_t seconds = (time_t)timeInterval; \ struct tm tm; \ \ if (seconds != trunc(timeInterval)) \ @throw [OFOutOfRangeException exception]; \ \ if (gmtime_r(&seconds, &tm) == NULL) \ @throw [OFOutOfRangeException exception]; \ \ return tm.field; # define LOCALTIME_RET(field) \ of_time_interval_t timeInterval = self.timeIntervalSince1970; \ time_t seconds = (time_t)timeInterval; \ struct tm tm; \ \ if (seconds != trunc(timeInterval)) \ @throw [OFOutOfRangeException exception]; \ \ if (localtime_r(&seconds, &tm) == NULL) \ @throw [OFOutOfRangeException exception]; \ \ return tm.field; #else # ifdef OF_HAVE_THREADS # define GMTIME_RET(field) \ of_time_interval_t timeInterval = self.timeIntervalSince1970; \ time_t seconds = (time_t)timeInterval; \ struct tm *tm; \ \ if (seconds != trunc(timeInterval)) \ @throw [OFOutOfRangeException exception]; \ \ [mutex lock]; \ \ @try { \ if ((tm = gmtime(&seconds)) == NULL) \ @throw [OFOutOfRangeException exception]; \ \ return tm->field; \ } @finally { \ [mutex unlock]; \ } # define LOCALTIME_RET(field) \ of_time_interval_t timeInterval = self.timeIntervalSince1970; \ time_t seconds = (time_t)timeInterval; \ struct tm *tm; \ \ if (seconds != trunc(timeInterval)) \ @throw [OFOutOfRangeException exception]; \ \ [mutex lock]; \ \ @try { \ if ((tm = localtime(&seconds)) == NULL) \ @throw [OFOutOfRangeException exception]; \ \ return tm->field; \ } @finally { \ [mutex unlock]; \ } # else # define GMTIME_RET(field) \ of_time_interval_t timeInterval = self.timeIntervalSince1970; \ time_t seconds = (time_t)timeInterval; \ struct tm *tm; \ \ if (seconds != trunc(timeInterval)) \ @throw [OFOutOfRangeException exception]; \ \ if ((tm = gmtime(&seconds)) == NULL) \ @throw [OFOutOfRangeException exception]; \ \ return tm->field; # define LOCALTIME_RET(field) \ of_time_interval_t timeInterval = self.timeIntervalSince1970; \ time_t seconds = (time_t)timeInterval; \ struct tm *tm; \ \ if (seconds != trunc(timeInterval)) \ @throw [OFOutOfRangeException exception]; \ \ if ((tm = localtime(&seconds)) == NULL) \ @throw [OFOutOfRangeException exception]; \ \ return tm->field; # endif |
︙ | ︙ | |||
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 | /* Seconds */ seconds += tm->tm_sec; /* Time zone */ seconds += -(double)*tz * 60; return seconds; } @implementation OFDate + (void)initialize { #ifdef OF_WINDOWS HMODULE module; #endif if (self != [OFDate class]) return; #if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \ defined(OF_HAVE_THREADS) mutex = [[OFMutex alloc] init]; #endif #ifdef OF_WINDOWS if ((module = LoadLibrary("msvcrt.dll")) != NULL) func__mktime64 = (__time64_t (*)(struct tm *)) GetProcAddress(module, "_mktime64"); #endif } + (instancetype)date { return [[[self alloc] init] autorelease]; } + (instancetype)dateWithTimeIntervalSince1970: (of_time_interval_t)seconds | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | /* Seconds */ seconds += tm->tm_sec; /* Time zone */ seconds += -(double)*tz * 60; return seconds; } @implementation OFDateSingleton - (instancetype)autorelease { return self; } - (instancetype)retain { return self; } - (void)release { } - (unsigned int)retainCount { return OF_RETAIN_COUNT_MAX; } @end @implementation OFDatePlaceholder #ifdef __clang__ /* We intentionally don't call into super, so silence the warning. */ # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wobjc-designated-initializers" #endif - (instancetype)initWithTimeIntervalSince1970: (of_time_interval_t)seconds { return (id)[[OFDate of_alloc] initWithTimeIntervalSince1970: seconds]; } #ifdef __clang__ # pragma clang diagnostic pop #endif @end @implementation OFDate + (void)initialize { #ifdef OF_WINDOWS HMODULE module; #endif if (self != [OFDate class]) return; placeholder.isa = [OFDatePlaceholder class]; #if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \ defined(OF_HAVE_THREADS) mutex = [[OFMutex alloc] init]; #endif #ifdef OF_WINDOWS if ((module = LoadLibrary("msvcrt.dll")) != NULL) func__mktime64 = (__time64_t (*)(struct tm *)) GetProcAddress(module, "_mktime64"); #endif } + (instancetype)of_alloc { return [super alloc]; } + (instancetype)alloc { if (self == [OFDate class]) return (id)&placeholder; return [super alloc]; } + (instancetype)date { return [[[self alloc] init] autorelease]; } + (instancetype)dateWithTimeIntervalSince1970: (of_time_interval_t)seconds |
︙ | ︙ | |||
240 241 242 243 244 245 246 | { return [[[self alloc] initWithLocalDateString: string format: format] autorelease]; } + (instancetype)distantFuture { | > > | < > > | < < | < < < < < < < < < | < < < < < < | | | < < | | | | < < < < < < < < < | | | | | | | | | < | | | | | | | < | | | < | | | < < < | | < < < < | > | | | | | | < < < | | < | | | 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | { return [[[self alloc] initWithLocalDateString: string format: format] autorelease]; } + (instancetype)distantFuture { static of_once_t once = OF_ONCE_INIT; of_once(&once, initDistantFuture); return distantFuture; } + (instancetype)distantPast { static of_once_t once = OF_ONCE_INIT; of_once(&once, initDistantPast); return distantPast; } - (instancetype)init { return [self initWithTimeIntervalSince1970: now()]; } - (instancetype)initWithTimeIntervalSince1970: (of_time_interval_t)seconds { self = [super init]; _seconds = seconds; return self; } - (instancetype)initWithTimeIntervalSinceNow: (of_time_interval_t)seconds { return [self initWithTimeIntervalSince1970: now() + seconds]; } - (instancetype)initWithDateString: (OFString *)string format: (OFString *)format { const char *UTF8String = string.UTF8String; struct tm tm = { .tm_isdst = -1 }; int16_t tz = 0; if (of_strptime(UTF8String, format.UTF8String, &tm, &tz) != UTF8String + string.UTF8StringLength) @throw [OFInvalidFormatException exception]; return [self initWithTimeIntervalSince1970: tmAndTzToTime(&tm, &tz)]; } - (instancetype)initWithLocalDateString: (OFString *)string format: (OFString *)format { const char *UTF8String = string.UTF8String; struct tm tm = { .tm_isdst = -1 }; /* * 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; of_time_interval_t seconds; if (of_strptime(UTF8String, format.UTF8String, &tm, &tz) != UTF8String + string.UTF8StringLength) @throw [OFInvalidFormatException exception]; if (tz == INT16_MAX) { #ifdef OF_WINDOWS if (func__mktime64 != NULL) { if ((seconds = func__mktime64(&tm)) == -1) @throw [OFInvalidFormatException exception]; } else { #endif if ((seconds = mktime(&tm)) == -1) @throw [OFInvalidFormatException exception]; #ifdef OF_WINDOWS } #endif } else seconds = tmAndTzToTime(&tm, &tz); return [self initWithTimeIntervalSince1970: seconds]; } - (instancetype)initWithSerialization: (OFXMLElement *)element { void *pool = objc_autoreleasePoolPush(); of_time_interval_t seconds; if (![element.name isEqual: @"OFDate"] || ![element.namespace isEqual: OF_SERIALIZATION_NS]) @throw [OFInvalidArgumentException exception]; seconds = OF_BSWAP_DOUBLE_IF_LE(OF_INT_TO_DOUBLE_RAW(OF_BSWAP64_IF_LE( element.hexadecimalValue))); objc_autoreleasePoolPop(pool); return [self initWithTimeIntervalSince1970: seconds]; } - (bool)isEqual: (id)object { OFDate *otherDate; if (object == self) return true; if (![object isKindOfClass: [OFDate class]]) return false; otherDate = object; if (otherDate.timeIntervalSince1970 != self.timeIntervalSince1970) return false; return true; } - (uint32_t)hash { uint32_t hash; double tmp; OF_HASH_INIT(hash); tmp = OF_BSWAP_DOUBLE_IF_BE(self.timeIntervalSince1970); for (size_t i = 0; i < sizeof(double); i++) OF_HASH_ADD(hash, ((char *)&tmp)[i]); OF_HASH_FINALIZE(hash); return hash; |
︙ | ︙ | |||
424 425 426 427 428 429 430 | OFDate *otherDate; if (![(id)object isKindOfClass: [OFDate class]]) @throw [OFInvalidArgumentException exception]; otherDate = (OFDate *)object; | | | | > | | > | 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | OFDate *otherDate; if (![(id)object isKindOfClass: [OFDate class]]) @throw [OFInvalidArgumentException exception]; otherDate = (OFDate *)object; if (self.timeIntervalSince1970 < otherDate.timeIntervalSince1970) return OF_ORDERED_ASCENDING; if (self.timeIntervalSince1970 > otherDate.timeIntervalSince1970) return OF_ORDERED_DESCENDING; return OF_ORDERED_SAME; } - (OFString *)description { return [self dateStringWithFormat: @"%Y-%m-%dT%H:%M:%SZ"]; } - (OFXMLElement *)XMLElementBySerializing { void *pool = objc_autoreleasePoolPush(); OFXMLElement *element; element = [OFXMLElement elementWithName: self.className namespace: OF_SERIALIZATION_NS]; element.stringValue = [OFString stringWithFormat: @"%016" PRIx64, OF_BSWAP64_IF_LE(OF_DOUBLE_TO_INT_RAW(OF_BSWAP_DOUBLE_IF_LE( self.timeIntervalSince1970)))]; [element retain]; objc_autoreleasePoolPop(pool); return [element autorelease]; } - (OFData *)messagePackRepresentation { void *pool = objc_autoreleasePoolPush(); of_time_interval_t timeInterval = self.timeIntervalSince1970; int64_t seconds = (int64_t)timeInterval; uint32_t nanoseconds = (timeInterval - trunc(timeInterval)) * 1000000000; OFData *ret; if (seconds >= 0 && seconds < 0x400000000) { if (seconds <= UINT32_MAX && nanoseconds == 0) { uint32_t seconds32 = (uint32_t)seconds; OFData *data; |
︙ | ︙ | |||
513 514 515 516 517 518 519 | objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (uint32_t)microsecond { | > | > | 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (uint32_t)microsecond { of_time_interval_t timeInterval = self.timeIntervalSince1970; return (uint32_t)((timeInterval - trunc(timeInterval)) * 1000000); } - (uint8_t)second { GMTIME_RET(tm_sec) } |
︙ | ︙ | |||
594 595 596 597 598 599 600 | { LOCALTIME_RET(tm_yday + 1) } - (OFString *)dateStringWithFormat: (OFConstantString *)format { OFString *ret; | > | | | 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | { LOCALTIME_RET(tm_yday + 1) } - (OFString *)dateStringWithFormat: (OFConstantString *)format { OFString *ret; of_time_interval_t timeInterval = self.timeIntervalSince1970; time_t seconds = (time_t)timeInterval; struct tm tm; size_t pageSize; #ifndef OF_WINDOWS char *buffer; #else wchar_t *buffer; #endif if (seconds != trunc(timeInterval)) @throw [OFOutOfRangeException exception]; #ifdef HAVE_GMTIME_R if (gmtime_r(&seconds, &tm) == NULL) @throw [OFOutOfRangeException exception]; #else # ifdef OF_HAVE_THREADS |
︙ | ︙ | |||
654 655 656 657 658 659 660 | return ret; } - (OFString *)localDateStringWithFormat: (OFConstantString *)format { OFString *ret; | > | | | 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 | return ret; } - (OFString *)localDateStringWithFormat: (OFConstantString *)format { OFString *ret; of_time_interval_t timeInterval = self.timeIntervalSince1970; time_t seconds = (time_t)timeInterval; struct tm tm; size_t pageSize; #ifndef OF_WINDOWS char *buffer; #else wchar_t *buffer; #endif if (seconds != trunc(timeInterval)) @throw [OFOutOfRangeException exception]; #ifdef HAVE_LOCALTIME_R if (localtime_r(&seconds, &tm) == NULL) @throw [OFOutOfRangeException exception]; #else # ifdef OF_HAVE_THREADS |
︙ | ︙ | |||
740 741 742 743 744 745 746 | - (of_time_interval_t)timeIntervalSince1970 { return _seconds; } - (of_time_interval_t)timeIntervalSinceDate: (OFDate *)otherDate { | | | | > | 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 | - (of_time_interval_t)timeIntervalSince1970 { return _seconds; } - (of_time_interval_t)timeIntervalSinceDate: (OFDate *)otherDate { return self.timeIntervalSince1970 - otherDate.timeIntervalSince1970; } - (of_time_interval_t)timeIntervalSinceNow { struct timeval t; of_time_interval_t seconds; OF_ENSURE(gettimeofday(&t, NULL) == 0); seconds = t.tv_sec; seconds += (of_time_interval_t)t.tv_usec / 1000000; return self.timeIntervalSince1970 - seconds; } - (OFDate *)dateByAddingTimeInterval: (of_time_interval_t)seconds { return [OFDate dateWithTimeIntervalSince1970: self.timeIntervalSince1970 + seconds]; } @end |