Consider the following code:
let timestamp = Timestamp(seconds: 171475088, nanoseconds: 330370000)
let dateValue = timestamp.dateValue()
let newTimeStamp = Timestamp(date: timestamp.dateValue())
let newDateValue = newTimeStamp.dateValue()
print("timestamp: (timestamp)")
print("dateValue: (dateValue)")
print("newTimeStamp: (newTimeStamp)")
print("newDateValue: (newDateValue)")
This prints:
timestamp: <FIRTimestamp: seconds=171475088 nanoseconds=330370000>
dateValue: 1975-06-08 15:58:08 +0000
newTimeStamp: <FIRTimestamp: seconds=171475088 nanoseconds=330369949>
newDateValue: 1975-06-08 15:58:08 +0000
I am loosing 51 nanoseconds. This may not seem like a lot but it affects how we compare dates in queries.
The specific use case is I am trying to store a lastModifiedDate on a document every time I make a change. I can create a timestamp from a date but the problem is I want to store this date in UserDefaults so that next time I launch my app I can retrieve it and only fetch newer documents. I cannot store timestamps in UserDefaults. When I do store the Swift Date in user defaults and get it later and turn it back into a timestamp for the isGreaterThan comparison, it is different and the comparison fails. So outside of creating a string representation of the timestamp does anyone have any other suggestions?