this test fail on the specific device which disabled "Date & Time -> 24-Hour Time" and location is Japan, and when I set the locale, it runs fine on that specific device to, and I am curious what makes this happen, so anybody know the reason please help
import XCTest
final class DateFormatterTest: XCTestCase {
func testDateFormatter() throws {
let sendTime = "2023-04-25T02:07:29"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
// dateFormatter.locale = Locale(identifier: "en_US_POSIX") // test fail until I set locale
let result = dateFormatter.date(from: sendTime)
XCTAssertNotNil(result)
}
}
This is a long-standing gotcha with DateFormatter
. I explain it in detail in QA1480 NSDateFormatter and Internet Dates.
IMPORTANT When you apply en_US_POSIX
, do it before setting the dateFormat
property.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"