swiftlang/swift-corelibs-foundation

DateFormatter.timeStyle == .none doesn't format Raw Date

Closed this issue · 5 comments

DateFormatter.timeStyle == .none doesn't format Date

import Foundation

let dateString = "2024-07-31"
let formatter = DateFormatter()

formatter.dateStyle = .short
formatter.timeStyle = .none

print(formatter.date(from: dateString))

Result

macOS: 2024-07-30 15:00:00 +0000
Linux: nil

Expect

macOS: 2024-07-30 15:00:00 +0000
Linux: 2024-07-30 15:00:00 +0000

just tested this, on macOS it returns nil aswell..

$ swift --version
swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

$ uname -a
Darwin JPXNGP6J95 23.6.0 Darwin Kernel Version 23.6.0: Wed Jul 31 20:48:52 PDT 2024; root:xnu-10063.141.1.700.5~1/RELEASE_ARM64_T6020 arm64

Result(Swift 6, macOS 15)

Optional(2024-07-30 15:00:00 +0000)
$ swift --version
swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.11 clang-1600.0.26.2)
Target: arm64-apple-macosx15.0

$ uname -a
Darwin MacBookAir.localdomain 24.1.0 Darwin Kernel Version 24.1.0: Tue Sep 17 07:48:19 PDT 2024; root:xnu-11215.40.59~38/RELEASE_ARM64_T8103 arm64

so... is this expected behaviour?
can't test on windows as I don't have a windows machine at hand...

just from looking at the code it seems to be uniformily handled to return nil..
maybe this isn't an issue, rather expected behaviour?

Yes, this is one of those odd legacy behaviors that caused us to introduce the new FormatStyle family of API to replace DateFormatter and other formatter types.