FClock.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "FirebaseDatabase/Sources/FClock.h"
  17. @implementation FSystemClock
  18. - (NSTimeInterval)currentTime {
  19. return [[NSDate date] timeIntervalSince1970];
  20. }
  21. + (FSystemClock *)clock {
  22. static dispatch_once_t onceToken;
  23. static FSystemClock *clock;
  24. dispatch_once(&onceToken, ^{
  25. clock = [[FSystemClock alloc] init];
  26. });
  27. return clock;
  28. }
  29. @end
  30. @interface FOffsetClock ()
  31. @property(nonatomic, strong) id<FClock> clock;
  32. @property(nonatomic) NSTimeInterval offset;
  33. @end
  34. @implementation FOffsetClock
  35. - (NSTimeInterval)currentTime {
  36. return [self.clock currentTime] + self.offset;
  37. }
  38. - (id)initWithClock:(id<FClock>)clock offset:(NSTimeInterval)offset {
  39. self = [super init];
  40. if (self != nil) {
  41. self->_clock = clock;
  42. self->_offset = offset;
  43. }
  44. return self;
  45. }
  46. @end