1
2
3
4
5
6
7
8
9
|
1
2
3
4
5
6
7
8
9
|
-
+
|
/*
* Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
* Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This file is part of ObjFW. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE.QPL included in
* the packaging of this file.
*
|
| ︙ | | |
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
|
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
|
+
+
-
-
+
+
-
+
-
-
+
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
+
-
+
-
+
|
#include <limits.h>
#include <time.h>
#include <math.h>
#include <sys/time.h>
#import "OFDate.h"
#import "OFConcreteDate.h"
#import "OFData.h"
#import "OFDictionary.h"
#import "OFMessagePackExtension.h"
#ifdef OF_HAVE_THREADS
# import "OFMutex.h"
#endif
#import "OFStrFTime.h"
#import "OFStrPTime.h"
#import "OFString.h"
#import "OFSystemInfo.h"
#import "OFXMLAttribute.h"
#import "OFXMLElement.h"
#import "OFTaggedPointerDate.h"
#import "OFXMLAttribute.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#if defined(OF_AMIGAOS_M68K) || defined(OF_MINT)
/* amiga-gcc and freemint-gcc do not have trunc() */
# define trunc(x) ((int64_t)(x))
#endif
@interface OFDate ()
@interface OFPlaceholderDate: OFDate
+ (instancetype)of_alloc;
@end
@interface OFDateSingleton: OFDate
@interface OFConcreteDateSingleton: OFConcreteDate
@end
@interface OFDatePlaceholder: OFDateSingleton
@end
#if defined(OF_OBJFW_RUNTIME) && UINTPTR_MAX == UINT64_MAX
@interface OFTaggedPointerDate: OFDateSingleton
@end
#endif
static struct {
Class isa;
} placeholder;
static OFDateSingleton *zeroDate, *distantFuture, *distantPast;
static OFConcreteDateSingleton *zeroDate, *distantFuture, *distantPast;
#if defined(OF_OBJFW_RUNTIME) && UINTPTR_MAX == UINT64_MAX
static int dateTag;
#endif
static void
initZeroDate(void)
{
zeroDate = [[OFConcreteDateSingleton alloc]
zeroDate = [[OFDateSingleton alloc] initWithTimeIntervalSince1970: 0];
initWithTimeIntervalSince1970: 0];
}
static void
initDistantFuture(void)
{
distantFuture = [[OFDateSingleton alloc]
distantFuture = [[OFConcreteDateSingleton alloc]
initWithTimeIntervalSince1970: 64060588800.0];
}
static void
initDistantPast(void)
{
distantPast = [[OFDateSingleton alloc]
distantPast = [[OFConcreteDateSingleton alloc]
initWithTimeIntervalSince1970: -62167219200.0];
}
static OFTimeInterval
now(void)
{
struct timeval tv;
|
| ︙ | | |
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
|
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
|
seconds += tm->tm_sec;
/* Time zone */
seconds += -(double)tz * 60;
return seconds;
}
@implementation OFDateSingleton
@implementation OFConcreteDateSingleton
- (instancetype)autorelease
{
return self;
}
OF_SINGLETON_METHODS
- (instancetype)retain
{
return self;
}
- (void)release
{
}
- (unsigned int)retainCount
{
return OFMaxRetainCount;
}
@end
@implementation OFDatePlaceholder
@implementation OFPlaceholderDate
#ifdef __clang__
/* We intentionally don't call into super, so silence the warning. */
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunknown-pragmas"
# pragma clang diagnostic ignored "-Wobjc-designated-initializers"
#endif
- (instancetype)initWithTimeIntervalSince1970: (OFTimeInterval)seconds
|
| ︙ | | |
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
|
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
|
-
-
+
+
+
-
+
-
-
-
-
-
-
-
+
-
-
+
-
-
-
+
-
-
-
+
-
+
-
-
-
-
-
-
-
-
-
|
#if defined(OF_OBJFW_RUNTIME) && UINTPTR_MAX == UINT64_MAX
value = OFFromBigEndian64(OFDoubleToRawUInt64(OFToBigEndianDouble(
seconds)));
/* Almost all dates fall into this range. */
if (value & (UINT64_C(4) << 60)) {
id ret = objc_createTaggedPointer(dateTag,
value & ~(UINT64_C(4) << 60));
id ret = [OFTaggedPointerDate
dateWithUInt64TimeIntervalSince1970: value];
if (ret != nil)
return ret;
}
#endif
return (id)[[OFConcreteDate alloc]
return (id)[[OFDate of_alloc] initWithTimeIntervalSince1970: seconds];
initWithTimeIntervalSince1970: seconds];
}
#ifdef __clang__
# pragma clang diagnostic pop
#endif
@end
#if defined(OF_OBJFW_RUNTIME) && UINTPTR_MAX == UINT64_MAX
@implementation OFTaggedPointerDate
- (OFTimeInterval)timeIntervalSince1970
{
uint64_t value = (uint64_t)object_getTaggedPointerValue(self);
OF_SINGLETON_METHODS
value |= UINT64_C(4) << 60;
@end
return OFFromBigEndianDouble(OFRawUInt64ToDouble(OFToBigEndian64(
value)));
}
@end
#endif
@implementation OFDate
+ (void)initialize
{
#ifdef OF_WINDOWS
HMODULE module;
#endif
if (self != [OFDate class])
return;
placeholder.isa = [OFDatePlaceholder class];
object_setClass((id)&placeholder, [OFPlaceholderDate class]);
#if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \
defined(OF_HAVE_THREADS)
mutex = [[OFMutex alloc] init];
atexit(releaseMutex);
#endif
#ifdef OF_WINDOWS
if ((module = LoadLibrary("msvcrt.dll")) != NULL)
if ((module = GetModuleHandle("msvcrt.dll")) != NULL)
_mktime64FuncPtr = (__time64_t (*)(struct tm *))
GetProcAddress(module, "_mktime64");
#endif
#if defined(OF_OBJFW_RUNTIME) && UINTPTR_MAX == UINT64_MAX
dateTag = objc_registerTaggedPointerClass([OFTaggedPointerDate class]);
#endif
}
+ (instancetype)of_alloc
{
return [super alloc];
}
+ (instancetype)alloc
{
if (self == [OFDate class])
return (id)&placeholder;
|
| ︙ | | |
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
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
|
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
|
- (instancetype)init
{
return [self initWithTimeIntervalSince1970: now()];
}
- (instancetype)initWithTimeIntervalSince1970: (OFTimeInterval)seconds
{
if ([self isMemberOfClass: [OFDate class]]) {
@try {
[self doesNotRecognizeSelector: _cmd];
} @catch (id e) {
self = [super init];
_seconds = seconds;
return self;
[self release];
@throw e;
}
abort();
}
return [super init];
}
- (instancetype)initWithTimeIntervalSinceNow: (OFTimeInterval)seconds
{
return [self initWithTimeIntervalSince1970: now() + seconds];
}
|
| ︙ | | |
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
|
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
seconds = tmAndTzToTime(&tm, tz);
objc_autoreleasePoolPop(pool);
return [self initWithTimeIntervalSince1970: seconds];
}
- (instancetype)initWithSerialization: (OFXMLElement *)element
{
OFTimeInterval seconds;
@try {
void *pool = objc_autoreleasePoolPush();
unsigned long long value;
if (![element.name isEqual: @"OFDate"] ||
![element.namespace isEqual: OFSerializationNS])
@throw [OFInvalidArgumentException exception];
if (![[element attributeForName: @"encoding"].stringValue
isEqual: @"hex"])
@throw [OFInvalidFormatException exception];
value = [element unsignedLongLongValueWithBase: 16];
if (value > UINT64_MAX)
@throw [OFOutOfRangeException exception];
seconds = OFFromBigEndianDouble(OFRawUInt64ToDouble(
OFToBigEndian64(value)));
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
@throw e;
}
return [self initWithTimeIntervalSince1970: seconds];
}
- (bool)isEqual: (id)object
{
OFDate *otherDate;
if (object == self)
return true;
|
| ︙ | | |
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
return OFOrderedDescending;
return OFOrderedSame;
}
- (OFString *)description
{
return [self dateStringWithFormat: @"%Y-%m-%dT%H:%M:%SZ"];
return [self dateStringWithFormat: @"%Y-%m-%dT%H:%M:%S%z"];
}
- (OFXMLElement *)XMLElementBySerializing
{
void *pool = objc_autoreleasePoolPush();
OFXMLElement *element;
element = [OFXMLElement elementWithName: @"OFDate"
namespace: OFSerializationNS];
[element addAttributeWithName: @"encoding" stringValue: @"hex"];
element.stringValue = [OFString stringWithFormat: @"%016" PRIx64,
OFFromBigEndian64(OFDoubleToRawUInt64(OFToBigEndianDouble(
self.timeIntervalSince1970)))];
[element retain];
objc_autoreleasePoolPop(pool);
return [element autorelease];
}
- (OFData *)messagePackRepresentation
{
void *pool = objc_autoreleasePoolPush();
OFTimeInterval timeInterval = self.timeIntervalSince1970;
int64_t seconds = (int64_t)timeInterval;
|
| ︙ | | |
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
-
-
-
-
|
- (OFString *)dateStringWithFormat: (OFConstantString *)format
{
OFString *ret;
OFTimeInterval 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];
|
| ︙ | | |
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
|
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
|
}
# endif
#endif
pageSize = [OFSystemInfo pageSize];
buffer = OFAllocMemory(1, pageSize);
@try {
#ifndef OF_WINDOWS
if (strftime(buffer, pageSize, format.UTF8String, &tm) == 0)
if (OFStrFTime(buffer, pageSize, format.UTF8String, &tm,
0) == 0)
@throw [OFOutOfRangeException exception];
ret = [OFString stringWithUTF8String: buffer];
#else
if (wcsftime(buffer, pageSize / sizeof(wchar_t),
format.UTF16String, &tm) == 0)
@throw [OFOutOfRangeException exception];
ret = [OFString stringWithUTF16String: buffer];
#endif
} @finally {
OFFreeMemory(buffer);
}
return ret;
}
- (OFString *)localDateStringWithFormat: (OFConstantString *)format
{
OFString *ret;
OFTimeInterval 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];
|
| ︙ | | |
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
|
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
-
-
+
+
-
-
-
-
-
-
-
|
}
# endif
#endif
pageSize = [OFSystemInfo pageSize];
buffer = OFAllocMemory(1, pageSize);
@try {
#ifndef OF_WINDOWS
if (strftime(buffer, pageSize, format.UTF8String, &tm) == 0)
if (OFStrFTime(buffer, pageSize, format.UTF8String, &tm,
0) == 0)
@throw [OFOutOfRangeException exception];
ret = [OFString stringWithUTF8String: buffer];
#else
if (wcsftime(buffer, pageSize / sizeof(wchar_t),
format.UTF16String, &tm) == 0)
@throw [OFOutOfRangeException exception];
ret = [OFString stringWithUTF16String: buffer];
#endif
} @finally {
OFFreeMemory(buffer);
}
return ret;
}
|
| ︙ | | |
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
|
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
|
-
+
|
return otherDate;
return self;
}
- (OFTimeInterval)timeIntervalSince1970
{
return _seconds;
OF_UNRECOGNIZED_SELECTOR
}
- (OFTimeInterval)timeIntervalSinceDate: (OFDate *)otherDate
{
return self.timeIntervalSince1970 - otherDate.timeIntervalSince1970;
}
|
| ︙ | | |