aboutsummaryrefslogtreecommitdiffhomepage
path: root/objectivec/GPBWellKnownTypes.m
diff options
context:
space:
mode:
authorGravatar Thomas Van Lenten <thomasvl@google.com>2017-01-12 15:50:59 -0500
committerGravatar GitHub <noreply@github.com>2017-01-12 15:50:59 -0500
commitcf477d46ca8a837bc92e591617f283d9fa1ace5c (patch)
treea5151cc514710a1b7bd40bd85732ec606f71a91e /objectivec/GPBWellKnownTypes.m
parentfeb78fb293ac891baa4a25047581f34589f33a2f (diff)
parentadcccd0f8161394f61e39fd9c26eada35e091a8f (diff)
Merge pull request #2586 from thomasvl/objc_timestamp
Fix Timestamps with dates before the Unix epoch that contain fractional seconds.
Diffstat (limited to 'objectivec/GPBWellKnownTypes.m')
-rw-r--r--objectivec/GPBWellKnownTypes.m9
1 files changed, 9 insertions, 0 deletions
diff --git a/objectivec/GPBWellKnownTypes.m b/objectivec/GPBWellKnownTypes.m
index ed798a2e..83b1c833 100644
--- a/objectivec/GPBWellKnownTypes.m
+++ b/objectivec/GPBWellKnownTypes.m
@@ -50,6 +50,15 @@ static int32_t SecondsAndNanosFromTimeIntervalSince1970(NSTimeInterval time,
int64_t *outSeconds) {
NSTimeInterval seconds;
NSTimeInterval nanos = modf(time, &seconds);
+
+ // Per Timestamp.proto, nanos is non-negative and "Negative second values with
+ // fractions must still have non-negative nanos values that count forward in
+ // time. Must be from 0 to 999,999,999 inclusive."
+ if (nanos < 0) {
+ --seconds;
+ nanos = 1.0 + nanos;
+ }
+
nanos *= 1e9;
*outSeconds = (int64_t)seconds;
return (int32_t)nanos;