Dimension UnitSystem::parseFactor(const std::string& dimension) const { std::vector<std::string> dimensionList; boost::split(dimensionList , dimension , boost::is_any_of("*")); double SIfactor = 1.0; for( const auto& x : dimensionList ) { auto dim = getDimension( x ); // all constituing dimension must be compositable. The // only exception is if there is the "composite" dimension // consists of exactly a single atomic dimension... if (dimensionList.size() > 1 && !dim.isCompositable()) throw std::invalid_argument("Composite dimensions currently cannot require a conversion offset"); SIfactor *= dim.getSIScaling(); } return Dimension::newComposite( dimension , SIfactor ); }
T convertToSeconds(const std::string &unit, T value) { T seconds; if (unit == "min") { seconds = value * 60; } else if (unit == "h") { std::string new_unit = "min"; seconds = convertToSeconds(new_unit, value * 60); } else if (unit == "s" || unit == "sec") { seconds = value; } else if (isScalable(unit, "s")) { double scaled = value * getSIScaling(unit, "s"); seconds = static_cast<T>(std::is_integral<T>::value ? std::round(scaled) : scaled); } else { std::cerr << "[nix::util::convertToSeconds]"; std::cerr << " Warning: given unit [" + unit + "] is not supported!" << std::endl; seconds = value; } return seconds; }
T convertToKelvin(const std::string &unit, T value) { if (unit == "°K" || unit == "K") { return value; } double temperature; if (unit == "°C" || unit == "C") { temperature = value + 273.15; } else if (unit == "°F" || unit == "F") { temperature = (value - 32) * 5.0/9 + 273.15; } else if (isScalable(unit, "K")) { temperature = value * getSIScaling(unit, "K"); } else { std::cerr << "[nix::util::convertToKelvin]" << std::endl; std::cerr << " Warning: given unit [" + unit + "] is not supported!" << std::endl; return value; } return static_cast<T>(std::is_integral<T>::value ? std::round(temperature) : temperature); }