Reputation:
I am trying to convert date from apple receipt to swift global date and time. But I am not succeeded,
Purchased time (device): 2019-08-30 22:23:44 America/Los_Angeles
Purchased time (server): 2019-08-31 05:23:44 Etc/GMT
Expire time (server): 2019-08-31 05:28:44 Etc/GMT
When I get date by using Date()
, it's completely or almost some hour difference than my device & those date&time above. I wanted to compare today Date()
into expire time.
I used below codes but not success,
extension Date {
static func fromAppleServer(dateString: String) -> Date? {
let seperated = dateString.components(separatedBy: " ")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(identifier: seperated.last!)
let dateObject = dateFormatter.date(from: (seperated.dropLast()).joined(separator: " "))
return dateObject
}
static func localUTC() -> Date {
let date = Date()
print("Date before conversion: \(date)")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let string = dateFormatter.string(from: date)
dateFormatter.timeZone = TimeZone(identifier: "America/Los_Angeles")
return dateFormatter.date(from: string)!
}
}
let appleDate = Date.fromAppleServer(dateString: "2019-08-30 22:29:02 America/Los_Angeles") //used 2019-08-31 05:23:44 Etc/GMT too
let localUTC = Date.localUTC()
print("Apple Server: ",appleDate) //Always 2019-08-31 05:29:02 +0000
print("Local UTC: ",localUTC)
Update:
Tried code:
extension Date{
static func fromAppleServer(dateString: String) -> Date?{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
let dateObject = dateFormatter.date(from: dateString)
return dateObject
}
static func localUTC() -> Date{
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
let string = dateFormatter.string(from: date)
dateFormatter.timeZone = TimeZone(identifier: "UTC")
return dateFormatter.date(from: string)!
}
}
Debugging Log:
(lldb) po print date
▿ 2019-08-31 05:28:44 +0000
- timeIntervalSinceReferenceDate : 588922124.0
Fix-it applied, fixed expression was:
print; date
(lldb) po print Date.localUTC()
▿ 2019-09-01 03:07:45 +0000
- timeIntervalSinceReferenceDate : 589000065.0
Fix-it applied, fixed expression was:
print; Date.localUTC()
(lldb) po print self.expiryDate
▿ Optional<String>
- some : "2019-08-31 05:28:44 Etc/GMT"
Fix-it applied, fixed expression was:
print; self.expiryDate
(lldb) po print Date()
▿ 2019-09-01 03:09:03 +0000
- timeIntervalSinceReferenceDate : 589000143.939402
Fix-it applied, fixed expression was:
print; Date()
(lldb)
Here,
Device date is ~ 8:mm PM, Sat 31 August
.
Purchased date is(5 mins duration):2019-08-31 05:23:44 Etc/GMT
(For device it's 8:mm PM Sat 31 August)
Expire date is: 2019-08-31 05:28:44 Etc/GMT
Swift Date is:~ 2019-09-01 03:09:03
I thought we can easily compare date once Etc/GMT
time is converted into Swift Date()
global time. But I can't convert 2019-08-31 05:28:44 Etc/GMT
to ~ 2019-09-01 03:09:03
. So, how to convert it? or Any other suggestions welcome.
Note: '~' indicates some seconds or minutes delay because of testing time.
Upvotes: 3
Views: 1462
Reputation: 236458
No need to manually parse your date string. You can use date format "VV"
to interprete the timezone identifier associated with your date string:
extension Formatter {
static let customDate: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
return formatter
}()
}
let dateString = "2019-08-31 05:23:44 Etc/GMT" // or "2019-08-30 22:23:44 America/Los_Angeles"
if let date = Formatter.customDate.date(from: dateString) {
print(date) // "2019-08-31 05:23:44 +0000\n"
print(date.description(with: .current)) // "Saturday, August 31, 2019 at 2:28:44 AM Brasilia Standard Time\n"
}
Upvotes: 4