ObjFW  Diff

Differences From Artifact [b94395832f]:

To Artifact [249eca1bb1]:


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 WINAPI __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 = (WINAPI __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
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







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







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