ObjFW  Check-in [33a133cda0]

Overview
Comment:OFDate: Add a designated initializer

This means all other initializers use this initializer, which means all
other initializers can be reused for tagged pointers.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tagged-pointers
Files: files | file ages | folders
SHA3-256: 33a133cda036fdeb9ae0e5fd10e692c0647b82c82fb7d16563aae86a56a37309
User & Date: js on 2020-07-25 14:17:14
Other Links: branch diff | manifest | tags
Context
2020-07-25
16:22
OFDate: Use tagged pointers check-in: ad37db9133 user: js tags: tagged-pointers
14:17
OFDate: Add a designated initializer check-in: 33a133cda0 user: js tags: tagged-pointers
09:58
OFDate: Prepare for tagged pointers check-in: 95830bf78b user: js tags: tagged-pointers
Changes

Modified src/OFDate.h from [8f39732833] to [c713751502].

215
216
217
218
219
220
221
222


223
224
225
226
227
228
229
215
216
217
218
219
220
221

222
223
224
225
226
227
228
229
230







-
+
+







/*!
 * @brief Initializes an already allocated OFDate with the specified date and
 *	  time since 1970-01-01T00:00:00Z.
 *
 * @param seconds The seconds since 1970-01-01T00:00:00Z
 * @return An initialized OFDate with the specified date and time
 */
- (instancetype)initWithTimeIntervalSince1970: (of_time_interval_t)seconds;
- (instancetype)initWithTimeIntervalSince1970: (of_time_interval_t)seconds
    OF_DESIGNATED_INITIALIZER;

/*!
 * @brief Initializes an already allocated OFDate with the specified date and
 *	  time since now.
 *
 * @param seconds The seconds since now
 * @return An initialized OFDate with the specified date and time

Modified src/OFDate.m from [81bcdf5816] to [7bc28080b9].

48
49
50
51
52
53
54
55

56
57
58

59
60
61
62
63
64
65
48
49
50
51
52
53
54

55
56
57

58
59
60
61
62
63
64
65







-
+


-
+







# define trunc(x) ((int64_t)(x))
#endif

@interface OFDate ()
+ (instancetype)of_alloc;
@end

@interface OFDatePlaceholder: OFDate
@interface OFDateSingleton: OFDate
@end

@interface OFDateSingleton: OFDate
@interface OFDatePlaceholder: OFDateSingleton
@end

static struct {
	Class isa;
} placeholder;

static OFDateSingleton *distantFuture, *distantPast;
73
74
75
76
77
78
79














80
81
82
83
84
85
86
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+








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
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
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







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-




















+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	seconds += tm->tm_sec;
	/* Time zone */
	seconds += -(double)*tz * 60;

	return seconds;
}

@implementation OFDatePlaceholder
- (instancetype)init
{
	return (id)[[OFDate of_alloc] init];
}

- (instancetype)initWithTimeIntervalSince1970: (of_time_interval_t)seconds
{
	return (id)[[OFDate of_alloc] initWithTimeIntervalSince1970: seconds];
}

- (instancetype)initWithTimeIntervalSinceNow: (of_time_interval_t)seconds
{
	return (id)[[OFDate of_alloc] initWithTimeIntervalSinceNow: seconds];
}

- (instancetype)initWithDateString: (OFString *)string
			    format: (OFString *)format
{
	return (id)[[OFDate of_alloc] initWithDateString: string
						  format: format];
}

- (instancetype)initWithLocalDateString: (OFString *)string
				 format: (OFString *)format
{
	return (id)[[OFDate of_alloc] initWithLocalDateString: string
						       format: format];
}

- (instancetype)initWithSerialization: (OFXMLElement *)element
{
	return (id)[[OFDate of_alloc] initWithSerialization: element];
}
@end

@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
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
480



481
482
483


484
485

486
487
488
489
490


491
492
493
494
495
496
497
498
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







-
-
+
-
-
-
-
-
-
-
-













-
-
+
-
-
-





-
-
-
-
-
-
+
+
+

-
-
-
-
-
+
+
+

-
+
-
-
-
-
-
-





-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-

-
-
-
+
+
+

-
+

-
-
-
+
+
+
-
-
+

-
-
+
+
-

-
+

-
-
+
+
-
-
-
-
-
+
+
-




