ObjFW  Diff

Differences From Artifact [a29328b01b]:

To Artifact [bc182cbf9d]:


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







-
+


-
+



-
+





-
+


-
+



-
+







-
+


-
+






-
+








-
+


-
+






-
+









-
+


-
+



-
+





-
+


-
+



-
+







#if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \
    defined(OF_HAVE_THREADS)
static OFMutex *mutex;
#endif

#ifdef HAVE_GMTIME_R
# define GMTIME_RET(field)						\
	time_t seconds_ = (time_t)seconds;				\
	time_t seconds = (time_t)_seconds;				\
	struct tm tm;							\
									\
	if (seconds_ != floor(seconds))					\
	if (seconds != floor(_seconds))					\
		@throw [OFOutOfRangeException				\
		    exceptionWithClass: [self class]];			\
									\
	if (gmtime_r(&seconds_, &tm) == NULL)				\
	if (gmtime_r(&seconds, &tm) == NULL)				\
		@throw [OFOutOfRangeException				\
		    exceptionWithClass: [self class]];			\
									\
	return tm.field;
# define LOCALTIME_RET(field)						\
	time_t seconds_ = (time_t)seconds;				\
	time_t seconds = (time_t)_seconds;				\
	struct tm tm;							\
									\
	if (seconds_ != floor(seconds))					\
	if (seconds != floor(_seconds))					\
		@throw [OFOutOfRangeException				\
		    exceptionWithClass: [self class]];			\
									\
	if (localtime_r(&seconds_, &tm) == NULL)			\
	if (localtime_r(&seconds, &tm) == NULL)				\
		@throw [OFOutOfRangeException				\
		    exceptionWithClass: [self class]];			\
									\
	return tm.field;
#else
# ifdef OF_HAVE_THREADS
#  define GMTIME_RET(field)						\
	time_t seconds_ = (time_t)seconds;				\
	time_t seconds = (time_t)_seconds;				\
	struct tm *tm;							\
									\
	if (seconds_ != floor(seconds))					\
	if (seconds != floor(_seconds))					\
		@throw [OFOutOfRangeException				\
		    exceptionWithClass: [self class]];			\
									\
	[mutex lock];							\
									\
	@try {								\
		if ((tm = gmtime(&seconds_)) == NULL)			\
		if ((tm = gmtime(&seconds)) == NULL)			\
			@throw [OFOutOfRangeException			\
			    exceptionWithClass: [self class]];		\
									\
		return tm->field;					\
	} @finally {							\
		[mutex unlock];						\
	}
#  define LOCALTIME_RET(field)						\
	time_t seconds_ = (time_t)seconds;				\
	time_t seconds = (time_t)_seconds;				\
	struct tm *tm;							\
									\
	if (seconds_ != floor(seconds))					\
	if (seconds != floor(_seconds))					\
		@throw [OFOutOfRangeException				\
		    exceptionWithClass: [self class]];			\
									\
	[mutex lock];							\
									\
	@try {								\
		if ((tm = localtime(&seconds_)) == NULL)		\
		if ((tm = localtime(&seconds)) == NULL)			\
			@throw [OFOutOfRangeException			\
			    exceptionWithClass: [self class]];		\
									\
		return tm->field;					\
	} @finally {							\
		[mutex unlock];						\
	}
# else
#  define GMTIME_RET(field)						\
	time_t seconds_ = (time_t)seconds;				\
	time_t seconds = (time_t)_seconds;				\
	struct tm *tm;							\
									\
	if (seconds_ != floor(seconds))					\
	if (seconds != floor(_seconds))					\
		@throw [OFOutOfRangeException				\
		    exceptionWithClass: [self class]];			\
									\
	if ((tm = gmtime(&seconds_)) == NULL)				\
	if ((tm = gmtime(&seconds)) == NULL)				\
		@throw [OFOutOfRangeException				\
		    exceptionWithClass: [self class]];			\
									\
	return tm->field;
#  define LOCALTIME_RET(field)						\
	time_t seconds_ = (time_t)seconds;				\
	time_t seconds = (time_t)_seconds;				\
	struct tm *tm;							\
									\
	if (seconds_ != floor(seconds))					\
	if (seconds != floor(_seconds))					\
		@throw [OFOutOfRangeException				\
		    exceptionWithClass: [self class]];			\
									\
	if ((tm = localtime(&seconds_)) == NULL)			\
	if ((tm = localtime(&seconds)) == NULL)				\
		@throw [OFOutOfRangeException				\
		    exceptionWithClass: [self class]];			\
									\
	return tm->field;
