[英]how to get ISO8601 format in swift 4?
I am using amazon web service for items search. 我正在使用亚马逊网络服务进行商品搜索。 i can able get time stamp value and i converted into iso format. 我可以获取时间戳记值并将其转换为ISO格式。 but in console output it shows Timestamp is invalid. 但在控制台输出中,它显示时间戳无效。 Reason: Must be in ISO8601 format 原因:必须为ISO8601格式
Here is my full console output value : status code is: Optional(400) XML: . 这是我完整的控制台输出值: 状态代码是:Optional(400)XML:。 InvalidParameterValue
Value 2018-06-15T18%3A43%3A52Z for parameter Timestamp is invalid. 参数Timestamp的InvalidParameterValue
值2018-06-15T18%3A43%3A52Z无效。 Reason: Must be in ISO8601 format.66a698a6-a967-4b34-b15a-94780f9287ce 原因:必须为ISO8601格式。66a698a6-a967-4b34-b15a-94780f9287ce
here is the coed i tried for : 这是我尝试过的男女同校:
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
}
} }
The error being: 错误是:
Optional(400) XML: . InvalidParameterValue Value 2018-06-15T18%3A43%3A52Z for parameter Timestamp is invalid. Reason: Must be in ISO8601 format.
The issue is about the :
percent escaped (transformed into %3A
), because 2018-06-15T18:43:52Z
seems valid in ISO8601 format while 2018-06-15T18%3A43%3A52Z
doesn't. 问题是关于:
逸出百分比(已转换为%3A
),因为2018-06-15T18:43:52Z
在ISO8601格式中似乎有效,而2018-06-15T18%3A43%3A52Z
无效。
You are adding yourself the percent escape in parameters
: 您正在为自己添加parameters
逸出百分比:
So: 所以:
"Timestamp": urlEncode(timeStamp.string(from: date))
=> =>
"Timestamp": timeStamp.string(from: date)
Also, note that in iOS there is a ISO8601DateFormatter
available from iOS10+ that could avoid you writing the format yourself. 另外,请注意,在iOS ISO8601DateFormatter
,iOS10 +提供了ISO8601DateFormatter,可以避免您自己编写格式。
Try 尝试
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.