-
-
-
-
+
+

-
-
-
+
+
+

-
-
+
+

-
+
-
-
-
-
-
+
+
-







	static of_once_t once = OF_ONCE_INIT;
	of_once(&once, initDistantPast);
	return distantPast;
}

- (instancetype)init
{
	struct timeval t;

	return [self initWithTimeIntervalSince1970: now()];
	self = [super init];

	OF_ENSURE(gettimeofday(&t, NULL) == 0);

	_seconds = t.tv_sec;
	_seconds += (of_time_interval_t)t.tv_usec / 1000000;

	return self;
}

- (instancetype)initWithTimeIntervalSince1970: (of_time_interval_t)seconds
{
	self = [super init];

	_seconds = seconds;

	return self;
}

- (instancetype)initWithTimeIntervalSinceNow: (of_time_interval_t)seconds
{
	self = [self init];

	return [self initWithTimeIntervalSince1970: now() + seconds];
	_seconds += seconds;

	return self;
}

- (instancetype)initWithDateString: (OFString *)string
			    format: (OFString *)format
{
	self = [super init];

	@try {
		const char *UTF8String = string.UTF8String;
		struct tm tm = { 0 };
		int16_t tz = 0;
	const char *UTF8String = string.UTF8String;
	struct tm tm = { .tm_isdst = -1 };
	int16_t tz = 0;

		tm.tm_isdst = -1;

		if (of_strptime(UTF8String, format.UTF8String,
		    &tm, &tz) != UTF8String + string.UTF8StringLength)
			@throw [OFInvalidFormatException exception];
	if (of_strptime(UTF8String, format.UTF8String, &tm, &tz) !=
	    UTF8String + string.UTF8StringLength)
		@throw [OFInvalidFormatException exception];

		_seconds = tmAndTzToTime(&tm, &tz);
	return [self initWithTimeIntervalSince1970: tmAndTzToTime(&tm, &tz)];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (instancetype)initWithLocalDateString: (OFString *)string
				 format: (OFString *)format
{
	self = [super init];

	@try {
		const char *UTF8String = string.UTF8String;
		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;

	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;
		tm.tm_isdst = -1;

		if (of_strptime(UTF8String, format.UTF8String,
		    &tm, &tz) != UTF8String + string.UTF8StringLength)
			@throw [OFInvalidFormatException exception];
	if (of_strptime(UTF8String, format.UTF8String, &tm, &tz) !=
	    UTF8String + string.UTF8StringLength)
		@throw [OFInvalidFormatException exception];

		if (tz == INT16_MAX) {
	if (tz == INT16_MAX) {
#ifdef OF_WINDOWS
			if (func__mktime64 != NULL) {
				if ((_seconds = func__mktime64(&tm)) == -1)
					@throw [OFInvalidFormatException
		if (func__mktime64 != NULL) {
			if ((seconds = func__mktime64(&tm)) == -1)
				@throw [OFInvalidFormatException exception];
					    exception];
			} else {
		} else {
#endif
				if ((_seconds = mktime(&tm)) == -1)
					@throw [OFInvalidFormatException
			if ((seconds = mktime(&tm)) == -1)
				@throw [OFInvalidFormatException exception];
					    exception];
#ifdef OF_WINDOWS
			}
		}
#endif
		} else
			_seconds = tmAndTzToTime(&tm, &tz);
	} else
		seconds = tmAndTzToTime(&tm, &tz);
	} @catch (id e) {
		[self release];
		@throw e;
	}


	return [self initWithTimeIntervalSince1970: seconds];
	return self;
}

- (instancetype)initWithSerialization: (OFXMLElement *)element
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
	void *pool = objc_autoreleasePoolPush();
	of_time_interval_t seconds;

		if (![element.name isEqual: self.className] ||
		    ![element.namespace isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException exception];
	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)));
	seconds = OF_BSWAP_DOUBLE_IF_LE(OF_INT_TO_DOUBLE_RAW(OF_BSWAP64_IF_LE(
	    element.hexadecimalValue)));

		objc_autoreleasePoolPop(pool);
	objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}


	return [self initWithTimeIntervalSince1970: seconds];
	return self;
}

- (bool)isEqual: (id)object
{
	OFDate *otherDate;

	if (object == self)