将Etc/GMT时间转换成SwiftDate()全局TIme

Convert Etc/GMT time into Swift Date() global TIme

我正在尝试将日期从 Apple 收据转换为 swift 全球日期和时间。但是我没有成功,

购买时间(设备): 2019-08-30 22:23:44 America/Los_Angeles

购买时间(服务器): 2019-08-31 05:23:44 Etc/GMT

过期时间(服务器): 2019-08-31 05:28:44 Etc/GMT

当我使用 Date() 获取日期时,它与我的设备和上面的日期和时间完全或几乎相差几个小时。我想将今天 Date() 与过期时间进行比较。

我使用了下面的代码但没有成功,

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)

更新:

尝试过的代码:

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)!
    }
}

调试日志:

(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)

这里,

设备日期是 ~ 8:mm PM, Sat 31 August。 购买日期是(持续 5 分钟):2019-08-31 05:23:44 Etc/GMT(对于设备,它是 8 月 31 日星期六的 8:mm 下午) 到期日期为:2019-08-31 05:28:44 Etc/GMT Swift 日期是:~ 2019-09-01 03:09:03

我认为一旦 Etc/GMT 时间转换为 Swift Date() 全球时间,我们就可以轻松比较日期。但是我无法将 2019-08-31 05:28:44 Etc/GMT 转换为 ~ 2019-09-01 03:09:03。那么,如何转换呢?或者欢迎任何其他建议。

注意:'~'表示由于测试时间延迟几秒或几分钟。

无需手动解析您的日期字符串。您可以使用日期格式 "VV" 来解释与您的日期字符串关联的时区标识符:

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"
}