@@ -31,10 +31,11 @@ # import "OFThread.h" #endif #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" +#import "OFInvalidFormatException.h" #import "OFOutOfRangeException.h" #import "macros.h" #if (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) && \ @@ -167,10 +168,24 @@ { return [[[self alloc] initWithTimeIntervalSinceNow: seconds microseconds: microseconds] autorelease]; } + ++ dateWithDateString: (OFString*)string + format: (OFString*)format +{ + return [[[self alloc] initWithDateString: string + format: format] autorelease]; +} + ++ dateWithLocalDateString: (OFString*)string + format: (OFString*)format +{ + return [[[self alloc] initWithLocalDateString: string + format: format] autorelease]; +} + distantFuture { if (sizeof(time_t) == sizeof(int64_t)) return [[[self alloc] @@ -238,10 +253,63 @@ seconds += seconds_; microseconds += microseconds_; seconds += microseconds / 1000000; microseconds %= 1000000; + + return self; +} + +- initWithDateString: (OFString*)string + format: (OFString*)format +{ + self = [super init]; + + @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; + } + + return self; +} + +- initWithLocalDateString: (OFString*)string + format: (OFString*)format +{ + self = [super init]; + + @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