diff options
author | lloyd <[email protected]> | 2008-05-01 15:09:08 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-05-01 15:09:08 +0000 |
commit | 250cb392d6bdd29adea28b0f9a5562a880656dca (patch) | |
tree | 5379027a707a3c74e0cf4f4e51a54ba0a2ecc3b1 /src | |
parent | 91ee44181caadaac5cf42b1469134f42281c5497 (diff) |
Add a new function timespec_to_u32bit that handles a very simplistic
time format and converts it to a duration in seconds represented as a
u32bit. This is from Config::option_as_time, which is now implemented
simply as: return timespec_to_u32bit(option(key))
Diffstat (limited to 'src')
-rw-r--r-- | src/config.cpp | 28 | ||||
-rw-r--r-- | src/parsing.cpp | 31 |
2 files changed, 32 insertions, 27 deletions
diff --git a/src/config.cpp b/src/config.cpp index d131b2f59..0d4d459b6 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -126,33 +126,7 @@ std::string Config::option(const std::string& key) const *************************************************/ u32bit Config::option_as_time(const std::string& key) const { - const std::string timespec = option(key); - if(timespec == "") - return 0; - - const char suffix = timespec[timespec.size()-1]; - std::string value = timespec.substr(0, timespec.size()-1); - - u32bit scale = 1; - - if(Charset::is_digit(suffix)) - value += suffix; - else if(suffix == 's') - scale = 1; - else if(suffix == 'm') - scale = 60; - else if(suffix == 'h') - scale = 60 * 60; - else if(suffix == 'd') - scale = 24 * 60 * 60; - else if(suffix == 'y') - scale = 365 * 24 * 60 * 60; - else - throw Decoding_Error( - "Config::option_as_time: Unknown time value " + value - ); - - return scale * to_u32bit(value); + return timespec_to_u32bit(option(key)); } } diff --git a/src/parsing.cpp b/src/parsing.cpp index 59c0e3324..550cd3586 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -55,6 +55,37 @@ std::string to_string(u64bit n, u32bit min_len) } /************************************************* +* Convert a string into a time duration * +*************************************************/ +u32bit timespec_to_u32bit(const std::string& timespec) + { + if(timespec == "") + return 0; + + const char suffix = timespec[timespec.size()-1]; + std::string value = timespec.substr(0, timespec.size()-1); + + u32bit scale = 1; + + if(Charset::is_digit(suffix)) + value += suffix; + else if(suffix == 's') + scale = 1; + else if(suffix == 'm') + scale = 60; + else if(suffix == 'h') + scale = 60 * 60; + else if(suffix == 'd') + scale = 24 * 60 * 60; + else if(suffix == 'y') + scale = 365 * 24 * 60 * 60; + else + throw Decoding_Error("timespec_to_u32bit: Bad input " + timespec); + + return scale * to_u32bit(value); + } + +/************************************************* * Parse a SCAN-style algorithm name * *************************************************/ std::vector<std::string> parse_algorithm_name(const std::string& namex) |