FIRCoreDiagnosticsDateFileStorage.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #import "FIRCDLibrary/FIRCoreDiagnosticsDateFileStorage.h"
  17. @interface FIRCoreDiagnosticsDateFileStorage ()
  18. @property(nonatomic, readonly) NSURL *fileURL;
  19. @end
  20. @implementation FIRCoreDiagnosticsDateFileStorage
  21. - (instancetype)initWithFileURL:(NSURL *)fileURL {
  22. if (fileURL == nil) {
  23. return nil;
  24. }
  25. self = [super init];
  26. if (self) {
  27. _fileURL = fileURL;
  28. }
  29. return self;
  30. }
  31. - (BOOL)setDate:(nullable NSDate *)date error:(NSError **)outError {
  32. NSString *stringToSave = @"";
  33. if (date != nil) {
  34. NSTimeInterval timestamp = [date timeIntervalSinceReferenceDate];
  35. stringToSave = [NSString stringWithFormat:@"%f", timestamp];
  36. }
  37. return [stringToSave writeToURL:self.fileURL
  38. atomically:YES
  39. encoding:NSUTF8StringEncoding
  40. error:outError];
  41. }
  42. - (nullable NSDate *)date {
  43. NSString *timestampString = [NSString stringWithContentsOfURL:self.fileURL
  44. encoding:NSUTF8StringEncoding
  45. error:nil];
  46. if (timestampString.length == 0) {
  47. return nil;
  48. }
  49. NSTimeInterval timestamp = timestampString.doubleValue;
  50. return [NSDate dateWithTimeIntervalSinceReferenceDate:timestamp];
  51. }
  52. @end