ObjFW  Diff

Differences From Artifact [ab9874f31d]:

To Artifact [1c4647d564]:


46
47
48
49
50
51
52




53
54
55
56
57
58
59
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63







+
+
+
+







# 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)						\
	time_t seconds = (time_t)_seconds;				\
	struct tm tm;							\
									\
	if (seconds != trunc(_seconds))					\
182
183
184
185
186
187
188
189
190
191
192




193
194
195












196

197
198
199
200
201
202
203
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







-
-


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

+







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

	return seconds;
}

@implementation OFDate
#if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \
    defined(OF_HAVE_THREADS)
+ (void)initialize
{
#ifdef OF_WINDOWS
	HMODULE module;
#endif

	if (self == [OFDate class])
		mutex = [[OFMutex alloc] init];
}
	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
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
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
377
378







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

















-
-
-
-





-
-
-
+
+







		tm.tm_isdst = -1;

		if (of_strptime(UTF8String, format.UTF8String,
		    &tm, &tz) != UTF8String + string.UTF8StringLength)
			@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];
#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);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

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

	@try {
		void *pool = objc_autoreleasePoolPush();
		union {
			double d;
			uint64_t u;
		} d;

		if (![element.name isEqual: self.className] ||
		    ![element.namespace isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException exception];

		d.u = (uint64_t)element.hexadecimalValue;
		d.u = OF_BSWAP64_IF_LE(d.u);
		_seconds = OF_BSWAP_DOUBLE_IF_LE(d.d);
		_seconds = OF_BSWAP_DOUBLE_IF_LE(OF_INT_TO_DOUBLE_RAW(
		    OF_BSWAP64_IF_LE(element.hexadecimalValue)));

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

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
396
397
398
399
400
401
402


403


404

405
406

407
408
409

410
411
412
413
414
415
416
417







-
-
+
-
-

-
+

-
+


-
+








	return true;
}

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

	d.d = OF_BSWAP_DOUBLE_IF_BE(_seconds);
	OF_HASH_INIT(hash);

	OF_HASH_INIT(hash);
	tmp = OF_BSWAP_DOUBLE_IF_BE(_seconds);

	for (size_t i = 0; i < sizeof(double); i++)
		OF_HASH_ADD(hash, d.b[i]);
		OF_HASH_ADD(hash, ((char *)&tmp)[i]);

	OF_HASH_FINALIZE(hash);

	return hash;
}

- (id)copy
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
441
442
443
444
445
446
447




448
449
450
451



452
453
454
455
456
457
458
459
460
461







-
-
-
-




-
-
-
+
+
+







	return [self dateStringWithFormat: @"%Y-%m-%dT%H:%M:%SZ"];
}

- (OFXMLElement *)XMLElementBySerializing
{
	void *pool = objc_autoreleasePoolPush();
	OFXMLElement *element;
	union {
		double d;
		uint64_t u;
	} d;

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

	d.d = OF_BSWAP_DOUBLE_IF_LE(_seconds);
	element.stringValue =
	    [OFString stringWithFormat: @"%016" PRIx64, OF_BSWAP64_IF_LE(d.u)];
	element.stringValue = [OFString stringWithFormat: @"%016" PRIx64,
	    OF_BSWAP64_IF_LE(OF_DOUBLE_TO_INT_RAW(OF_BSWAP_DOUBLE_IF_LE(
	    _seconds)))];

	[element retain];

	objc_autoreleasePoolPop(pool);

	return [element autorelease];
}