簡體   English   中英

如何快速獲得ISO8601格式?

[英]how to get ISO8601 format in swift 4?

我正在使用亞馬遜網絡服務進行商品搜索。 我可以獲取時間戳記值並將其轉換為ISO格式。 但在控制台輸出中,它顯示時間戳無效。 原因:必須為ISO8601格式

這是我完整的控制台輸出值: 狀態代碼是:Optional(400)XML:。 參數Timestamp的InvalidParameterValue值2018-06-15T18%3A43%3A52Z無效。 原因:必須為ISO8601格式。66a698a6-a967-4b34-b15a-94780f9287ce

這是我嘗試過的男女同校:

    public func getSearchItem(searchKeyword: String) -> [String:AnyObject]{
      let timeStamp = getTimestamp()

    let dateFormatter = DateFormatter()
    dateFormatter.timeZone = TimeZone(identifier: "IST")
    let tempLocale = dateFormatter.locale // save locale temporarily
    dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    let date = dateFormatter.date(from: timeStamp.string(from: Date()))!
    dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
    dateFormatter.locale = tempLocale // reset the locale
    let dateString = dateFormatter.string(from: date)
    print("EXACT_DATE : \(dateString)")

    let url = "http://webservices.amazon.in/onca/xml"
    let parameters: Parameters = ["Service": "AWSECommerceService",
                                  "Operation": "ItemSearch",
                                  "ResponseGroup": "Images,ItemAttributes",
                                  "SearchIndex":"All",
                                  "Keywords": searchKeyword,
                                  "Timestamp": urlEncode(timeStamp.string(from: date)),
                                  "AWSAccessKeyId": urlEncode(ViewController.kAmazonAccessID),
                                  "AssociateTag": urlEncode(ViewController.kAmazonAssociateTag)
                                 ]



    let signedParams = signedParametersForParameters(parameters: parameters as! [String : String])

    Alamofire.request(url, method: .get, parameters: signedParams, encoding: URLEncoding.default)
        .responseString { response in
            print(" - API url: \(String(describing: response.request!))")   // original url request
            var statusCode = response.response?.statusCode

            switch response.result {
            case .success:
                print("status code is: \(String(describing: statusCode))")
                if let string = response.result.value {
                    print("XML: \(string)")
                }
            case .failure(let error):
                statusCode = error._code // statusCode private
                print("status code is: \(String(describing: statusCode))")
                print(error)
            }
    }


    return parameters as [String : AnyObject]
}

func getTimestamp() -> DateFormatter{
    var timestampFormatter = DateFormatter()
    timestampFormatter = DateFormatter()
    timestampFormatter.dateFormat = AWSDateISO8601DateFormat1
    timestampFormatter.timeZone = TimeZone(identifier: "IST")
    timestampFormatter.locale = Locale(identifier: "en_US_POSIX")
    return timestampFormatter
}

}

錯誤是:

Optional(400) XML: . InvalidParameterValue Value 2018-06-15T18%3A43%3A52Z for parameter Timestamp is invalid. Reason: Must be in ISO8601 format.

問題是關於:逸出百分比(已轉換為%3A ),因為2018-06-15T18:43:52Z在ISO8601格式中似乎有效,而2018-06-15T18%3A43%3A52Z無效。

您正在為自己添加parameters逸出百分比:

所以:

"Timestamp": urlEncode(timeStamp.string(from: date))

=>

"Timestamp": timeStamp.string(from: date)

另外,請注意,在iOS ISO8601DateFormatter ,iOS10 +提供了ISO8601DateFormatter,可以避免您自己編寫格式。

嘗試

func convertDateToDateString(date: Date, format: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_EN")
    dateFormatter.dateFormat = format
    dateFormatter.calendar = Calendar(identifier: .iso8601)

    let resultDate = dateFormatter.string(from: date)
    return resultDate
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM