// // Foundation+Extensions.swift // KissMeConsole // // Created by ened-book-m1 on 2023/05/26. // import Foundation extension String { func maxSpace(_ length: Int) -> String { let count = unicodeScalars.reduce(0) { $0 + ($1.value >= 0x80 ? 2: 1) } if count < length { return appending(String(repeating: " ", count: length - count)) } return self } func maxSpace(_ length: Int, digitBy: Int) -> String { guard let number = Int(self) else { return self } // Add comma let formatter = NumberFormatter() formatter.numberStyle = .decimal guard let str = formatter.string(from: NSNumber(value: number)) else { return self } // Add space let count = str.count if (count + count / 3) <= length { return str.appending(String(repeating: " ", count: length - count)) } return self } } extension Date { public func changing(hour: Int, min: Int, sec: Int, timeZone: String = "KST") -> Date? { let sets: Set = [.year, .month, .day, .hour, .minute, .second] var components = Calendar.current.dateComponents(sets, from: self) components.timeZone = TimeZone(abbreviation: timeZone) components.hour = hour components.minute = min components.second = sec return Calendar.current.date(from: components) } public mutating func change(hour: Int, min: Int, sec: Int, timeZone: String = "KST") { if let newDate = changing(hour: hour, min: min, sec: sec, timeZone: timeZone) { self = newDate } } public func changing(year: Int, month: Int, day: Int, timeZone: String = "KST") -> Date? { let sets: Set = [.year, .month, .day, .hour, .minute, .second] var components = Calendar.current.dateComponents(sets, from: self) components.timeZone = TimeZone(abbreviation: timeZone) components.year = year components.month = month components.day = day return Calendar.current.date(from: components) } public mutating func change(year: Int, month: Int, day: Int, timeZone: String = "KST") { if let newDate = changing(year: year, month: month, day: day, timeZone: timeZone) { self = newDate } } }