diff options
author | Konstantin Varlamov <var-const@users.noreply.github.com> | 2018-02-20 17:33:57 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-20 17:33:57 -0500 |
commit | 14ea068f5fd03a658017f8472a4078a727fabc3a (patch) | |
tree | 56696c49546e2147ec1b8ebdeba24dd292f0c951 /Firestore/Source/Core | |
parent | de00de25deebc35c4c7167135d5b1f29a7d9fe6f (diff) |
Make FSTTimestamp into a public Firestore class (#698)
- FSTTimestamp is now FIRTimestamp, under Firestore/Source/{Public,API}. This is a temporary solution; eventually, FIRTimestamp is supposed to live somewhere under Firebase;
- move most internal Timestamp methods to the public header (the only exception is ISOString).
Diffstat (limited to 'Firestore/Source/Core')
-rw-r--r-- | Firestore/Source/Core/FSTSnapshotVersion.h | 6 | ||||
-rw-r--r-- | Firestore/Source/Core/FSTSnapshotVersion.mm | 8 | ||||
-rw-r--r-- | Firestore/Source/Core/FSTTimestamp.h | 72 | ||||
-rw-r--r-- | Firestore/Source/Core/FSTTimestamp.mm | 125 |
4 files changed, 7 insertions, 204 deletions
diff --git a/Firestore/Source/Core/FSTSnapshotVersion.h b/Firestore/Source/Core/FSTSnapshotVersion.h index b72e4a2..8649d40 100644 --- a/Firestore/Source/Core/FSTSnapshotVersion.h +++ b/Firestore/Source/Core/FSTSnapshotVersion.h @@ -18,7 +18,7 @@ NS_ASSUME_NONNULL_BEGIN -@class FSTTimestamp; +@class FIRTimestamp; /** * A version of a document in Firestore. This corresponds to the version timestamp, such as @@ -30,13 +30,13 @@ NS_ASSUME_NONNULL_BEGIN + (instancetype)noVersion; /** Creates a new version representing the given timestamp. */ -+ (instancetype)versionWithTimestamp:(FSTTimestamp *)timestamp; ++ (instancetype)versionWithTimestamp:(FIRTimestamp *)timestamp; - (instancetype)init NS_UNAVAILABLE; - (NSComparisonResult)compare:(FSTSnapshotVersion *)other; -@property(nonatomic, strong, readonly) FSTTimestamp *timestamp; +@property(nonatomic, strong, readonly) FIRTimestamp *timestamp; @end diff --git a/Firestore/Source/Core/FSTSnapshotVersion.mm b/Firestore/Source/Core/FSTSnapshotVersion.mm index 980ae52..58b2be4 100644 --- a/Firestore/Source/Core/FSTSnapshotVersion.mm +++ b/Firestore/Source/Core/FSTSnapshotVersion.mm @@ -16,7 +16,7 @@ #import "Firestore/Source/Core/FSTSnapshotVersion.h" -#import "Firestore/Source/Core/FSTTimestamp.h" +#import "FIRTimestamp.h" NS_ASSUME_NONNULL_BEGIN @@ -26,17 +26,17 @@ NS_ASSUME_NONNULL_BEGIN static FSTSnapshotVersion *min; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - FSTTimestamp *timestamp = [[FSTTimestamp alloc] initWithSeconds:0 nanos:0]; + FIRTimestamp *timestamp = [[FIRTimestamp alloc] initWithSeconds:0 nanoseconds:0]; min = [FSTSnapshotVersion versionWithTimestamp:timestamp]; }); return min; } -+ (instancetype)versionWithTimestamp:(FSTTimestamp *)timestamp { ++ (instancetype)versionWithTimestamp:(FIRTimestamp *)timestamp { return [[FSTSnapshotVersion alloc] initWithTimestamp:timestamp]; } -- (instancetype)initWithTimestamp:(FSTTimestamp *)timestamp { +- (instancetype)initWithTimestamp:(FIRTimestamp *)timestamp { self = [super init]; if (self) { _timestamp = timestamp; diff --git a/Firestore/Source/Core/FSTTimestamp.h b/Firestore/Source/Core/FSTTimestamp.h deleted file mode 100644 index f86779d..0000000 --- a/Firestore/Source/Core/FSTTimestamp.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2017 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import <Foundation/Foundation.h> - -NS_ASSUME_NONNULL_BEGIN - -/** - * An FSTTimestamp represents an absolute time from the backend at up to nanosecond precision. - * An FSTTimestamp is represented in terms of UTC and does not have an associated timezone. - */ -@interface FSTTimestamp : NSObject <NSCopying> - -- (instancetype)init NS_UNAVAILABLE; - -/** - * Creates a new timestamp. - * - * @param seconds the number of seconds since epoch. - * @param nanos the number of nanoseconds after the seconds. - */ -- (instancetype)initWithSeconds:(int64_t)seconds nanos:(int32_t)nanos NS_DESIGNATED_INITIALIZER; - -/** Creates a new timestamp with the current date / time. */ -+ (instancetype)timestamp; - -/** Creates a new timestamp from the given date. */ -+ (instancetype)timestampWithDate:(NSDate *)date; - -/** Returns a new NSDate corresponding to this timestamp. This may lose precision. */ -- (NSDate *)approximateDateValue; - -/** - * Converts the given date to a an ISO 8601 timestamp string, useful for rendering in JSON. - * - * ISO 8601 dates times in UTC look like this: "1912-04-14T23:40:00.000000000Z". - * - * @see http://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format - */ -- (NSString *)ISO8601String; - -- (NSComparisonResult)compare:(FSTTimestamp *)other; - -/** - * Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. - * Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive. - */ -@property(nonatomic, assign, readonly) int64_t seconds; - -/** - * Non-negative fractions of a second at nanosecond resolution. 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. - */ -@property(nonatomic, assign, readonly) int32_t nanos; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Core/FSTTimestamp.mm b/Firestore/Source/Core/FSTTimestamp.mm deleted file mode 100644 index d2b492a..0000000 --- a/Firestore/Source/Core/FSTTimestamp.mm +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright 2017 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import "Firestore/Source/Core/FSTTimestamp.h" - -#include "Firestore/core/src/firebase/firestore/util/comparison.h" - -#import "Firestore/Source/Util/FSTAssert.h" - -using firebase::firestore::util::WrapCompare; - -NS_ASSUME_NONNULL_BEGIN - -static const int kNanosPerSecond = 1000000000; - -@implementation FSTTimestamp - -#pragma mark - Constructors - -+ (instancetype)timestamp { - return [FSTTimestamp timestampWithDate:[NSDate date]]; -} - -+ (instancetype)timestampWithDate:(NSDate *)date { - double secondsDouble; - double fraction = modf(date.timeIntervalSince1970, &secondsDouble); - // GCP Timestamps always have non-negative nanos. - if (fraction < 0) { - fraction += 1.0; - secondsDouble -= 1.0; - } - int64_t seconds = (int64_t)secondsDouble; - int32_t nanos = (int32_t)(fraction * kNanosPerSecond); - return [[FSTTimestamp alloc] initWithSeconds:seconds nanos:nanos]; -} - -- (instancetype)initWithSeconds:(int64_t)seconds nanos:(int32_t)nanos { - self = [super init]; - if (self) { - FSTAssert(nanos >= 0, @"timestamp nanoseconds out of range: %d", nanos); - FSTAssert(nanos < 1e9, @"timestamp nanoseconds out of range: %d", nanos); - // Midnight at the beginning of 1/1/1 is the earliest timestamp Firestore supports. - FSTAssert(seconds >= -62135596800L, @"timestamp seconds out of range: %lld", seconds); - // This will break in the year 10,000. - FSTAssert(seconds < 253402300800L, @"timestamp seconds out of range: %lld", seconds); - - _seconds = seconds; - _nanos = nanos; - } - return self; -} - -#pragma mark - NSObject methods - -- (BOOL)isEqual:(id)object { - if (self == object) { - return YES; - } - if (![object isKindOfClass:[FSTTimestamp class]]) { - return NO; - } - return [self isEqualToTimestamp:(FSTTimestamp *)object]; -} - -- (NSUInteger)hash { - return (NSUInteger)((self.seconds >> 32) ^ self.seconds ^ self.nanos); -} - -- (NSString *)description { - return [NSString - stringWithFormat:@"<FSTTimestamp: seconds=%lld nanos=%d>", self.seconds, self.nanos]; -} - -/** Implements NSCopying without actually copying because timestamps are immutable. */ -- (id)copyWithZone:(NSZone *_Nullable)zone { - return self; -} - -#pragma mark - Public methods - -- (NSDate *)approximateDateValue { - NSTimeInterval interval = (NSTimeInterval)self.seconds + ((NSTimeInterval)self.nanos) / 1e9; - return [NSDate dateWithTimeIntervalSince1970:interval]; -} - -- (BOOL)isEqualToTimestamp:(FSTTimestamp *)other { - return [self compare:other] == NSOrderedSame; -} - -- (NSString *)ISO8601String { - NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; - formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss"; - formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; - NSDate *secondsDate = [NSDate dateWithTimeIntervalSince1970:self.seconds]; - NSString *secondsString = [formatter stringFromDate:secondsDate]; - FSTAssert(secondsString.length == 19, @"Invalid ISO string: %@", secondsString); - - NSString *nanosString = [NSString stringWithFormat:@"%09d", self.nanos]; - return [NSString stringWithFormat:@"%@.%@Z", secondsString, nanosString]; -} - -- (NSComparisonResult)compare:(FSTTimestamp *)other { - NSComparisonResult result = WrapCompare<int64_t>(self.seconds, other.seconds); - if (result != NSOrderedSame) { - return result; - } - return WrapCompare<int32_t>(self.nanos, other.nanos); -} - -@end - -NS_ASSUME_NONNULL_END |