54 lines
1.5 KiB
Swift
54 lines
1.5 KiB
Swift
//
|
|
// ConsoleExtensions.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<Calendar.Component> = [.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)
|
|
}
|
|
}
|