firebase-kotlin-sdk icon indicating copy to clipboard operation
firebase-kotlin-sdk copied to clipboard

Inconsistent UserMetadata time values

Open chRyNaN opened this issue 3 years ago • 0 comments
trafficstars

Overview

The UserMetadata creationTime and lastSignInTime values are inconsistent between the different Kotlin targets. In particular, the iOS target produces results different from the Android and JS targets. The iOS target uses an epoch value of January 1st, 2001, where the Android and JS targets use the UNIX Epoch value of January 1, 1970. This causes issues when using the properties in common code, as each target will produce a different value.

Issue

The iOS source uses the timeIntervalSinceReferenceDate which produces seconds since a specific MacOS/iOS epoch value of January 1st, 2001. Here is the relevant code:

actual val creationTime: Double?
        get() = ios.creationDate?.timeIntervalSinceReferenceDate?.toDouble()
actual val lastSignInTime: Double?
        get() = ios.lastSignInDate?.timeIntervalSinceReferenceDate?.toDouble()

Solution

The solution is to simply convert the timeIntervalSinceReferenceDate usage in the iOS code to use timeIntervalSince1970 instead. The timeIntervalSince1970 property uses the UNIX Epoch value of January 1, 1970, which is the same epoch value that the Android and JS targets use. Here would be the updated relevant code:

actual val creationTime: Double?
        get() = ios.creationDate?.timeIntervalSince1970?.toDouble()
actual val lastSignInTime: Double?
        get() = ios.lastSignInDate?.timeIntervalSince1970?.toDouble()

Resources

  • https://en.wikipedia.org/wiki/Unix_time
  • https://developer.apple.com/documentation/foundation/nsdate/1417376-timeintervalsincereferencedate
  • https://developer.apple.com/documentation/foundation/nsdate/1407504-timeintervalsince1970
  • https://stackoverflow.com/a/33330705/1478764
  • https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/auth/UserMetadata#getLastSignInTimestamp()
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_ecmascript_epoch_and_timestamps
  • https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html

chRyNaN avatar May 10 '22 19:05 chRyNaN