Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Etc/GMT时间转换为Swift Date()全局时间_Swift_Nsdate - Fatal编程技术网

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

将Etc/GMT时间转换为Swift Date()全局时间,swift,nsdate,Swift,Nsdate,我正在尝试将苹果收据上的日期转换为swift全球日期和时间。但我没有成功 购买时间(设备):2019-08-30 22:23:44美国/洛杉矶 购买时间(服务器):2019-08-31 05:23:44等/GMT 过期时间(服务器):2019-08-31 05:28:44等/GMT 当我使用date()获取日期时,它与我的设备和上面的日期和时间完全或几乎相差一个小时。我想将今天的Date()与过期时间进行比较 我使用了以下代码,但没有成功 extension Date { static

我正在尝试将苹果收据上的日期转换为swift全球日期和时间。但我没有成功

购买时间(设备):2019-08-30 22:23:44美国/洛杉矶

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

过期时间(服务器):2019-08-31 05:28:44等/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)!
    }
}

更新:

(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)
试用代码:

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)
(lldb)采购订单打印日期
▿ 2019-08-31 05:28:44 +0000
-参考日期:588922124.0
应用修复它,修复表达式为:
印刷品;日期
(lldb)采购订单打印日期。localUTC()
▿ 2019-09-01 03:07:45 +0000
-时间间隔自参考日期:589000065.0
应用修复它,修复表达式为:
印刷品;Date.localUTC()
(lldb)采购订单打印自过期日期
▿ 可选的
-一些:“2019-08-3105:28:44等/GMT”
应用修复它,修复表达式为:
印刷品;自过期
(lldb)采购订单打印日期()
▿ 2019-09-01 03:09:03 +0000
-时间间隔自参考日期:589000143.939402
应用修复它,修复表达式为:
印刷品;日期(
(lldb)
这里,

设备日期为8月31日星期六晚上8:mm。 购买日期为(持续时间为5分钟):
2019-08-31 05:23:44等/GMT
(对于设备,是8月31日星期六晚上8:mm) 过期日期为:
2019-08-31 05:28:44等/GMT
Swift日期为:~
2019-09-01 03:09:03

我想只要把时间转换成Swift
date()
global time,我们就可以很容易地比较日期了。但我无法将
2019-08-3105:28:44 Etc/GMT
转换为~
2019-09-0103: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
    }()
}


什么是
Etc/GMT
?可能是UTC@rmaddy,我不知道什么是Etc/GMT。刚刚输入了apple接收json对象。@LeoDabus,好的,我将把本地时间转换为UTC@LeoDabus,只想将
etc/GMT
时间转换为全局时间,如Swift
Date()
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"
}