1// Protocol Buffers - Google's data interchange format 2// Copyright 2015 Google Inc. All rights reserved. 3// https://developers.google.com/protocol-buffers/ 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions are 7// met: 8// 9// * Redistributions of source code must retain the above copyright 10// notice, this list of conditions and the following disclaimer. 11// * Redistributions in binary form must reproduce the above 12// copyright notice, this list of conditions and the following disclaimer 13// in the documentation and/or other materials provided with the 14// distribution. 15// * Neither the name of Google Inc. nor the names of its 16// contributors may be used to endorse or promote products derived from 17// this software without specific prior written permission. 18// 19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31// Importing sources here to force the linker to include our category methods in 32// the static library. If these were compiled separately, the category methods 33// below would be stripped by the linker. 34 35#import "google/protobuf/Timestamp.pbobjc.m" 36#import "google/protobuf/Duration.pbobjc.m" 37#import "GPBWellKnownTypes.h" 38 39static NSTimeInterval TimeIntervalSince1970FromSecondsAndNanos(int64_t seconds, 40 int32_t nanos) { 41 return seconds + (NSTimeInterval)nanos / 1e9; 42} 43 44static int32_t SecondsAndNanosFromTimeIntervalSince1970(NSTimeInterval time, 45 int64_t *outSeconds) { 46 NSTimeInterval seconds; 47 NSTimeInterval nanos = modf(time, &seconds); 48 nanos *= 1e9; 49 *outSeconds = (int64_t)seconds; 50 return (int32_t)nanos; 51} 52 53@implementation GPBTimestamp (GBPWellKnownTypes) 54 55- (instancetype)initWithDate:(NSDate *)date { 56 return [self initWithTimeIntervalSince1970:date.timeIntervalSince1970]; 57} 58 59- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { 60 if ((self = [super init])) { 61 int64_t seconds; 62 int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970( 63 timeIntervalSince1970, &seconds); 64 self.seconds = seconds; 65 self.nanos = nanos; 66 } 67 return self; 68} 69 70- (NSDate *)date { 71 return [NSDate dateWithTimeIntervalSince1970:self.timeIntervalSince1970]; 72} 73 74- (void)setDate:(NSDate *)date { 75 self.timeIntervalSince1970 = date.timeIntervalSince1970; 76} 77 78- (NSTimeInterval)timeIntervalSince1970 { 79 return TimeIntervalSince1970FromSecondsAndNanos(self.seconds, self.nanos); 80} 81 82- (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { 83 int64_t seconds; 84 int32_t nanos = 85 SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds); 86 self.seconds = seconds; 87 self.nanos = nanos; 88} 89 90@end 91 92@implementation GPBDuration (GBPWellKnownTypes) 93 94- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { 95 if ((self = [super init])) { 96 int64_t seconds; 97 int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970( 98 timeIntervalSince1970, &seconds); 99 self.seconds = seconds; 100 self.nanos = nanos; 101 } 102 return self; 103} 104 105- (NSTimeInterval)timeIntervalSince1970 { 106 return TimeIntervalSince1970FromSecondsAndNanos(self.seconds, self.nanos); 107} 108 109- (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { 110 int64_t seconds; 111 int32_t nanos = 112 SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds); 113 self.seconds = seconds; 114 self.nanos = nanos; 115} 116 117@end 118