gpt4 book ai didi

swift - 将 Etc/GMT 时间转换为 Swift Date() 全局时间

转载 作者:可可西里 更新时间:2023-11-01 01:06:55 30 4
gpt4 key购买 nike

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

购买时间(设备): 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 月 31 日星期六下午 8:00。购买日期是(持续 5 分钟):2019-08-31 05:23:44 Etc/GMT(对于设备,它是 8 月 31 日星期六下午 8:00)到期日期为:2019-08-31 05:28:44 Etc/GMTSwift 日期是:~ 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"
}

关于swift - 将 Etc/GMT 时间转换为 Swift Date() 全局时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57742681/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com