Skip to content

Instantly share code, notes, and snippets.

@mattyoung
Created August 17, 2021 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattyoung/18041a8e8875241c25c5be796ca92e64 to your computer and use it in GitHub Desktop.
Save mattyoung/18041a8e8875241c25c5be796ca92e64 to your computer and use it in GitHub Desktop.
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