| 201 | } |
| 202 | |
| 203 | std::string Utils::FindSecondNewestFile(const std::string &path, const std::string &extension) { |
| 204 | std::string latestDateDir = Utils::FindLatestSubDir(path); |
| 205 | if (latestDateDir.empty()) { |
| 206 | std::cerr << "No date directories found." << std::endl; |
| 207 | return ""; |
| 208 | } |
| 209 | |
| 210 | std::string datePath = (fs::path(path) / latestDateDir).string(); |
| 211 | std::string latestHourDir = Utils::FindLatestSubDir(datePath); |
| 212 | if (latestHourDir.empty()) { |
| 213 | std::cerr << "No hour directories found." << std::endl; |
| 214 | return ""; |
| 215 | } |
| 216 | |
| 217 | std::string latestDir = (fs::path(datePath) / latestHourDir).string(); |
| 218 | auto files = Utils::GetFiles(latestDir, extension); |
| 219 | |
| 220 | // find previous hour |
| 221 | if (files.size() < 2) { |
| 222 | std::string prevHourDir = latestHourDir; |
| 223 | if (!prevHourDir.empty() && prevHourDir > "00") { |
| 224 | prevHourDir = std::to_string(std::stoi(prevHourDir) - 1); |
| 225 | if (prevHourDir.length() < 2) { |
| 226 | prevHourDir = "0" + prevHourDir; |
| 227 | } |
| 228 | std::string prevDir = (fs::path(datePath) / prevHourDir).string(); |
| 229 | auto prevFiles = Utils::GetFiles(prevDir, extension); |
| 230 | |
| 231 | files.insert(files.end(), prevFiles.begin(), prevFiles.end()); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // find previous date |
| 236 | if (files.size() < 2) { |
| 237 | std::string prevDateDir = Utils::GetPreviousDate(latestDateDir); |
| 238 | |
| 239 | std::string prevDatePath = (fs::path(path) / prevDateDir).string(); |
| 240 | latestHourDir = Utils::FindLatestSubDir(prevDatePath); |
| 241 | std::string prevDir = (fs::path(prevDatePath) / latestHourDir).string(); |
| 242 | auto prevFiles = Utils::GetFiles(prevDir, extension); |
| 243 | |
| 244 | files.insert(files.end(), prevFiles.begin(), prevFiles.end()); |
| 245 | } |
| 246 | |
| 247 | std::sort(files.begin(), files.end(), std::greater<>()); |
| 248 | |
| 249 | if (files.size() < 2) { |
| 250 | std::cerr << "Not enough files to determine the second newest file." << std::endl; |
| 251 | return ""; |
| 252 | } |
| 253 | |
| 254 | return files[1].second.string(); |
| 255 | } |
| 256 | |
| 257 | std::chrono::system_clock::time_point Utils::ParseDatetime(const std::string &datetime_str) { |
| 258 | std::tm tm = {}; |