来自const char *“substring”的boost :: posix_time :: ptime

时间:2014-07-02 17:40:06

标签: c++ boost unit-conversion

编辑:实际上,当我们在这里时,我需要为double做同样的事情 - 我在{{1}的中间有一些NNNDD其中N是整个部分而D是小数(因此在123.45中将显示为const char*到12345)。我知道我将拥有多少整数,而且我知道有多少小数,所以这只是从它们中得到一小部分的问题。

我的中间某处const char*有一个格式为HHMMSSMMM的时间序列(小时,分钟,秒,毫秒)。我想用它创建一个const char*(我知道序列的开始位置,所以我的开始时有一个ptime)。有没有一个好的,有效的方法来做到这一点?现在我提取每个时间单位值,将它们存储在局部变量中,并使用默认const char*ptime boost::gregorian::date()来调用time_duration构造函数...但是感觉有点笨拙,迂回。我不应该只是传递时间字符串而不担心日期吗?

1 个答案:

答案 0 :(得分:0)

对于双打,请插入一个。然后把它读成双。

 const char * pDoubleString = ...;
 const long DoubleStringLength = ...;
 const long DoubleWholeCharCount = ...;

 std::string DoubleAsString = std::string(pDoubleString, DoubleStringLength);
 DoubleAsString.Insert(DoubleWholeCharCount, ".");
 double DoubleValue = std::atof(DoubleAsString.c_str());