ObjFW  Check-in [f9d3f89aff]

Overview
Comment:Add -[stringWithFormat:] to OFDate.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f9d3f89aff81631709aca45cd78307ff2abcea206713a941c9c30afbddba1216
User & Date: js on 2010-12-28 21:21:25
Other Links: manifest | tags
Context
2010-12-28
22:18
Make ObjFW work again without threads and without atomic ops. check-in: 88c920bd62 user: js tags: trunk
21:21
Add -[stringWithFormat:] to OFDate. check-in: f9d3f89aff user: js tags: trunk
2010-12-27
22:36
Add methods to access parts of a date, in GMT/UTC. check-in: b97bb55c50 user: js tags: trunk
Changes

Modified src/OFDate.h from [6d9504057e] to [5bb469595c].

8
9
10
11
12
13
14


15
16
17
18
19
20
21
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include <sys/time.h>

#import "OFObject.h"



#ifdef __MINGW32__
/*
 * They think they don't need suseconds_t and can use long instead in
 * struct timeval... POSIX demands suseconds_t, of course.
 */
typedef long suseconds_t;







>
>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include <sys/time.h>

#import "OFObject.h"

@class OFString;

#ifdef __MINGW32__
/*
 * They think they don't need suseconds_t and can use long instead in
 * struct timeval... POSIX demands suseconds_t, of course.
 */
typedef long suseconds_t;
107
108
109
110
111
112
113










114
 */
- (int)dayOfWeek;

/**
 * \return The day of the year of the date
 */
- (int)dayOfYear;










@end







>
>
>
>
>
>
>
>
>
>

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
 */
- (int)dayOfWeek;

/**
 * \return The day of the year of the date
 */
- (int)dayOfYear;

/**
 * Creates a string of the date with the specified format.
 *
 * See the manpage for strftime for information on the format.
 *
 * \param fmt The format for the date string
 * \return A new, autoreleased OFString
 */
- (OFString*)stringWithFormat: (OFString*)fmt;
@end

Modified src/OFDate.m from [52c00039c4] to [0a3ac06fbe].

13
14
15
16
17
18
19

20
21
22
23
24
25
26

#include <time.h>

#include <sys/time.h>

#import "OFDate.h"
#import "OFString.h"

#import "OFExceptions.h"

#if !defined(HAVE_GMTIME_R) && defined(OF_THREADS)
# import "OFThread.h"

static OFMutex *mutex;
#endif







>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#include <time.h>

#include <sys/time.h>

#import "OFDate.h"
#import "OFString.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"

#if !defined(HAVE_GMTIME_R) && defined(OF_THREADS)
# import "OFThread.h"

static OFMutex *mutex;
#endif
148
149
150
151
152
153
154
155

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
		return OF_ORDERED_DESCENDING;

	return OF_ORDERED_SAME;
}

- (OFString*)description
{
	char str[20];	/* YYYY-MM-DD hh:mm:ss */


#ifdef HAVE_GMTIME_R
	struct tm tm;

	if (gmtime_r(&sec, &tm) == NULL)
		@throw [OFOutOfRangeException newWithClass: isa];

	strftime(str, 20, "%Y-%m-%dT%H:%M:%S", &tm);
#else
	struct tm *tm;

# ifdef OF_THREADS
	[mutex lock];

	@try {
# endif
		if ((tm = gmtime(&sec)) == NULL)
			@throw [OFOutOfRangeException newWithClass: isa];

		strftime(str, 20, "%Y-%m-%dT%H:%M:%S", tm);
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}
# endif
#endif

	if (usec == 0)
		return [OFString stringWithFormat: @"%sZ", str];

	return [OFString stringWithFormat: @"%s.%06dZ", str, usec];
}

- (int)seconds
{
	GMTIME_RET(tm_sec)
}








|
>

<
|

|
<
|
<
|
<
|
<
<
|
<
<
<
<

<
<
|
<
<
<
<
|
<
<

|







149
150
151
152
153
154
155
156
157
158

159
160
161

162

163

164


165




166


167




168


169
170
171
172
173
174
175
176
177
		return OF_ORDERED_DESCENDING;

	return OF_ORDERED_SAME;
}

- (OFString*)description
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *tmp, *ret;


	tmp = [self stringWithFormat: @"%Y-%m-%dT%H:%M:%S"];

	if (usec == 0)

		ret = [OFString stringWithFormat: @"%sZ", [tmp cString]];

	else

		ret = [OFString stringWithFormat: @"%s.%06dZ", [tmp cString],


						  usec];







	[ret retain];




	[pool release];



	return [ret autorelease];
}

- (int)seconds
{
	GMTIME_RET(tm_sec)
}

226
227
228
229
230
231
232







































233
	GMTIME_RET(tm_wday)
}

- (int)dayOfYear
{
	GMTIME_RET(tm_yday + 1)
}







































@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
	GMTIME_RET(tm_wday)
}

- (int)dayOfYear
{
	GMTIME_RET(tm_yday + 1)
}

- (OFString*)stringWithFormat: (OFString*)fmt
{
	struct tm tm;
	char *buf;

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

	@try {
# endif
		struct tm *tmp;

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

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

	buf = [self allocMemoryWithSize: of_pagesize];

	@try {
		if (!strftime(buf, of_pagesize, [fmt cString], &tm))
			@throw [OFOutOfRangeException newWithClass: isa];

		return [OFString stringWithCString: buf];
	} @finally {
		[self freeMemory: buf];
	}
}
@end