ObjFW  Diff

Differences From Artifact [f76ec7bd46]:

To Artifact [aa9496cc90]:


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







-
+





-
-
+
+

















-
+










-
-
+
+








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

	@try {
		const char *UTF8String = [string UTF8String];
		const char *UTF8String = string.UTF8String;
		struct tm tm = { 0 };
		int16_t tz = 0;

		tm.tm_isdst = -1;

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

		_seconds = 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];
		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;

		tm.tm_isdst = -1;

		if (of_strptime(UTF8String, [format UTF8String],
		    &tm, &tz) != UTF8String + [string UTF8StringLength])
		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
341
342
343
344
345
346
347
348
349


350
351
352

353
354
355
356
357
358
359
341
342
343
344
345
346
347


348
349
350
351

352
353
354
355
356
357
358
359







-
-
+
+


-
+







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

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

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

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}
431
432
433
434
435
436
437
438

439
440
441
442
443


444
445
446
447
448
449
450
431
432
433
434
435
436
437

438
439
440
441


442
443
444
445
446
447
448
449
450







-
+



-
-
+
+







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

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

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

	[element retain];

	objc_autoreleasePoolPop(pool);

	return [element autorelease];
}
622
623
624
625
626
627
628
629

630
631
632
633
634
635

636
637
638
639
640
641
642
622
623
624
625
626
627
628

629
630
631
632
633
634

635
636
637
638
639
640
641
642







-
+





-
+







#endif

	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
#ifndef OF_WINDOWS
		if (strftime(buffer, pageSize, [format UTF8String], &tm) == 0)
		if (strftime(buffer, pageSize, format.UTF8String, &tm) == 0)
			@throw [OFOutOfRangeException exception];

		ret = [OFString stringWithUTF8String: buffer];
#else
		if (wcsftime(buffer, pageSize / sizeof(wchar_t),
		    [format UTF16String], &tm) == 0)
		    format.UTF16String, &tm) == 0)
			@throw [OFOutOfRangeException exception];

		ret = [OFString stringWithUTF16String: buffer];
#endif
	} @finally {
		[self freeMemory: buffer];
	}
682
683
684
685
686
687
688
689

690
691
692
693
694
695

696
697
698
699
700
701
702
682
683
684
685
686
687
688

689
690
691
692
693
694

695
696
697
698
699
700
701
702







-
+





-
+







#endif

	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
#ifndef OF_WINDOWS
		if (strftime(buffer, pageSize, [format UTF8String], &tm) == 0)
		if (strftime(buffer, pageSize, format.UTF8String, &tm) == 0)
			@throw [OFOutOfRangeException exception];

		ret = [OFString stringWithUTF8String: buffer];
#else
		if (wcsftime(buffer, pageSize / sizeof(wchar_t),
		    [format UTF16String], &tm) == 0)
		    format.UTF16String, &tm) == 0)
			@throw [OFOutOfRangeException exception];

		ret = [OFString stringWithUTF16String: buffer];
#endif
	} @finally {
		[self freeMemory: buffer];
	}