@@ -15,10 +15,11 @@ #include #import "OFDate.h" #import "OFString.h" +#import "OFAutoreleasePool.h" #import "OFExceptions.h" #if !defined(HAVE_GMTIME_R) && defined(OF_THREADS) # import "OFThread.h" @@ -150,42 +151,25 @@ 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 + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFString *tmp, *ret; + + tmp = [self stringWithFormat: @"%Y-%m-%dT%H:%M:%S"]; if (usec == 0) - return [OFString stringWithFormat: @"%sZ", str]; + ret = [OFString stringWithFormat: @"%sZ", [tmp cString]]; + else + ret = [OFString stringWithFormat: @"%s.%06dZ", [tmp cString], + usec]; + + [ret retain]; + [pool release]; - return [OFString stringWithFormat: @"%s.%06dZ", str, usec]; + return [ret autorelease]; } - (int)seconds { GMTIME_RET(tm_sec) @@ -228,6 +212,45 @@ - (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