ISO 8601
을 아십니까?
일단 난 몰랐음 .. :(
국제표준화기구 ISO에서 정한 날짜, 시간 데이터에 대한 규격이다.
글 제목과 같이 `yyyy-MM-ddThh-mm-ssZ`의 형태이다. 최근 News API를 사용해서 공부하느라 앱을 만들고 있는데, 이 API에서 뉴스 작성일에 대한 응답값을 이런 형태로 넘겨준다. 왜 이렇게 보내주나 하고 처음에는 T와 Z를 기준(separator)으로 split해주고 인덱스를 사용해서 일일이 날짜만 가져와줬는데, 그럴 필요가 없더라. 웹에서 보편화된 포맷이라 스위프트에서도 해당 포맷을 이미 정의해놨기 때문.
# 기존 작성한 코드 (하나하나 split하고 인덱싱)
static let dateAndTime = "T"
static let endOfTime = "Z"
static func getPublishedDate(_ publishedAt: String) -> String {
if !publishedAt.contains(dateAndTime) { return publishedAt }
if let date = publishedAt.split(separator: Character(dateAndTime)).map({ String($0) }).first {
let splitDate = date.split(separator: "-").map({Int($0)!})
let (year, month, day) = (splitDate[0], splitDate[1], splitDate[2])
let myDateComponents = DateComponents(year: year, month: month, day: day)
let startDate = Calendar.current.date(from: myDateComponents)!
let offset = Calendar.current.dateComponents([.year,.month,.day], from: startDate, to: Date())
return date
}
return publishedAt
}
# ISO8601DateFormatter를 사용하는 방법
static func getPublishedDate(_ publishedAt: String) -> String {
let iso8601FormattedDate = ISO8601DateFormatter().date(from: publishedAt)
if let iso8601FormattedDate = iso8601FormattedDate,
let publishedAt = iso8601FormattedDate.description.split(separator: " ").first {
return publishedAt.description
}
return publishedAt
}
그냥 코드만 봐도 훨씬 줄고, separator를 기준으로 잘라서 인덱스가 뭐면 뭐고 ... 이럴 필요 없음
당연히 publisheAt으로 들어오는 String의 포맷은 `yyyy-MM-ddThh-mm-ssZ`이어야 한다. 이 앱에서는 예외가 nil인 경우 말고 다른 포맷으로 들어오는 경우는 절대 없을거라 따로 처리해주지 않았는데, 필요하다면 예외처리를 따로 해주어야겠다.
아 그리고 위 예제 코드에서 iso8601FormattedDate는 2024-05-16 00:00:00 +0000 형태로 들어오는걸 띄어쓰기 기준으로 잘라서 날짜만 반환하게 햇지만, ISO8601DateFormatter의 formatOptions를 활용하면 원하는 부분만 원하는 형태로 사용할 수 있다. 옵션은 공식문서를 참고 🥸
🥸 끗 - 🥸
References
ISO8601DateFormatter | Apple Developer Documentation
A formatter that converts between dates and their ISO 8601 string representations.
developer.apple.com
formatOptions | Apple Developer Documentation
Options for generating and parsing ISO 8601 date representations. See for possible values.
developer.apple.com
News API – Search News and Blog Articles on the Web
“Ascender AI has a mission to apply AI to the media, and NewsAPI is one of our most valuable resources. Ascender is redefining how users interact with complex information, and the NewsAPI feed is an essential showcase for our technologies.” Braddock Ga
newsapi.org
ISO 8601
을 아십니까?
일단 난 몰랐음 .. :(
국제표준화기구 ISO에서 정한 날짜, 시간 데이터에 대한 규격이다.
글 제목과 같이 `yyyy-MM-ddThh-mm-ssZ`의 형태이다. 최근 News API를 사용해서 공부하느라 앱을 만들고 있는데, 이 API에서 뉴스 작성일에 대한 응답값을 이런 형태로 넘겨준다. 왜 이렇게 보내주나 하고 처음에는 T와 Z를 기준(separator)으로 split해주고 인덱스를 사용해서 일일이 날짜만 가져와줬는데, 그럴 필요가 없더라. 웹에서 보편화된 포맷이라 스위프트에서도 해당 포맷을 이미 정의해놨기 때문.
# 기존 작성한 코드 (하나하나 split하고 인덱싱)
static let dateAndTime = "T"
static let endOfTime = "Z"
static func getPublishedDate(_ publishedAt: String) -> String {
if !publishedAt.contains(dateAndTime) { return publishedAt }
if let date = publishedAt.split(separator: Character(dateAndTime)).map({ String($0) }).first {
let splitDate = date.split(separator: "-").map({Int($0)!})
let (year, month, day) = (splitDate[0], splitDate[1], splitDate[2])
let myDateComponents = DateComponents(year: year, month: month, day: day)
let startDate = Calendar.current.date(from: myDateComponents)!
let offset = Calendar.current.dateComponents([.year,.month,.day], from: startDate, to: Date())
return date
}
return publishedAt
}
# ISO8601DateFormatter를 사용하는 방법
static func getPublishedDate(_ publishedAt: String) -> String {
let iso8601FormattedDate = ISO8601DateFormatter().date(from: publishedAt)
if let iso8601FormattedDate = iso8601FormattedDate,
let publishedAt = iso8601FormattedDate.description.split(separator: " ").first {
return publishedAt.description
}
return publishedAt
}
그냥 코드만 봐도 훨씬 줄고, separator를 기준으로 잘라서 인덱스가 뭐면 뭐고 ... 이럴 필요 없음
당연히 publisheAt으로 들어오는 String의 포맷은 `yyyy-MM-ddThh-mm-ssZ`이어야 한다. 이 앱에서는 예외가 nil인 경우 말고 다른 포맷으로 들어오는 경우는 절대 없을거라 따로 처리해주지 않았는데, 필요하다면 예외처리를 따로 해주어야겠다.
아 그리고 위 예제 코드에서 iso8601FormattedDate는 2024-05-16 00:00:00 +0000 형태로 들어오는걸 띄어쓰기 기준으로 잘라서 날짜만 반환하게 햇지만, ISO8601DateFormatter의 formatOptions를 활용하면 원하는 부분만 원하는 형태로 사용할 수 있다. 옵션은 공식문서를 참고 🥸
🥸 끗 - 🥸
References
ISO8601DateFormatter | Apple Developer Documentation
A formatter that converts between dates and their ISO 8601 string representations.
developer.apple.com
formatOptions | Apple Developer Documentation
Options for generating and parsing ISO 8601 date representations. See for possible values.
developer.apple.com
News API – Search News and Blog Articles on the Web
“Ascender AI has a mission to apply AI to the media, and NewsAPI is one of our most valuable resources. Ascender is redefining how users interact with complex information, and the NewsAPI feed is an essential showcase for our technologies.” Braddock Ga
newsapi.org