Created
October 3, 2024 13:14
-
-
Save Codelaby/859d6601e497dc15b068ab98527a343b to your computer and use it in GitHub Desktop.
timezone_playground.swift
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 | |
fileprivate extension Date.FormatStyle { | |
func withTimeZone(_ timeZone: TimeZone?) -> Date.FormatStyle { | |
var copy = self | |
if let timeZone = timeZone { | |
copy.timeZone = timeZone | |
} | |
return copy | |
} | |
} | |
struct TimeZonePlayground: View { | |
@State private var date = Date() | |
var body: some View { | |
VStack { | |
DatePicker("Korea", selection: $date, displayedComponents: [.date, .hourAndMinute]) | |
.environment(\.timeZone, TimeZone(identifier: "Asia/Seoul")!) | |
DatePicker("US", selection: $date, displayedComponents: [.date, .hourAndMinute]) | |
.environment(\.timeZone, TimeZone(identifier: "America/New_York")!) | |
DatePicker("Paris", selection: $date, displayedComponents: [.date, .hourAndMinute]) | |
.environment(\.timeZone, TimeZone(abbreviation: "CET")!) | |
// User/Device Date Picker | |
DatePicker("Local", selection: $date, displayedComponents: [.date, .hourAndMinute]) | |
//Timezone conversion by Date.FormatStyle | |
GroupBox("Date.FormatStyle") { | |
Text(date.formatted(Date.FormatStyle(timeZone: TimeZone(identifier: "Asia/Seoul")!).hour().minute().day().month().year().timeZone(.specificName(.long)))) | |
Text(date.formatted(Date.FormatStyle(timeZone: TimeZone(identifier: "America/New_York")!).hour().minute().day().month().year().timeZone(.specificName(.long)))) | |
Text(date.formatted(Date.FormatStyle(timeZone: TimeZone(identifier: "CET")!).hour().minute().day().month().year().timeZone(.specificName(.long)))) | |
Text(date.formatted(Date.FormatStyle().hour().minute().day().month().year().timeZone(.specificName(.long)))) | |
} | |
.font(.caption) | |
//Timezone conversion with extension .withTimeZone | |
let dfTzLong = Date.FormatStyle().day().month().year().hour().minute().timeZone(.specificName(.long)) | |
let dfTzShort = Date.FormatStyle().day().month().year().hour().minute().timeZone(.specificName(.short)) | |
GroupBox(".withTimeZone extension") { | |
Text(date.formatted(dfTzLong.withTimeZone(TimeZone(identifier: "Asia/Seoul")))) | |
Text(date.formatted(dfTzLong.withTimeZone(TimeZone(identifier: "America/New_York")))) | |
Text(date.formatted(dfTzLong.withTimeZone(TimeZone(identifier: "CET")))) | |
Text(date.formatted(dfTzLong.withTimeZone(nil))) | |
let _ = print(date.formatted(dfTzShort.withTimeZone(TimeZone(identifier: "America/New_York")))) | |
// Text(date.formatted(.dateTime.day().month().year().hour().minute().timeZone(.specificName(.long)).withTimeZone(TimeZone(identifier: "Asia/Seoul")))) | |
// Text(date.formatted(.dateTime.day().month().year().hour().minute().timeZone(.specificName(.long)).withTimeZone(TimeZone(identifier: "America/New_York")))) | |
// Text(date.formatted(.dateTime.day().month().year().hour().minute().timeZone(.specificName(.long)).withTimeZone(TimeZone(identifier: "CET")))) | |
// Text(date.formatted(.dateTime.day().month().year().hour().minute().timeZone(.specificName(.long)).withTimeZone(nil))) | |
} | |
.font(.caption) | |
//Timezone conversion by function and DateFormatter | |
GroupBox("DateFormatter") { | |
Text(dateFormatterTz(date, timeZone: TimeZone(identifier: "Asia/Seoul"))) | |
Text(dateFormatterTz(date, timeZone: TimeZone(identifier: "America/New_York"))) | |
Text(dateFormatterTz(date, timeZone: TimeZone(identifier: "CET"))) | |
Text(dateFormatterTz(date, timeZone: Locale.current.timeZone)) | |
} | |
.font(.caption) | |
} | |
.padding() | |
} | |
private func dateFormatterTz(_ initDate: Date = Date(), timeZone: TimeZone? = nil) -> String { | |
let formatter = DateFormatter() | |
formatter.dateStyle = .short | |
formatter.timeStyle = .full | |
if let timeZone = timeZone { | |
formatter.timeZone = timeZone | |
} | |
return formatter.string(from: initDate) | |
} | |
} | |
#Preview { | |
TimeZonePlayground() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment