Index: src/OFDate.h ================================================================== --- src/OFDate.h +++ src/OFDate.h @@ -31,17 +31,17 @@ * \return A new, autoreleased OFDate with the current date and time */ + date; /** - * \param sec The seconds since 1970-01-01 00:00:00 + * \param sec The seconds since 1970-01-01T00:00:00Z * \return A new, autoreleased OFDate with the specified date and time */ + dateWithTimeIntervalSince1970: (int64_t)sec; /** - * \param sec The seconds since 1970-01-01 00:00:00 + * \param sec The seconds since 1970-01-01T00:00:00Z * \param usec The microsecond part of the time * \return A new, autoreleased OFDate with the specified date and time */ + dateWithTimeIntervalSince1970: (int64_t)sec microseconds: (uint32_t)usec; @@ -75,19 +75,19 @@ + distantPast; /** * Initializes an already allocated OFDate with the specified date and time. * - * \param sec The seconds since 1970-01-01 00:00:00 + * \param sec The seconds since 1970-01-01T00:00:00Z * \return An initialized OFDate with the specified date and time */ - initWithTimeIntervalSince1970: (int64_t)sec; /** * Initializes an already allocated OFDate with the specified date and time. * - * \param sec The seconds since 1970-01-01 00:00:00 + * \param sec The seconds since 1970-01-01T00:00:00Z * \param usec The microsecond part of the time * \return An initialized OFDate with the specified date and time */ - initWithTimeIntervalSince1970: (int64_t)sec microseconds: (uint32_t)usec; @@ -211,10 +211,30 @@ * \param date Another date * \return The later date of the two dates */ - (OFDate*)laterDate: (OFDate*)date; +/** + * \return The seconds since 1970-01-01T00:00:00Z + */ +- (int64_t)timeIntervalSince1970; + +/** + * \return The microseconds part of the seconds since 1970-01-01T00:00:00Z + */ +- (uint32_t)microsecondsOfTimeIntervalSince1970; + +/** + * \return The seconds the date is after the receiver + */ +- (int64_t)timeIntervalSinceDate: (OFDate*)date; + +/** + * \return The microseconds part of the seconds the date is after the receiver + */ +- (uint32_t)microsecondsOfTimeIntervalSinceDate: (OFDate*)date; + /** * Returns a new date with the specified time interval added. * * \param sec The seconds after the date * \return A new, autoreleased OFDate Index: src/OFDate.m ================================================================== --- src/OFDate.m +++ src/OFDate.m @@ -225,11 +225,11 @@ sec += sec_; usec += usec_; while (usec > 999999) { - usec -= 999999; + usec -= 10000000; sec++; } return self; } @@ -361,11 +361,11 @@ @try { # endif struct tm *tmp; - if ((tmp = gmtime(&sec)) == NULL) + if ((tmp = gmtime(&sec_)) == NULL) @throw [OFOutOfRangeException newWithClass: isa]; tm = *tmp; # ifdef OF_THREADS } @finally { @@ -404,11 +404,11 @@ @try { # endif struct tm *tmp; - if ((tmp = localtime(&sec)) == NULL) + if ((tmp = localtime(&sec_)) == NULL) @throw [OFOutOfRangeException newWithClass: isa]; tm = *tmp; # ifdef OF_THREADS } @finally { @@ -442,10 +442,51 @@ if ([self compare: date] == OF_ORDERED_ASCENDING) return [[date retain] autorelease]; return [[self retain] autorelease]; } + +- (int64_t)timeIntervalSince1970 +{ + return sec; +} + +- (uint32_t)microsecondsOfTimeIntervalSince1970 +{ + return usec; +} + +- (int64_t)timeIntervalSinceDate: (OFDate*)date +{ + int64_t sec_ = sec - date->sec; + int32_t usec_ = (int32_t)usec - date->usec; + + while (usec_ > 999999) { + usec_ -= 1000000; + sec_++; + } + + while (usec < 0) { + usec_ += 1000000; + sec_--; + } + + return sec_; +} + +- (uint32_t)microsecondsOfTimeIntervalSinceDate: (OFDate*)date +{ + int32_t usec_ = (int32_t)usec - date->usec; + + while (usec_ > 999999) + usec_ -= 1000000; + + while (usec < 0) + usec_ += 1000000; + + return usec_; +} - (OFDate*)dateByAddingTimeInterval: (int64_t)sec_ { return [self dateByAddingTimeInterval: sec_ withMicroseconds: 0]; @@ -456,13 +497,13 @@ { sec_ += sec; usec_ += usec; while (usec_ > 999999) { - usec_ -= 999999; + usec_ -= 1000000; sec_++; } return [OFDate dateWithTimeIntervalSince1970: sec_ microseconds: usec_]; } @end