Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] #391 - 솝마디 메인 뷰 화면 전환 및 데이터 바인딩 #393

Merged
merged 7 commits into from
Sep 28, 2024
Next Next commit
[Fix] #391 - 기존 dateFormat 메소드 수정
- 주어진 날짜가 없다면 오늘 날짜를 사용
  • Loading branch information
yungu0010 committed Sep 27, 2024
commit 87d18685bc2719adce2d34370413205d314569ad
38 changes: 22 additions & 16 deletions SOPT-iOS/Projects/Core/Sources/Utils/setDateFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,33 @@

import Foundation

extension String {

/**

- Description:
"yyyy-MM-dd'T'HH:mm:ss" 형태로 주어진 날짜를 원하는 format으로 변경하는 메서드
/**

- parameters:
- dateString: 변경하고자 하는 dateformat을 입력합니다. ex. "HH:mm"

*/
- Description:
주어진 날짜를 원하는 format으로 변경하는 메서드
주어진 날짜가 없는 경우 오늘 날짜 반환

public func setDateFormat(dateString: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.locale = Locale(identifier: "ko_KR")
dateFormatter.timeZone = TimeZone(identifier: "Asia/Seoul")
- parameters:
- dateString: 변경하고자 하는 dateformat을 입력합니다. ex. "HH:mm"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫶🏻

*/

public func setDateFormat(date: String? = nil, from before: String? = nil, to after: String) -> String {
let dateFormatter = DateFormatter()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift의 DateFormatter init은 생각보다 오버헤드가 큰 작업으로 유명해요. 관련 링크
매번 생성하는 것이 아닌 가능하면 하나의 인스턴스를 두고 공유하는 것이 조금 더 좋을 것 같네요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉 그동안 init 작업의 오버헤드는 고민해보지 않았던 것 같아요 감사합니다 !!

dateFormatter.locale = Locale(identifier: "ko_KR")
dateFormatter.timeZone = TimeZone(identifier: "Asia/Seoul")

if let dateString = date,
let inputFormat = before {

guard let date = dateFormatter.date(from: self) else { return "00:00" }
dateFormatter.dateFormat = before
guard let date = dateFormatter.date(from: dateString) else { return "00:00" }
dateFormatter.dateFormat = after

dateFormatter.dateFormat = dateString
return dateFormatter.string(from: date)
} else {
dateFormatter.dateFormat = after
return dateFormatter.string(from: Date())
}
}