【问题标题】:Convert Etc/GMT time into Swift Date() global TIme将 Etc/GMT 时间转换为 Swift Date() 全球时间
【发布时间】:2019-09-01 01:47:35
【问题描述】:

我正在尝试将日期从苹果收据转换为快速的全球日期和时间。但是我没有成功,

购买时间(设备): 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。那么,如何转换呢?或欢迎任何其他建议。

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

【问题讨论】:

  • 什么是Etc/GMT
  • 可能是UTC
  • @rmaddy,我不知道什么是 Etc/GMT。刚刚收到苹果收据 json 对象。
  • @LeoDabus,好的,我会将当地时间转换为 UTC
  • @LeoDabus,只是想将 etc/GMT 时间转换为全球时间,如 Swift Date()

标签: swift nsdate


【解决方案1】:

无需手动解析您的日期字符串。您可以使用日期格式"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"
}

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 2013-10-29
    • 2021-10-02
    • 2018-06-28
    • 2017-08-26
    • 2012-12-25
    • 1970-01-01
    相关资源
    最近更新 更多