# endif
#endif

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







-
+

-
-
+
+




-
+



-
+




-
+



-
+




















-
+

-
-
-
+
+
+




-
+




-
+

-
+

-
+

-
+

-
+








- init
{
	struct timeval t;

	self = [super init];

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

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

	return self;
}

- initWithTimeIntervalSince1970: (double)seconds_
- initWithTimeIntervalSince1970: (double)seconds
{
	self = [super init];

	seconds = seconds_;
	_seconds = seconds;

	return self;
}

- initWithTimeIntervalSinceNow: (double)seconds_
- initWithTimeIntervalSinceNow: (double)seconds
{
	self = [self init];

	seconds += seconds_;
	_seconds += seconds;

	return self;
}

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

	@try {
		struct tm tm = {};

		tm.tm_isdst = -1;

		if (of_strptime([string UTF8String], [format UTF8String],
		    &tm) == NULL)
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];

		/* Years */
		seconds = (int64_t)(tm.tm_year - 70) * 31536000;
		_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;
		_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;
			_seconds += 86400;
		/* Months */
		if (tm.tm_mon < 0 || tm.tm_mon > 12)
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];
		seconds += month_to_day_of_year[tm.tm_mon] * 86400;
		_seconds += month_to_day_of_year[tm.tm_mon] * 86400;
		/* Days */
		seconds += (tm.tm_mday - 1) * 86400;
		_seconds += (tm.tm_mday - 1) * 86400;
		/* Hours */
		seconds += tm.tm_hour * 3600;
		_seconds += tm.tm_hour * 3600;
		/* Minutes */
		seconds += tm.tm_min * 60;
		_seconds += tm.tm_min * 60;
		/* Seconds */
		seconds += tm.tm_sec;
		_seconds += tm.tm_sec;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
303
304
305
306
307
308
309
310

311
312
313
314
315
316
317
303
304
305
306
307
308
309

310
311
312
313
314
315
316
317







-
+







		tm.tm_isdst = -1;

		if (of_strptime([string UTF8String], [format UTF8String],
		    &tm) == NULL)
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];

		if ((seconds = mktime(&tm)) == -1)
		if ((_seconds = mktime(&tm)) == -1)
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];
	} @catch (id e) {
		[self release];
		@throw e;
	}

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







-
+



















-
+














-
+







		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		d.u = (uint64_t)[element hexadecimalValue];
		seconds = d.d;
		_seconds = d.d;

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

	return self;
}

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

	if (![object isKindOfClass: [OFDate class]])
		return NO;

	otherDate = object;

	if (otherDate->seconds != seconds)
	if (otherDate->_seconds != _seconds)
		return NO;

	return YES;
}

