What is this number supposed to be ?
I tested as if it was elapsed since 1970 (reference on Mac) in micro or nano seconds, but that does not give the expected date.
extension Date {
var millisecondsSince1970: Int64 {
Int64((self.timeIntervalSince1970 * 1000.0).rounded())
}
init(milliseconds: Int64) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)
}
init(microseconds: Int64) {
self = Date(timeIntervalSince1970: TimeInterval(microseconds) / 1000_000)
}
init(nanoseconds: Int64) {
self = Date(timeIntervalSince1970: TimeInterval(nanoseconds) / 1000_000_000)
}
}
print("micro", Date(microseconds: 565_599_639_274_028_992)) // micro 19893-02-20 02:54:34 +0000
print("nano", Date(nanoseconds : 565_599_639_274_028_992)) // nano 1987-12-04 07:00:39 +0000
Is the number a time since about 2000, expressed as nanoseconds ?
I tested this:
let aDateStr = "04/12/2018"
// Create Date Formatter
let aDateFormatter = DateFormatter()
aDateFormatter.dateFormat = "dd/MM/yy"
// Convert String to Date
let aDate = aDateFormatter.date(from: aDateStr)!
let elapsed : Double = 565_599_639_274_028_992 / 1000_000_000
let newDate = Date(timeInterval: -elapsed, since: aDate)
print("newdate", newDate)
and get:
newdate 2000-12-31 15:59:20 +0000
So, the Int is probably a time elapsed in micro seconds since 1/1/2001.
Then, to convert to date, just do the other way:
let refDateStr = "01/01/2001"
// Create Date Formatter
let aDateFormatter = DateFormatter()
aDateFormatter.dateFormat = "dd/MM/yy"
aDateFormatter.timeZone = TimeZone(abbreviation: "GMT")
// Convert String to Date
let refDate = aDateFormatter.date(from: refDateStr)!
let elapsed : Double = 565_599_639_274_028_992 / 1000_000_000
let newDate = Date(timeInterval: elapsed, since: refDate)
print("newDate", newDate)
And get:
newDate 2018-12-04 07:00:39 +0000
Which is the expected result, at 7 am, GMT.
Of course, need to set timezone appropriately.