Index: src/OFDate.h ================================================================== --- src/OFDate.h +++ src/OFDate.h @@ -72,10 +72,42 @@ * \return A new, autoreleased OFDate with the specified date and time */ + dateWithTimeIntervalSinceNow: (int64_t)seconds microseconds: (uint32_t)microseconds; +/** + * \brief Creates a new OFDate with the specified string in the specified + * format. + * + * The time zone used is UTC. If a time zone is specified anyway, an + * OFInvalidFormatException is thrown. See +[dateWithLocalDateString:format:] + * if you want to specify a time zone. + * + * See the manpage for strptime for information on the format. + * + * \param string The string describing the date + * \param format The format of the string describing the date + * \return A new, autoreleased OFDate with the specified date and time + */ ++ dateWithDateString: (OFString*)string + format: (OFString*)format; + +/** + * \brief Creates a new OFDate with the specified string in the specified + * format. + * + * If no time zone is specified, local time is assumed. + * + * See the manpage for strptime for information on the format. + * + * \param string The string describing the date + * \param format The format of the string describing the date + * \return A new, autoreleased OFDate with the specified date and time + */ ++ dateWithLocalDateString: (OFString*)string + format: (OFString*)format; + /** * \brief Returns a date in the distant future. * * The date is system-dependant. * @@ -115,25 +147,57 @@ /** * \brief Initializes an already allocated OFDate with the specified date and * time since now. * * \param seconds The seconds since now - * \return A new, autoreleased OFDate with the specified date and time + * \return An initialized OFDate with the specified date and time */ - initWithTimeIntervalSinceNow: (int64_t)seconds; /** * \brief Initializes an already allocated OFDate with the specified date and * time since now. * * \param seconds The seconds since now * \param microseconds The microsecond part of the time - * \return A new, autoreleased OFDate with the specified date and time + * \return An initialized OFDate with the specified date and time */ - initWithTimeIntervalSinceNow: (int64_t)seconds microseconds: (uint32_t)microseconds; +/** + * \brief Initializes an already allocated OFDate with the specified string in + * the specified format. + * + * The time zone used is UTC. If a time zone is specified anyway, an + * OFInvalidFormatException is thrown. See -[initWithLocalDateString:format:] + * if you want to specify a time zone. + * + * See the manpage for strptime for information on the format. + * + * \param string The string describing the date + * \param format The format of the string describing the date + * \return An initialized OFDate with the specified date and time + */ +- initWithDateString: (OFString*)string + format: (OFString*)format; + +/** + * \brief Initializes an already allocated OFDate with the specified string in + * the specified format. + * + * If no time zone is specified, local time is assumed. + * + * See the manpage for strptime for information on the format. + * + * \param string The string describing the date + * \param format The format of the string describing the date + * \return An initialized OFDate with the specified date and time + */ +- initWithLocalDateString: (OFString*)string + format: (OFString*)format; + /** * \brief Returns the microsecond of the date. * * \return The microsecond of the date */ Index: src/OFDate.m ================================================================== --- src/OFDate.m +++ src/OFDate.m @@ -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