FSTTimestamp.mm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Core/FSTTimestamp.h"
  17. #include "Firestore/core/src/firebase/firestore/util/comparison.h"
  18. #import "Firestore/Source/Util/FSTAssert.h"
  19. using firebase::firestore::util::WrapCompare;
  20. NS_ASSUME_NONNULL_BEGIN
  21. static const int kNanosPerSecond = 1000000000;
  22. @implementation FSTTimestamp
  23. #pragma mark - Constructors
  24. + (instancetype)timestamp {
  25. return [FSTTimestamp timestampWithDate:[NSDate date]];
  26. }
  27. + (instancetype)timestampWithDate:(NSDate *)date {
  28. double secondsDouble;
  29. double fraction = modf(date.timeIntervalSince1970, &secondsDouble);
  30. // GCP Timestamps always have non-negative nanos.
  31. if (fraction < 0) {
  32. fraction += 1.0;
  33. secondsDouble -= 1.0;
  34. }
  35. int64_t seconds = (int64_t)secondsDouble;
  36. int32_t nanos = (int32_t)(fraction * kNanosPerSecond);
  37. return [[FSTTimestamp alloc] initWithSeconds:seconds nanos:nanos];
  38. }
  39. - (instancetype)initWithSeconds:(int64_t)seconds nanos:(int32_t)nanos {
  40. self = [super init];
  41. if (self) {
  42. FSTAssert(nanos >= 0, @"timestamp nanoseconds out of range: %d", nanos);
  43. FSTAssert(nanos < 1e9, @"timestamp nanoseconds out of range: %d", nanos);
  44. // Midnight at the beginning of 1/1/1 is the earliest timestamp Firestore supports.
  45. FSTAssert(seconds >= -62135596800L, @"timestamp seconds out of range: %lld", seconds);
  46. // This will break in the year 10,000.
  47. FSTAssert(seconds < 253402300800L, @"timestamp seconds out of range: %lld", seconds);
  48. _seconds = seconds;
  49. _nanos = nanos;
  50. }
  51. return self;
  52. }
  53. #pragma mark - NSObject methods
  54. - (BOOL)isEqual:(id)object {
  55. if (self == object) {
  56. return YES;
  57. }
  58. if (![object isKindOfClass:[FSTTimestamp class]]) {
  59. return NO;
  60. }
  61. return [self isEqualToTimestamp:(FSTTimestamp *)object];
  62. }
  63. - (NSUInteger)hash {
  64. return (NSUInteger)((self.seconds >> 32) ^ self.seconds ^ self.nanos);
  65. }
  66. - (NSString *)description {
  67. return [NSString
  68. stringWithFormat:@"<FSTTimestamp: seconds=%lld nanos=%d>", self.seconds, self.nanos];
  69. }
  70. /** Implements NSCopying without actually copying because timestamps are immutable. */
  71. - (id)copyWithZone:(NSZone *_Nullable)zone {
  72. return self;
  73. }
  74. #pragma mark - Public methods
  75. - (NSDate *)approximateDateValue {
  76. NSTimeInterval interval = (NSTimeInterval)self.seconds + ((NSTimeInterval)self.nanos) / 1e9;
  77. return [NSDate dateWithTimeIntervalSince1970:interval];
  78. }
  79. - (BOOL)isEqualToTimestamp:(FSTTimestamp *)other {
  80. return [self compare:other] == NSOrderedSame;
  81. }
  82. - (NSString *)ISO8601String {
  83. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  84. formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
  85. formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
  86. NSDate *secondsDate = [NSDate dateWithTimeIntervalSince1970:self.seconds];
  87. NSString *secondsString = [formatter stringFromDate:secondsDate];
  88. FSTAssert(secondsString.length == 19, @"Invalid ISO string: %@", secondsString);
  89. NSString *nanosString = [NSString stringWithFormat:@"%09d", self.nanos];
  90. return [NSString stringWithFormat:@"%@.%@Z", secondsString, nanosString];
  91. }
  92. - (NSComparisonResult)compare:(FSTTimestamp *)other {
  93. NSComparisonResult result = WrapCompare<int64_t>(self.seconds, other.seconds);
  94. if (result != NSOrderedSame) {
  95. return result;
  96. }
  97. return WrapCompare<int32_t>(self.nanos, other.nanos);
  98. }
  99. @end
  100. NS_ASSUME_NONNULL_END