QDateTime::fromString 不接受我的 QString?

发布于 2024-12-07 18:36:52 字数 1758 浏览 4 评论 0原文

我有一个 .txt 文件,其中包含如下行:

  • 2011-03-03 03.33.13.222 4 2000 Information BUSINESS ...etc blabla
  • 2011-03-03 03.33.13.333 4 2000 Information BUSINESS ...etc blabla
  • 2011 -03-03 03.33.13.444 4 2000 Information BUSINESS ...etc blabla

在我的代码中的某个时刻,我做了一些计算和查找,其中我仅提取每行开头的日期。现在,当我正确定位在文件开头时,我仅提取日期和时间(以毫秒为单位)“例如:2011-03-03 03.33.13.444”并转换为 QDateTime 对象。

假设我的文件指针正确定位在某一行的开头, 使用 readLine 我读取日期时间文本行并转换为 QDateTime 对象

QDateTime dt;
char lineBuff[1024];
qint64 lineLength;
lineLength=file.readLine(lineBuff, 24); 
dt = QDateTime::fromString(QString(lineBuff),"yyyy-MM-dd HH.mm.ss.zzz");

这绝对正确。

但是,问题是:

当我这样做时:

QDateTime dt;
QByteArray baLine;
char lineBuff[1024];
file.seek(nGotoPos); //QFile, nGotoPos = a position in my file
QString strPrev(baLine); // convert bytearry to qstring -> so i can use mid()

// calculate where the last two newline characters are in that string
int nEndLine = strPrev.lastIndexOf("\n");
int nStartLine = strPrev.lastIndexOf("\n", -2);

QString strMyWholeLineOfTextAtSomePoint = strPrev.mid(nStartLine,nEndLine);
QString strMyDateTime = strMyWholeLineOfTextAtSomePoint.left(24); 

// strMyDateTime in debug mode shows me that it is filled with my string 
// "ex: 2011-03-03 03.33.13.444" 

// THE PROBLEM
// But when i try to covert that string to my QDateTime object it is empty
dt = QDateTime::fromString(strMyDateTime ,"yyyy-MM-dd HH.mm.ss.zzz");

dt.isValid() //false
dt.toString () // "" -> empty ????

但是如果我这样做:

dt = QDateTime::fromString("2011-03-03 03.33.13.444","yyyy-MM-dd HH.mm.ss. zzz”); 那么一切就都好了。

我的 QString 可能有什么问题? 我是否需要在 strMyDateTime 后面附加一个“\0”或者我是否需要一些其他转换?

I have a .txt file which is filled with lines like this below:

  • 2011-03-03 03.33.13.222 4 2000 Information BUSINESS ...etc blabla
  • 2011-03-03 03.33.13.333 4 2000 Information BUSINESS ...etc blabla
  • 2011-03-03 03.33.13.444 4 2000 Information BUSINESS ...etc blabla

In some point in my code i do some calculating and seeking, where i extract only the dates from the beginning of each line. Now, when i'm positioned correct at the beginning of a file, i extract only the date and time (with the miliseconds) "ex: 2011-03-03 03.33.13.444" and convert to a QDateTime object.

Assuming that my file pointer is positioned correct at the beginning of a certain line,
with readLine i read my datetime text line and convert to QDateTime object

QDateTime dt;
char lineBuff[1024];
qint64 lineLength;
lineLength=file.readLine(lineBuff, 24); 
dt = QDateTime::fromString(QString(lineBuff),"yyyy-MM-dd HH.mm.ss.zzz");

This is apsolutely correct.

But, here is the problem:

When i do the same like this:

QDateTime dt;
QByteArray baLine;
char lineBuff[1024];
file.seek(nGotoPos); //QFile, nGotoPos = a position in my file
QString strPrev(baLine); // convert bytearry to qstring -> so i can use mid()

// calculate where the last two newline characters are in that string
int nEndLine = strPrev.lastIndexOf("\n");
int nStartLine = strPrev.lastIndexOf("\n", -2);

QString strMyWholeLineOfTextAtSomePoint = strPrev.mid(nStartLine,nEndLine);
QString strMyDateTime = strMyWholeLineOfTextAtSomePoint.left(24); 

// strMyDateTime in debug mode shows me that it is filled with my string 
// "ex: 2011-03-03 03.33.13.444" 

// THE PROBLEM
// But when i try to covert that string to my QDateTime object it is empty
dt = QDateTime::fromString(strMyDateTime ,"yyyy-MM-dd HH.mm.ss.zzz");

dt.isValid() //false
dt.toString () // "" -> empty ????

BUT IF I DO:

dt = QDateTime::fromString("2011-03-03 03.33.13.444","yyyy-MM-dd HH.mm.ss.zzz");
Then everything is alright.

What could posibly be the problem with my QString?
Do i need to append to strMyDateTime a "\0" or do i need some other conversions??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情感失落者 2024-12-14 18:36:52

您的字符串有额外的字符,很可能开头有一个空格。您的格式字符串有 23 个字符,并且您使用的是 left(24),因此必须有 1 个额外字符。您在对 Stephen Chu 的回答的评论中说,将 24 更改为 23 会删除最后一个毫秒字符,因此多余的字符必须位于开头。

Your string has extra characters, most likely a space in the beginning. Your format string is 23 characters and you are using left(24), so there must be one extra character. You said in the comment to Stephen Chu's answer that changing 24 to 23 dropped the last millisecond character, so the extra character must be in the beginning.

暮倦 2024-12-14 18:36:52

“2011-03-03 03.33.13.444”实际上是23个字符长,而不是24个。您提取的字符串末尾可能有一个额外的字符?

"2011-03-03 03.33.13.444" is actually 23 characters long, not 24. Your extracted string probably has a extra character at the end?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文