Created
August 17, 2021 03:15
-
-
Save mattyoung/18041a8e8875241c25c5be796ca92e64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct LocaleSpecificDateDisplay: View { | |
static let timeFormatter: DateFormatter = { | |
let formatter = DateFormatter() | |
formatter.dateFormat = "HH:mm:ss.SSSz" | |
return formatter | |
}() | |
static let timeZone = TimeZone(identifier: "Africa/Harare")! | |
var body: some View { | |
VStack { | |
let now = Date.now | |
Text(now, format: .dateTime.hour().minute().second().timeZone()) | |
Group { | |
// Using the new FormatStyle, does NOT obey \.locale environment value | |
// so is this is bug? How to control timezone for display purpose? | |
Text(now, format: .dateTime.hour().minute().second().timeZone()) | |
// The old DateFormatter, **DOES** obey \.locale environment value | |
Text(now, formatter: Self.timeFormatter) + Text(" <= \(Self.timeZone.identifier)") | |
} | |
// explicitly set the timezone here | |
.environment(\.timeZone, Self.timeZone) | |
} | |
} | |
} | |
struct LocaleSpecificDateDisplay_Previews: PreviewProvider { | |
static var previews: some View { | |
LocaleSpecificDateDisplay() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment