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/parsing.cpp | |
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/parsing.cpp')
-rw-r--r-- | src/parsing.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
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) |