Swift(스위프트): DateFormatter로 시간대 약어(Timezone abbreviation) 표시
Swift(스위프트): DateFormatter로 시간대 약어(Timezone abbreviation) 표시
TimeZone.current.identifier와 TimeZone.abbreviationDictionary를 사용하면 시간대 약어를 표시할 수 있습니다.
1
2
3
4
5
6
7
8
9
10
11
12
func dateString(timestamp: Int) -> String {
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
let formatter = DateFormatter()
formatter.timeZone = .autoupdatingCurrent
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let timezoneAbbr = TimeZone.abbreviationDictionary.first { $1 == formatter.timeZone.identifier }
// timezoneAbbr?.value는 Asia/Seoul, timezoneAbbr?.key는 KST로 표시
// .value는 timeZone.identifier와 동일
return formatter.string(from: date) + " (\(timezoneAbbr?.value ?? "Unknown"))"
}
1
2
3
1655192817
2022-06-14 16:46:57 (KST)
2022-06-14 16:46:57 (Asia/Seoul)
출처
참고
This post is licensed under
CC BY 4.0
by the author.