FIRTimestamp.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/API/FIRTimestamp+Internal.h"
  17. NS_ASSUME_NONNULL_BEGIN
  18. static const int kNanosPerSecond = 1000000000;
  19. @implementation FIRTimestamp (Internal)
  20. #pragma mark - Internal public methods
  21. - (NSString *)ISO8601String {
  22. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  23. formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
  24. formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
  25. NSDate *secondsDate = [NSDate dateWithTimeIntervalSince1970:self.seconds];
  26. NSString *secondsString = [formatter stringFromDate:secondsDate];
  27. if (secondsString.length != 19) {
  28. [NSException raise:@"Invalid ISO string" format:@"Invalid ISO string: %@", secondsString];
  29. }
  30. NSString *nanosString = [NSString stringWithFormat:@"%09d", self.nanoseconds];
  31. return [NSString stringWithFormat:@"%@.%@Z", secondsString, nanosString];
  32. }
  33. @end
  34. @implementation FIRTimestamp
  35. #pragma mark - Constructors
  36. + (instancetype)timestampWithDate:(NSDate *)date {
  37. double secondsDouble;
  38. double fraction = modf(date.timeIntervalSince1970, &secondsDouble);
  39. // GCP Timestamps always have non-negative nanos.
  40. if (fraction < 0) {
  41. fraction += 1.0;
  42. secondsDouble -= 1.0;
  43. }
  44. int64_t seconds = (int64_t)secondsDouble;
  45. int32_t nanos = (int32_t)(fraction * kNanosPerSecond);
  46. return [[FIRTimestamp alloc] initWithSeconds:seconds nanoseconds:nanos];
  47. }
  48. + (instancetype)timestampWithSeconds:(int64_t)seconds nanoseconds:(int32_t)nanoseconds {
  49. return [[FIRTimestamp alloc] initWithSeconds:seconds nanoseconds:nanoseconds];
  50. }
  51. + (instancetype)timestamp {
  52. return [FIRTimestamp timestampWithDate:[NSDate date]];
  53. }
  54. - (instancetype)initWithSeconds:(int64_t)seconds nanoseconds:(int32_t)nanoseconds {
  55. self = [super init];
  56. if (self) {
  57. if (nanoseconds < 0) {
  58. [NSException raise:@"Invalid timestamp"
  59. format:@"Timestamp nanoseconds out of range: %d", nanoseconds];
  60. }
  61. if (nanoseconds >= 1e9) {
  62. [NSException raise:@"Invalid timestamp"
  63. format:@"Timestamp nanoseconds out of range: %d", nanoseconds];
  64. }
  65. // Midnight at the beginning of 1/1/1 is the earliest timestamp supported.
  66. if (seconds < -62135596800L) {
  67. [NSException raise:@"Invalid timestamp"
  68. format:@"Timestamp seconds out of range: %lld", seconds];
  69. }
  70. // This will break in the year 10,000.
  71. if (seconds >= 253402300800L) {
  72. [NSException raise:@"Invalid timestamp"
  73. format:@"Timestamp seconds out of range: %lld", seconds];
  74. }
  75. _seconds = seconds;
  76. _nanoseconds = nanoseconds;
  77. }
  78. return self;
  79. }
  80. #pragma mark - NSObject methods
  81. - (BOOL)isEqual:(id)object {
  82. if (self == object) {
  83. return YES;
  84. }
  85. if (![object isKindOfClass:[FIRTimestamp class]]) {
  86. return NO;
  87. }
  88. return [self isEqualToTimestamp:(FIRTimestamp *)object];
  89. }
  90. - (NSUInteger)hash {
  91. return (NSUInteger)((self.seconds >> 32) ^ self.seconds ^ self.nanoseconds);
  92. }
  93. - (NSString *)description {
  94. return [NSString stringWithFormat:@"<FIRTimestamp: seconds=%lld nanoseconds=%d>", self.seconds,
  95. self.nanoseconds];
  96. }
  97. /** Implements NSCopying without actually copying because timestamps are immutable. */
  98. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  99. return self;
  100. }
  101. #pragma mark - Public methods
  102. - (NSDate *)dateValue {
  103. NSTimeInterval interval = (NSTimeInterval)self.seconds + ((NSTimeInterval)self.nanoseconds) / 1e9;
  104. return [NSDate dateWithTimeIntervalSince1970:interval];
  105. }
  106. - (NSComparisonResult)compare:(FIRTimestamp *)other {
  107. if (self.seconds < other.seconds) {
  108. return NSOrderedAscending;
  109. } else if (self.seconds > other.seconds) {
  110. return NSOrderedDescending;
  111. }
  112. if (self.nanoseconds < other.nanoseconds) {
  113. return NSOrderedAscending;
  114. } else if (self.nanoseconds > other.nanoseconds) {
  115. return NSOrderedDescending;
  116. }
  117. return NSOrderedSame;
  118. }
  119. #pragma mark - Private methods
  120. - (BOOL)isEqualToTimestamp:(FIRTimestamp *)other {
  121. return [self compare:other] == NSOrderedSame;
  122. }
  123. @end
  124. NS_ASSUME_NONNULL_END