aboutsummaryrefslogtreecommitdiff
path: root/objectivec/GPBWellKnownTypes.m
diff options
context:
space:
mode:
authorThomas Van Lenten <thomasvl@google.com>2017-01-12 15:39:53 -0500
committerThomas Van Lenten <thomasvl@google.com>2017-01-12 15:46:50 -0500
commitadcccd0f8161394f61e39fd9c26eada35e091a8f (patch)
treea5151cc514710a1b7bd40bd85732ec606f71a91e /objectivec/GPBWellKnownTypes.m
parentfeb78fb293ac891baa4a25047581f34589f33a2f (diff)
downloadprotobuf-adcccd0f8161394f61e39fd9c26eada35e091a8f.tar.gz
protobuf-adcccd0f8161394f61e39fd9c26eada35e091a8f.tar.bz2
protobuf-adcccd0f8161394f61e39fd9c26eada35e091a8f.zip
Fix Timestamps with dates before the Unix epoch that contain fractional seconds.
The Timestamp proto does not allow for negative nanos fields, so the seconds must be shifted and a positive nanos then applied.
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;