ObjFW  Check-in [f585645c21]

Overview
Comment:OFDate: Use wcsftime on Windows

strftime creates strings on Windows that are not valid UTF-8.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 0.8
Files: files | file ages | folders
SHA3-256: f585645c2109f054e8f624fb187e02f4e395fadd6656e98cc199391e44f21537
User & Date: js on 2015-11-02 15:10:53
Other Links: branch diff | manifest | tags
Context
2015-12-29
21:42
Refactor OFKernelEventObserver check-in: c5f0c5f9ba user: js tags: 0.8
2015-11-02
15:10
OFDate: Use wcsftime on Windows check-in: f585645c21 user: js tags: 0.8
13:54
OFHTTP{Client,Server}: Concatenate repeated fields check-in: 0913a2e1d9 user: js tags: 0.8
Changes

Modified src/OFDate.m from [d311cf12a2] to [a9bad35577].

506
507
508
509
510
511
512

513



514
515
516
517
518
519
520

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

	char *buffer;




	if (seconds != floor(_seconds))
		@throw [OFOutOfRangeException exception];

#ifdef HAVE_GMTIME_R
	if (gmtime_r(&seconds, &tm) == NULL)
		@throw [OFOutOfRangeException exception];







>

>
>
>







506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

- (OFString*)dateStringWithFormat: (OFConstantString*)format
{
	OFString *ret;
	time_t seconds = (time_t)_seconds;
	struct tm tm;
	size_t pageSize;
#ifndef _WIN32
	char *buffer;
#else
	wchar_t *buffer;
#endif

	if (seconds != floor(_seconds))
		@throw [OFOutOfRangeException exception];

#ifdef HAVE_GMTIME_R
	if (gmtime_r(&seconds, &tm) == NULL)
		@throw [OFOutOfRangeException exception];
537
538
539
540
541
542
543

544
545
546
547







548
549
550
551
552
553
554
555
556
557
558
559
560

561



562
563
564
565
566
567
568
# endif
#endif

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

	@try {

		if (!strftime(buffer, pageSize, [format UTF8String], &tm))
			@throw [OFOutOfRangeException exception];

		ret = [OFString stringWithUTF8String: buffer];







	} @finally {
		[self freeMemory: buffer];
	}

	return ret;
}

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

	char *buffer;




	if (seconds != floor(_seconds))
		@throw [OFOutOfRangeException exception];

#ifdef HAVE_LOCALTIME_R
	if (localtime_r(&seconds, &tm) == NULL)
		@throw [OFOutOfRangeException exception];







>
|



>
>
>
>
>
>
>













>

>
>
>







541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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
# endif
#endif

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

	@try {
#ifndef _WIN32
		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)
			@throw [OFOutOfRangeException exception];

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

	return ret;
}

- (OFString*)localDateStringWithFormat: (OFConstantString*)format
{
	OFString *ret;
	time_t seconds = (time_t)_seconds;
	struct tm tm;
	size_t pageSize;
#ifndef _WIN32
	char *buffer;
#else
	wchar_t *buffer;
#endif

	if (seconds != floor(_seconds))
		@throw [OFOutOfRangeException exception];

#ifdef HAVE_LOCALTIME_R
	if (localtime_r(&seconds, &tm) == NULL)
		@throw [OFOutOfRangeException exception];
585
586
587
588
589
590
591

592
593
594
595







596
597
598
599
600
601
602
# endif
#endif

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

	@try {

		if (!strftime(buffer, pageSize, [format UTF8String], &tm))
			@throw [OFOutOfRangeException exception];

		ret = [OFString stringWithUTF8String: buffer];







	} @finally {
		[self freeMemory: buffer];
	}

	return ret;
}








>
|



>
>
>
>
>
>
>







601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# endif
#endif

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

	@try {
#ifndef _WIN32
		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)
			@throw [OFOutOfRangeException exception];

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

	return ret;
}