- (uint32_t)hash
{
	uint32_t hash;
	union {
		double d;
		uint8_t b[sizeof(double)];
	} d;
	uint_fast8_t i;

	d.d = OF_BSWAP_DOUBLE_IF_BE(seconds);
	d.d = OF_BSWAP_DOUBLE_IF_BE(_seconds);

	OF_HASH_INIT(hash);

	for (i = 0; i < sizeof(double); i++)
		OF_HASH_ADD(hash, d.b[i]);

	OF_HASH_FINALIZE(hash);
395
396
397
398
399
400
401
402

403
404

405
406
407
408
409
410
411
395
396
397
398
399
400
401

402
403

404
405
406
407
408
409
410
411







-
+

-
+







	if (![object isKindOfClass: [OFDate class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherDate = (OFDate*)object;

	if (seconds < otherDate->seconds)
	if (_seconds < otherDate->_seconds)
		return OF_ORDERED_ASCENDING;
	if (seconds > otherDate->seconds)
	if (_seconds > otherDate->_seconds)
		return OF_ORDERED_DESCENDING;

	return OF_ORDERED_SAME;
}

- (OFString*)description
{
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
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







-
+












-
+







		double d;
		uint64_t u;
	} d;

	element = [OFXMLElement elementWithName: [self className]
				      namespace: OF_SERIALIZATION_NS];

	d.d = seconds;
	d.d = _seconds;
	[element setStringValue:
	    [OFString stringWithFormat: @"%016" PRIx64, d.u]];

	[element retain];

	objc_autoreleasePoolPop(pool);

	return [element autorelease];
}

- (uint32_t)microsecond
{
	return (uint32_t)rint((seconds - floor(seconds)) * 1000000);
	return (uint32_t)rint((_seconds - floor(_seconds)) * 1000000);
}

- (uint8_t)second
{
	GMTIME_RET(tm_sec)
}

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







-
+




-
+



-
+









-
+







{
	LOCALTIME_RET(tm_yday + 1)
}

- (OFString*)dateStringWithFormat: (OFConstantString*)format
{
	OFString *ret;
	time_t seconds_ = (time_t)seconds;
	time_t seconds = (time_t)_seconds;
	struct tm tm;
	size_t pageSize;
	char *buffer;

	if (seconds_ != floor(seconds))
	if (seconds != floor(_seconds))
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

#ifdef HAVE_GMTIME_R
	if (gmtime_r(&seconds_, &tm) == NULL)
	if (gmtime_r(&seconds, &tm) == NULL)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];
#else
# ifdef OF_HAVE_THREADS
	[mutex lock];

	@try {
# endif
		struct tm *tmp;

		if ((tmp = gmtime(&seconds_)) == NULL)
		if ((tmp = gmtime(&seconds)) == NULL)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		tm = *tmp;
# ifdef OF_HAVE_THREADS
	} @finally {
		[mutex unlock];
559
560
561
562
563
564
565
566

567
568
569
570
571

572
573
574
575

576
577
578
579
580
581
582
583
584
585

586
587
588
589
590
591
592
559
560
561
562
563
564
565

566
567
568
569
570

571
572
573
574

575
576
577
578
579
580
581
582
583
584

585
586
587
588
589
590
591
592







-
+




-
+



-
+









-
+








	return ret;
}

- (OFString*)localDateStringWithFormat: (OFConstantString*)format
{
	OFString *ret;
	time_t seconds_ = (time_t)seconds;
	time_t seconds = (time_t)_seconds;
	struct tm tm;
	size_t pageSize;
	char *buffer;

	if (seconds_ != floor(seconds))
	if (seconds != floor(_seconds))
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

#ifdef HAVE_LOCALTIME_R
	if (localtime_r(&seconds_, &tm) == NULL)
	if (localtime_r(&seconds, &tm) == NULL)
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];
#else
# ifdef OF_HAVE_THREADS
	[mutex lock];

	@try {
# endif
		struct tm *tmp;

		if ((tmp = localtime(&seconds_)) == NULL)
		if ((tmp = localtime(&seconds)) == NULL)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		tm = *tmp;
# ifdef OF_HAVE_THREADS
	} @finally {
		[mutex unlock];
630
631
632
633
634
635
636
637

638
639
640
641
642

643
644
645
646
647
648

649
650
651
652
653


654
655

656
657
658

659
660

661
662
630
631
632
633
634
635
636

637
638
639
640
641

642
643
644
645
646
647

648
649
650
651


652
653
654

655
656
657

658
659

660
661
662







-
+




-
+





-
+



-
-
+
+

-
+


-
+

-
+


		return [[otherDate retain] autorelease];

	return [[self retain] autorelease];
}

- (double)timeIntervalSince1970
{
	return seconds;
	return _seconds;
}

- (double)timeIntervalSinceDate: (OFDate*)otherDate
{
	return seconds - otherDate->seconds;
	return _seconds - otherDate->_seconds;
}

- (double)timeIntervalSinceNow
{
	struct timeval t;
	double seconds_;
	double seconds;

	OF_ENSURE(!gettimeofday(&t, NULL));

	seconds_ = t.tv_sec;
	seconds_ += (double)t.tv_usec / 1000000;
	seconds = t.tv_sec;
	seconds += (double)t.tv_usec / 1000000;

	return seconds - seconds_;
	return _seconds - seconds;
}

- (OFDate*)dateByAddingTimeInterval: (double)seconds_
- (OFDate*)dateByAddingTimeInterval: (double)seconds
{
	return [OFDate dateWithTimeIntervalSince1970: seconds + seconds_];
	return [OFDate dateWithTimeIntervalSince1970: _seconds + seconds];
}
@end