ObjFW  Diff

Differences From Artifact [a20bfc7333]:

To Artifact [03b1ee2aba]:

  • File src/OFDate.m — part of check-in [e1e7ffa903] at 2011-09-22 23:25:42 on branch trunk — Exceptions are now autoreleased.

    This is safe as an "exception loop" can't happen, since if allocating
    an exception fails, it throws an OFAllocFailedException which is
    preallocated and can always be thrown.

    So, the worst case would be that an autorelease of an exception fails,
    triggering an OFOutOfMemoryException for which there is no memory,
    resulting in an OFAllocFailedException to be thrown. (user: js, size: 11529) [annotate] [blame] [check-ins using]


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

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

#ifdef HAVE_GMTIME_R
# define GMTIME_RET(field)						  \
	time_t seconds_ = (time_t)seconds;				  \
	struct tm tm;							  \
									  \
	if (seconds_ != floor(seconds))					  \
		@throw [OFOutOfRangeException newWithClass: isa];	  \
									  \
	if (gmtime_r(&seconds_, &tm) == NULL)				  \
		@throw [OFOutOfRangeException newWithClass: isa];	  \
									  \
	return tm.field;
# define LOCALTIME_RET(field)						  \
	time_t seconds_ = (time_t)seconds;				  \
	struct tm tm;							  \
									  \
	if (seconds_ != floor(seconds))					  \
		@throw [OFOutOfRangeException newWithClass: isa];	  \
									  \
	if (localtime_r(&seconds_, &tm) == NULL)			  \
		@throw [OFOutOfRangeException newWithClass: isa];	  \
									  \
	return tm.field;
#else
# ifdef OF_THREADS
#  define GMTIME_RET(field)						  \
	time_t seconds_ = (time_t)seconds;				  \
	struct tm *tm;							  \
									  \
	if (seconds_ != floor(seconds))					  \
		@throw [OFOutOfRangeException newWithClass: isa];	  \
									  \
	[mutex lock];							  \
									  \
	@try {								  \
		if ((tm = gmtime(&seconds_)) == NULL)			  \
			@throw [OFOutOfRangeException newWithClass: isa]; \

									  \
		return tm->field;					  \
	} @finally {							  \
		[mutex unlock];						  \
	}
#  define LOCALTIME_RET(field)						  \
	time_t seconds_ = (time_t)seconds;				  \
	struct tm *tm;							  \
									  \
	if (seconds_ != floor(seconds))					  \
		@throw [OFOutOfRangeException newWithClass: isa];	  \
									  \
	[mutex lock];							  \
									  \
	@try {								  \
		if ((tm = localtime(&seconds_)) == NULL)		  \
			@throw [OFOutOfRangeException newWithClass: isa]; \

									  \
		return tm->field;					  \
	} @finally {							  \
		[mutex unlock];						  \
	}
# else
#  define GMTIME_RET(field)						  \
	time_t seconds_ = (time_t)seconds;				  \
	struct tm *tm;							  \
									  \
	if (seconds_ != floor(seconds))					  \
		@throw [OFOutOfRangeException newWithClass: isa];	  \
									  \
	if ((tm = gmtime(&seconds_)) == NULL)				  \
		@throw [OFOutOfRangeException newWithClass: isa];	  \
									  \
	return tm->field;
#  define LOCALTIME_RET(field)						  \
	time_t seconds_ = (time_t)seconds;				  \
	struct tm *tm;							  \
									  \
	if (seconds_ != floor(seconds))					  \
		@throw [OFOutOfRangeException newWithClass: isa];	  \
									  \
	if ((tm = localtime(&seconds_)) == NULL)			  \
		@throw [OFOutOfRangeException newWithClass: isa];	  \
									  \
	return tm->field;
# endif
#endif

@implementation OFDate
#if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \
    defined(OF_THREADS)







|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|



|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|


|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|







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

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

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

@implementation OFDate
#if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \
    defined(OF_THREADS)
225
226
227
228
229
230
231
232

233
234
235

236
237
238

239
240
241
242
243
244
245
	@try {
		struct tm tm = {};

		tm.tm_isdst = -1;

		if (strptime([string UTF8String], [format UTF8String],
		    &tm) == NULL)
			@throw [OFInvalidFormatException newWithClass: isa];


		if (tm.tm_gmtoff)
			@throw [OFInvalidFormatException newWithClass: isa];


		if ((seconds = mktime(&tm)) == -1)
			@throw [OFInvalidFormatException newWithClass: isa];


		seconds += tm.tm_gmtoff;
	} @catch (id e) {
		[self release];
		@throw e;
	}








|
>


|
>


|
>







