c++ - boost::gregorian::time 到 unix 时间戳作为 double

标签 c++ qt date boost qcustomplot

简要背景:

我正在尝试使用 QCustomPlot 绘制股票的烛台图1.3 版测试版。 我跳过了库的代码,发现对于时间序列,它使用了 type-def (qcustomplot.h:line 3140)

typedef QMap<double, QCPFinancialData> QCPFinancialDataMap;

QCPFinancialData 在哪里(qcustomplot.h:第 3124 行)

class QCP_LIB_DECL QCPFinancialData
{
public:
  QCPFinancialData();
  QCPFinancialData(double key, double open, double high, double low, double close);
  double key, open, high, low, close;
};

因此,OHLC 数据显然在那里,并且该类使用 QMap 中使用的键来索引时间序列条目。

因此,显而易见的关键是日期时间(我正在绘制日终图表,因此每个条目只是一个日期,没有使用时间)。在我的解析代码中,我使用了

boost::gregorian::date

因为它有很多优点(从字符串转换,计算经过的日期时间等)。

问题是,我是否应该继续简单地将 boost::gregorian::date 转换为 unix 时间戳,然后将该时间戳记录为 double 时间戳?我找到了 a small template function on github将它转换为 time_t 类型,但我想在这种情况下 double 应该不是问题,或者这是一个潜在的错误? AFAIK,Unix 时间戳表示自 1970 年 1 月 1 日以来的秒数,当表示为 double 时,对于 key 来说应该绰绰有余?

在 QCustomPlot 的例子中,他们从时间序列序列的开始(例如,开始日期)开始使用累加器/计数器,而不是时间戳。

最佳答案

自纪元以来的时间戳可以非常方便地存储在 double 中,因为您有足够的空间存储自纪元以来的秒数(即 1970 年 1 月 1 日)并且仍然有足够的分辨率来显示更多信息超过一微秒。

例如 R 这样做:

R> now <- Sys.time()                    # get current date and time
R> now                                  # default format to test
[1] "2014-11-11 20:38:27.307228 CST"    # NB I have an option set for subsec.
R> as.integer(now)                      # as integer: seconds since epoch
[1] 1415759907
R> as.double(now)                       # as double under default precision
[1] 1415759907
R> print(as.double(now), digits=16)     # as double with forced higher prec.
[1] 1415759907.307228
R> 

我一直在 C/C++ 层将它们用作 double。如果我没记错的话,你可以 让 Boost 为您进行转换。

编辑:我知道我在某处拥有它:

boost::posix_time::ptime pt;        
// ... in what follows dt has subcomponents we can access
pt = boost::posix_time::ptime(boost::gregorian::date(dt.getYear(), 
                                                     dt.getMonth(), 
                                                     dt.getDay()), 
                              boost::posix_time::time_duration(dt.getHours(), 
                                                              dt.getMinutes(),
                                                              dt.getSeconds(), 
                                                 dt.getMicroSeconds()/1000.0));

time_t 的转换,包括亚秒:

boost::posix_time::ptime dt = .... 


boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1)); 
boost::posix_time::time_duration x = dt - epoch;  // needs UTC to local corr.,
                                                  // but we get the fract. sec.
struct tm t = boost::posix_time::to_tm(dt);       // this helps with UTC conve.
time_t tt = mktime(&t) + 1.0 * x.fractional_seconds() / x.ticks_per_second()));

关于c++ - boost::gregorian::time 到 unix 时间戳作为 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26878276/

相关文章:

c++ - 通过鼠标右键而不是左键使 QGraphicsItem 可移动

linux - Qt中如何获取网络网关地址?

php - Html 表单 - php - mysql 日期字段?

date - 以年、月、日表示的两个日期之间的差异(在一列中)

C++ "Floating Point Enum"

c++ - 编写函数指针比调用函数指针好在哪里?

qt - 在 Qt 中,设置环境变量的不同方法有哪些,优缺点是什么?

java - 如何在 Java 中使用大于或小于日期 我有两个简单日期格式或字符串形式的日期

c++ - 具有字符数组元素的结构的 STL 容器

javascript - 无法在 QML 中创建 map 数据类型吗?