求助各位大神:QDateTime时间转换的问题(Qt C++)

policezdw 2016-12-06 02:39:13
Qt 做数据采集的项目时,从仪器返回的数据,解析出当前时间如下:
starttime = 201612061102264000
这是一个qint64类型的。
请教下,怎么将这个qint64类型的时间转换成从1970.1.1到这个时间的毫秒数(或者秒数)?
...全文
1099 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
policezdw 2016-12-06
  • 打赏
  • 举报
回复
引用 2 楼 qq423399099 的回复:
阅读官方的API: qint64 QIODevice::readLine(char * data, qint64 maxSize) This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read. 可以知道:readLine返回的是读取的一行数据的长度,并将其读取的数据内容缓存到data。通过以下测试: QString exg_str= QString(reply->readLine()); 可以猜测,QString可能会读取缓存区的data并进行const char*到QString的类型强转。 转换成QString以后,再转成QDateTime,然后利用QDateTime的daysTo方法就能求得到19700101的天数
感谢前辈的耐心回答!我试一下!
policezdw 2016-12-06
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
#include <afxdisp.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
    COleDateTime t,t0;
    COleDateTimeSpan ts;
    int nYear;
    int nMonth;
    int nDay;
    int nHour;
    int nMin;
    int nSec;
    int nMilliseconds;
    __int64 starttime =  201612061102264000i64;
    char s[20];
    double totalsec;
    double totalms;

    sprintf(s,"%I64d",starttime);
    sscanf(s,"%4d%2d%2d%2d%2d%2d%3d",&nYear,&nMonth,&nDay,&nHour,&nMin,&nSec,&nMilliseconds);
    t0=COleDateTime( 1970,      1,    1,     0,    0,    0);
    printf("1970-01-01 00:00:00.000\n");
    t =COleDateTime(nYear, nMonth, nDay, nHour, nMin, nSec);
    printf("%4d-%02d-%02d %02d:%02d:%02d.%03d\n",nYear, nMonth, nDay, nHour, nMin, nSec, nMilliseconds);
    ts=t-t0;
    totalsec=ts.GetTotalSeconds();
    printf("totalsec=%.15lg\n",totalsec);
    totalms=totalsec*1000.0+nMilliseconds;
    printf("totalms =%.15lg\n",totalms);

    return 0;
}
//1970-01-01 00:00:00.000
//2016-12-06 11:02:26.400
//totalsec=1481022146
//totalms =1481022146400
//
刚开始学C++\Qt,经常在其他朋友的帖子中看到您的回答。没想到我第一次发帖,您就帮我解答了!真的很感谢! 不知道Qt环境下您可以有相应的代码吗?我对照您的代码,在Qt中找找是否能有相应的方法可以实现。
小灸舞 2016-12-06
  • 打赏
  • 举报
回复
阅读官方的API:
qint64 QIODevice::readLine(char * data, qint64 maxSize)

This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read.

可以知道:readLine返回的是读取的一行数据的长度,并将其读取的数据内容缓存到data。通过以下测试:

QString exg_str= QString(reply->readLine());

可以猜测,QString可能会读取缓存区的data并进行const char*到QString的类型强转。

转换成QString以后,再转成QDateTime,然后利用QDateTime的daysTo方法就能求得到19700101的天数
赵4老师 2016-12-06
  • 打赏
  • 举报
回复
#include <afxdisp.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
    COleDateTime t,t0;
    COleDateTimeSpan ts;
    int nYear;
    int nMonth;
    int nDay;
    int nHour;
    int nMin;
    int nSec;
    int nMilliseconds;
    __int64 starttime =  201612061102264000i64;
    char s[20];
    double totalsec;
    double totalms;

    sprintf(s,"%I64d",starttime);
    sscanf(s,"%4d%2d%2d%2d%2d%2d%3d",&nYear,&nMonth,&nDay,&nHour,&nMin,&nSec,&nMilliseconds);
    t0=COleDateTime( 1970,      1,    1,     0,    0,    0);
    printf("1970-01-01 00:00:00.000\n");
    t =COleDateTime(nYear, nMonth, nDay, nHour, nMin, nSec);
    printf("%4d-%02d-%02d %02d:%02d:%02d.%03d\n",nYear, nMonth, nDay, nHour, nMin, nSec, nMilliseconds);
    ts=t-t0;
    totalsec=ts.GetTotalSeconds();
    printf("totalsec=%.15lg\n",totalsec);
    totalms=totalsec*1000.0+nMilliseconds;
    printf("totalms =%.15lg\n",totalms);

    return 0;
}
//1970-01-01 00:00:00.000
//2016-12-06 11:02:26.400
//totalsec=1481022146
//totalms =1481022146400
//

65,212

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