227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
	@try {
		struct tm tm = {};

		tm.tm_isdst = -1;

		if (strptime([string UTF8String], [format UTF8String],
		    &tm) == NULL)
			@throw [OFInvalidFormatException
			    exceptionWithClass: isa];

		if (tm.tm_gmtoff)
			@throw [OFInvalidFormatException
			    exceptionWithClass: isa];

		if ((seconds = mktime(&tm)) == -1)
			@throw [OFInvalidFormatException
			    exceptionWithClass: isa];

		seconds += tm.tm_gmtoff;
	} @catch (id e) {
		[self release];
		@throw e;
	}

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
	@try {
		struct tm tm = {};

		tm.tm_isdst = -1;

		if (strptime([string UTF8String], [format UTF8String],
		    &tm) == NULL)
			@throw [OFInvalidFormatException newWithClass: isa];


		if ((seconds = mktime(&tm)) == -1)
			@throw [OFInvalidFormatException newWithClass: isa];

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

	return self;
}

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

	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException newWithClass: isa

							       selector: _cmd];

		seconds = [element doubleValue];

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







|
>


|
>

















|
>
|







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
294
295
296
297
298
	@try {
		struct tm tm = {};

		tm.tm_isdst = -1;

		if (strptime([string UTF8String], [format UTF8String],
		    &tm) == NULL)
			@throw [OFInvalidFormatException
			    exceptionWithClass: isa];

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

	return self;
}

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

	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

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

		seconds = [element doubleValue];

		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
}

- (of_comparison_result_t)compare: (id)object
{
	OFDate *otherDate;

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

	otherDate = object;

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







|
|







343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
}

- (of_comparison_result_t)compare: (id)object
{
	OFDate *otherDate;

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

	otherDate = object;

	if (seconds < otherDate->seconds)
		return OF_ORDERED_ASCENDING;
	if (seconds > otherDate->seconds)
		return OF_ORDERED_DESCENDING;
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
499
500
501
502
503
504
505
506
507
508
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
{
	OFString *ret;
	time_t seconds_ = (time_t)seconds;
	struct tm tm;
	char *buffer;

	if (seconds_ != floor(seconds))
		@throw [OFOutOfRangeException newWithClass: isa];

#ifdef HAVE_GMTIME_R
	if (gmtime_r(&seconds_, &tm) == NULL)
		@throw [OFOutOfRangeException newWithClass: isa];
#else
# ifdef OF_THREADS
	[mutex lock];

	@try {
# endif
		struct tm *tmp;

		if ((tmp = gmtime(&seconds_)) == NULL)
			@throw [OFOutOfRangeException newWithClass: isa];

		tm = *tmp;
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}
# endif
#endif

	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		if (!strftime(buffer, of_pagesize, [format UTF8String], &tm))
			@throw [OFOutOfRangeException newWithClass: isa];

		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;
	char *buffer;

	if (seconds_ != floor(seconds))
		@throw [OFOutOfRangeException newWithClass: isa];

#ifdef HAVE_LOCALTIME_R
	if (localtime_r(&seconds_, &tm) == NULL)
		@throw [OFOutOfRangeException newWithClass: isa];
#else
# ifdef OF_THREADS
	[mutex lock];

	@try {
# endif
		struct tm *tmp;

		if ((tmp = localtime(&seconds_)) == NULL)
			@throw [OFOutOfRangeException newWithClass: isa];

		tm = *tmp;
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}
# endif
#endif

	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		if (!strftime(buffer, of_pagesize, [format UTF8String], &tm))
			@throw [OFOutOfRangeException newWithClass: isa];

		ret = [OFString stringWithUTF8String: buffer];
	} @finally {
		[self freeMemory: buffer];
	}

	return ret;







|



|









|













|

















|



|









|













|







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
499
500
501
502
503
504
505
506
507
508
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
543
544
545
546
547
548
{
	OFString *ret;
	time_t seconds_ = (time_t)seconds;
	struct tm tm;
	char *buffer;

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

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

	@try {
# endif
		struct tm *tmp;

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

		tm = *tmp;
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}
# endif
#endif

	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		if (!strftime(buffer, of_pagesize, [format UTF8String], &tm))
			@throw [OFOutOfRangeException exceptionWithClass: isa];

		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;
	char *buffer;

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

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

	@try {
# endif
		struct tm *tmp;

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

		tm = *tmp;
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}
# endif
#endif

	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		if (!strftime(buffer, of_pagesize, [format UTF8String], &tm))
			@throw [OFOutOfRangeException exceptionWithClass: isa];

		ret = [OFString stringWithUTF8String: buffer];
	} @finally {
		[self freeMemory: buffer];
	}

	return ret;