Skip to content

Commit c9cd6ac

Browse files
authored
Merge pull request protocolbuffers#2587 from google/revert-2586-objc_timestamp
Revert "Fix Timestamps with dates before the Unix epoch that contain fractional seconds."
2 parents cf477d4 + 1651342 commit c9cd6ac

File tree

2 files changed

+33
-57
lines changed

2 files changed

+33
-57
lines changed

objectivec/GPBWellKnownTypes.m

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ static int32_t SecondsAndNanosFromTimeIntervalSince1970(NSTimeInterval time,
5050
int64_t *outSeconds) {
5151
NSTimeInterval seconds;
5252
NSTimeInterval nanos = modf(time, &seconds);
53-
54-
// Per Timestamp.proto, nanos is non-negative and "Negative second values with
55-
// fractions must still have non-negative nanos values that count forward in
56-
// time. Must be from 0 to 999,999,999 inclusive."
57-
if (nanos < 0) {
58-
--seconds;
59-
nanos = 1.0 + nanos;
60-
}
61-
6253
nanos *= 1e9;
6354
*outSeconds = (int64_t)seconds;
6455
return (int32_t)nanos;

objectivec/Tests/GPBWellKnownTypesTest.m

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -46,54 +46,39 @@ @interface WellKnownTypesTest : XCTestCase
4646
@implementation WellKnownTypesTest
4747

4848
- (void)testTimeStamp {
49-
// Test a pre-Unix epoch date with fractional seconds.
50-
NSTimeInterval interval = -428027599.0 + -483999967/1e9;
51-
NSDate *preEpochDate = [NSDate dateWithTimeIntervalSince1970:interval];
52-
NSDate *now = [NSDate date];
53-
NSDate *future = [NSDate dateWithTimeIntervalSinceNow:kFutureOffsetInterval];
54-
NSArray *datesToTest = @[preEpochDate, now, future];
55-
56-
for (NSDate *date in datesToTest) {
57-
// Test Creation.
58-
GPBTimestamp *timeStamp = [[GPBTimestamp alloc] initWithDate:date];
59-
NSDate *timeStampDate = timeStamp.date;
60-
61-
XCTAssertGreaterThanOrEqual(timeStamp.nanos, 0,
62-
@"|nanos| must be >= 0. Failing date: %@", date);
63-
XCTAssertLessThan(timeStamp.nanos, 1e9, @"|nanos| must be < 1e9. Failing date: %@", date);
64-
65-
// Comparing timeIntervals instead of directly comparing dates because date
66-
// equality requires the time intervals to be exactly the same, and the
67-
// timeintervals go through a bit of floating point error as they are
68-
// converted back and forth from the internal representation.
69-
XCTAssertEqualWithAccuracy(date.timeIntervalSince1970,
70-
timeStampDate.timeIntervalSince1970,
71-
kTimeAccuracy,
72-
@"Failing date: %@", date);
73-
74-
NSTimeInterval time = [date timeIntervalSince1970];
75-
GPBTimestamp *timeStamp2 =
76-
[[GPBTimestamp alloc] initWithTimeIntervalSince1970:time];
77-
NSTimeInterval durationTime = timeStamp2.timeIntervalSince1970;
78-
XCTAssertEqualWithAccuracy(time, durationTime, kTimeAccuracy, @"Failing date: %@", date);
79-
[timeStamp release];
80-
[timeStamp2 release];
81-
82-
// Test Mutation.
83-
GPBTimestamp *timeStamp3 = [[GPBTimestamp alloc] init];
84-
timeStamp3.date = date;
85-
timeStampDate = timeStamp3.date;
86-
XCTAssertEqualWithAccuracy(date.timeIntervalSince1970,
87-
timeStampDate.timeIntervalSince1970,
88-
kTimeAccuracy,
89-
@"Failing date: %@", date);
90-
91-
time = date.timeIntervalSince1970;
92-
timeStamp3.timeIntervalSince1970 = time;
93-
durationTime = timeStamp3.timeIntervalSince1970;
94-
XCTAssertEqualWithAccuracy(time, durationTime, kTimeAccuracy, @"Failing date: %@", date);
95-
[timeStamp3 release];
96-
}
49+
// Test Creation.
50+
NSDate *date = [NSDate date];
51+
GPBTimestamp *timeStamp = [[GPBTimestamp alloc] initWithDate:date];
52+
NSDate *timeStampDate = timeStamp.date;
53+
54+
// Comparing timeIntervals instead of directly comparing dates because date
55+
// equality requires the time intervals to be exactly the same, and the
56+
// timeintervals go through a bit of floating point error as they are
57+
// converted back and forth from the internal representation.
58+
XCTAssertEqualWithAccuracy(date.timeIntervalSince1970,
59+
timeStampDate.timeIntervalSince1970,
60+
kTimeAccuracy);
61+
62+
NSTimeInterval time = [date timeIntervalSince1970];
63+
GPBTimestamp *timeStamp2 =
64+
[[GPBTimestamp alloc] initWithTimeIntervalSince1970:time];
65+
NSTimeInterval durationTime = timeStamp2.timeIntervalSince1970;
66+
XCTAssertEqualWithAccuracy(time, durationTime, kTimeAccuracy);
67+
[timeStamp release];
68+
69+
// Test Mutation.
70+
date = [NSDate dateWithTimeIntervalSinceNow:kFutureOffsetInterval];
71+
timeStamp2.date = date;
72+
timeStampDate = timeStamp2.date;
73+
XCTAssertEqualWithAccuracy(date.timeIntervalSince1970,
74+
timeStampDate.timeIntervalSince1970,
75+
kTimeAccuracy);
76+
77+
time = date.timeIntervalSince1970;
78+
timeStamp2.timeIntervalSince1970 = time;
79+
durationTime = timeStamp2.timeIntervalSince1970;
80+
XCTAssertEqualWithAccuracy(time, durationTime, kTimeAccuracy);
81+
[timeStamp2 release];
9782
}
9883

9984
- (void)testDuration {

0 commit comments

Comments
 (0)