Files
KissMe/KissMeConsole/Sources/Foundation+Extensions.swift
2023-06-20 18:30:04 +09:00

158 lines
5.2 KiB
Swift

//
// Foundation+Extensions.swift
// KissMeConsole
//
// Created by ened-book-m1 on 2023/05/26.
//
import Foundation
import KissMe
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 String {
func parseCandleDate() -> String? {
let fileNameFrag = split(separator: ".")
guard fileNameFrag.count == 2 else {
return nil
}
let candlePrefix = "candle-"
guard fileNameFrag[0].prefix(candlePrefix.count) == candlePrefix, fileNameFrag[1] == "csv" else {
return nil
}
let fileDateFrag = fileNameFrag[0].suffix(fileNameFrag[0].count - candlePrefix.count)
return String(fileDateFrag)
}
}
extension Date {
public var yyyyMMdd_split: (year: Int, month: Int, day: Int)? {
let sets: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
let components = Calendar.current.dateComponents(sets, from: self)
guard let year = components.year, let month = components.month, let day = components.day else {
return nil
}
return (year, month, day)
}
public var HHmmss_split: (hour: Int, minute: Int, second: Int)? {
let sets: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
let components = Calendar.current.dateComponents(sets, from: self)
guard let hour = components.hour, let minute = components.minute, let second = components.second else {
return nil
}
return (hour, minute, second)
}
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)
if let hour = hour {
components.hour = hour
}
if let min = min {
components.minute = min
}
if let sec = sec {
components.second = sec
}
components.nanosecond = 0
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<Calendar.Component> = [.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
}
}
}
extension FileManager {
/// period: If nil, all period of csv collected.
/// candleDate: If nil, all date of csv collected.
///
static func collectCsv(period: KissConsole.CandleFilePeriod?, candleDate: Date?) throws -> [URL] {
guard let enumerator = FileManager.subPathFiles("data") else {
throw GeneralError.noCsvFile
}
var csvUrls = [URL]()
for case let fileUrl as URL in enumerator {
guard fileUrl.pathExtension == "csv" else {
continue
}
let fileName = fileUrl.lastPathComponent
let periodDir = fileUrl.deletingLastPathComponent()
let periodAtPath = periodDir.lastPathComponent
let productNoDir = periodDir.deletingLastPathComponent()
let productNo = productNoDir.lastPathComponent
guard let _ = Int(productNo) else { continue }
if let period = period {
guard periodAtPath == period.rawValue else { continue }
}
if let candleDate = candleDate {
guard candleDate.yyyyMMdd == fileName.parseCandleDate() else { continue }
}
csvUrls.append(fileUrl)
}
return csvUrls
}
}