mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-18 08:49:29 -04:00
Add parse_time_with_unit function
This commit is contained in:
parent
5fcb0a1d13
commit
923eda7a71
@ -21,6 +21,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
@ -28,7 +29,8 @@ namespace dwarfs {
|
||||
|
||||
std::string time_with_unit(double sec);
|
||||
std::string size_with_unit(size_t size);
|
||||
size_t parse_size_with_unit(const std::string& str);
|
||||
size_t parse_size_with_unit(std::string const& str);
|
||||
std::chrono::milliseconds parse_time_with_unit(std::string const& str);
|
||||
std::string get_program_path();
|
||||
|
||||
} // namespace dwarfs
|
||||
|
@ -20,8 +20,10 @@
|
||||
*/
|
||||
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <climits>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
@ -50,7 +52,7 @@ std::string time_with_unit(double sec) {
|
||||
return trimmed(folly::prettyPrint(sec, folly::PRETTY_TIME, false));
|
||||
}
|
||||
|
||||
size_t parse_size_with_unit(const std::string& str) {
|
||||
size_t parse_size_with_unit(std::string const& str) {
|
||||
size_t end = 0;
|
||||
size_t size = std::stoul(str, &end);
|
||||
|
||||
@ -84,6 +86,40 @@ size_t parse_size_with_unit(const std::string& str) {
|
||||
DWARFS_THROW(runtime_error, "invalid size suffix");
|
||||
}
|
||||
|
||||
std::chrono::milliseconds parse_time_with_unit(std::string const& str) {
|
||||
uint64_t value;
|
||||
auto [ptr, ec]{std::from_chars(str.data(), str.data() + str.size(), value)};
|
||||
|
||||
if (ec != std::errc()) {
|
||||
DWARFS_THROW(runtime_error, "cannot parse time value");
|
||||
}
|
||||
|
||||
switch (ptr[0]) {
|
||||
case 'h':
|
||||
if (ptr[1] == '\0') {
|
||||
return std::chrono::hours(value);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'm':
|
||||
if (ptr[1] == '\0') {
|
||||
return std::chrono::minutes(value);
|
||||
} else if (ptr[1] == 's' && ptr[2] == '\0') {
|
||||
return std::chrono::milliseconds(value);
|
||||
}
|
||||
break;
|
||||
|
||||
case '\0':
|
||||
case 's':
|
||||
return std::chrono::seconds(value);
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
DWARFS_THROW(runtime_error, "unsupported time suffix");
|
||||
}
|
||||
|
||||
std::string get_program_path() {
|
||||
static const std::array<const char*, 3> paths = {{
|
||||
"/proc/self/exe",
|
||||
|
Loading…
x
Reference in New Issue
Block a user