timestamp_internal.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2019 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. #include "Firestore/core/src/timestamp_internal.h"
  17. #include "Firestore/core/include/firebase/firestore/firestore_errors.h"
  18. #include "Firestore/core/src/util/hashing.h"
  19. #include "Firestore/core/src/util/statusor.h"
  20. using firebase::firestore::util::Status;
  21. using firebase::firestore::util::StatusOr;
  22. namespace firebase {
  23. StatusOr<Timestamp> TimestampInternal::FromUntrustedTime(absl::Time time) {
  24. // Note `ToUnixSeconds` rounds towards negative infinity, this makes the
  25. // `nanos` calculated below always non-negative, meeting protobuf's
  26. // requirement.
  27. int64_t seconds = absl::ToUnixSeconds(time);
  28. int32_t nanos = static_cast<int32_t>((time - absl::FromUnixSeconds(seconds)) /
  29. absl::Nanoseconds(1));
  30. return FromUntrustedSecondsAndNanos(seconds, nanos);
  31. }
  32. StatusOr<Timestamp> TimestampInternal::FromUntrustedSecondsAndNanos(
  33. int64_t seconds, int32_t nanos) {
  34. // The Timestamp ctor will assert if we provide values outside the valid
  35. // range. However, since we're decoding, a single corrupt byte could cause
  36. // this to occur, so we'll verify the ranges before passing them in since we'd
  37. // rather not abort in these situations.
  38. if (seconds < Min().seconds()) {
  39. return Status(
  40. firestore::Error::kErrorInvalidArgument,
  41. "Invalid message: timestamp beyond the earliest supported date");
  42. } else if (Max().seconds() < seconds) {
  43. return Status(
  44. firestore::Error::kErrorInvalidArgument,
  45. "Invalid message: timestamp beyond the latest supported date");
  46. } else if (nanos < 0 || nanos > 999999999) {
  47. return Status(
  48. firestore::Error::kErrorInvalidArgument,
  49. "Invalid message: timestamp nanos must be between 0 and 999999999");
  50. }
  51. return Timestamp(seconds, nanos);
  52. }
  53. size_t TimestampInternal::Hash(const Timestamp& timestamp) {
  54. return firebase::firestore::util::Hash(timestamp.seconds(),
  55. timestamp.nanoseconds());
  56. }
  57. Timestamp TimestampInternal::Truncate(const Timestamp& timestamp) {
  58. int32_t truncated_nanos = timestamp.nanoseconds() / 1000 * 1000;
  59. return Timestamp(timestamp.seconds(), truncated_nanos);
  60. }
  61. } // namespace firebase