FSTTimestamp.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <Foundation/Foundation.h>
  17. NS_ASSUME_NONNULL_BEGIN
  18. /**
  19. * An FSTTimestamp represents an absolute time from the backend at up to nanosecond precision.
  20. * An FSTTimestamp is represented in terms of UTC and does not have an associated timezone.
  21. */
  22. @interface FSTTimestamp : NSObject <NSCopying>
  23. - (instancetype)init NS_UNAVAILABLE;
  24. /**
  25. * Creates a new timestamp.
  26. *
  27. * @param seconds the number of seconds since epoch.
  28. * @param nanos the number of nanoseconds after the seconds.
  29. */
  30. - (instancetype)initWithSeconds:(int64_t)seconds nanos:(int32_t)nanos NS_DESIGNATED_INITIALIZER;
  31. /** Creates a new timestamp with the current date / time. */
  32. + (instancetype)timestamp;
  33. /** Creates a new timestamp from the given date. */
  34. + (instancetype)timestampWithDate:(NSDate *)date;
  35. /** Returns a new NSDate corresponding to this timestamp. This may lose precision. */
  36. - (NSDate *)approximateDateValue;
  37. /**
  38. * Converts the given date to a an ISO 8601 timestamp string, useful for rendering in JSON.
  39. *
  40. * ISO 8601 dates times in UTC look like this: "1912-04-14T23:40:00.000000000Z".
  41. *
  42. * @see http://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format
  43. */
  44. - (NSString *)ISO8601String;
  45. - (NSComparisonResult)compare:(FSTTimestamp *)other;
  46. /**
  47. * Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z.
  48. * Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.
  49. */
  50. @property(nonatomic, assign, readonly) int64_t seconds;
  51. /**
  52. * Non-negative fractions of a second at nanosecond resolution. Negative second values with
  53. * fractions must still have non-negative nanos values that count forward in time.
  54. * Must be from 0 to 999,999,999 inclusive.
  55. */
  56. @property(nonatomic, assign, readonly) int32_t nanos;
  57. @end
  58. NS_ASSUME_NONNULL_END