Skip to content

Instantly share code, notes, and snippets.

@alibitek
Created June 7, 2014 15:10
Show Gist options
  • Save alibitek/37d087dac97e30d17458 to your computer and use it in GitHub Desktop.
Save alibitek/37d087dac97e30d17458 to your computer and use it in GitHub Desktop.
A template for converting a uint64_t giving a duration in terms of T1 into a duration expressed as a T2.
/**
* A template for converting a uint64_t giving a duration in terms of T1 into
* a duration expressed as a T2.
*
* For example, MkTime<std::chrono::seconds> takes in a uint64_t of seconds
* and returns a std::chrono::microseconds.
*/
template <typename T1, typename T2 = std::chrono::microseconds>
T2 MkTime(uint64_t raw_time)
{
return std::chrono::duration_cast<T2>(T1(raw_time));
}
typedef std::map<std::string,
std::function<std::chrono::microseconds(uint64_t)>>
TimeSuffixMap;
/**
* Mapping from unit suffixes for seek command times to functions converting
* time integers to the appropriate duration in microseconds.
*/
static const TimeSuffixMap time_suffixes = {
{"s", MkTime<std::chrono::seconds>},
{"sec", MkTime<std::chrono::seconds>},
{"secs", MkTime<std::chrono::seconds>},
{"m", MkTime<std::chrono::minutes>},
{"min", MkTime<std::chrono::minutes>},
{"mins", MkTime<std::chrono::minutes>},
{"h", MkTime<std::chrono::hours>},
{"hour", MkTime<std::chrono::hours>},
{"hours", MkTime<std::chrono::hours>},
// Default when there is no unit
{"", MkTime<std::chrono::microseconds>}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment