字符串(__DATE__)的QDateTime无效 [英] QDateTime from string (__DATE__) invalid

查看:43
本文介绍了字符串(__DATE__)的QDateTime无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时,qdatetime无效:

When im running the following code the qdatetime is invalid:

QString dateString = QString(__DATE__).simplified();
QDateTime date =  QDateTime::fromString(dateString, "MMM d yyyy");
qDebug() << "Build date " << date.toMSecsSinceEpoch();

dateString的内容= 2020年7月14日,因此没有多余的空格.为什么它不起作用.以下代码可以正常工作:

The content of dateString = Jul 14 2020 so there are no extra spaces. Why is it not working. The following code works fine:

qDebug() << "Build date 2" << QDateTime(QLocale("en_US").toDate(QString(__DATE__).simplified(), "MMM d yyyy")).toMSecsSinceEpoch();

它具有相同的日期格式,并且也是基于 __ DATE __ .

It has the same date format and is also based on __DATE__.

输出为:

Build date  -3600000
Build date 2 1594677600000

我的完整代码:

#include <QCoreApplication>
#include <QDate>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString dateString = QString(__DATE__).simplified();
    QDateTime date =  QDateTime::fromString(dateString, "MMM d yyyy");
    qDebug() << "Build date " << date.toMSecsSinceEpoch();    
    qDebug() << "Build date 2" << QDateTime(QLocale("en_US").toDate(QString(__DATE__).simplified(), "MMM d yyyy")).toMSecsSinceEpoch();

    return a.exec();
}

我正在ubuntu 18.04上运行.

I am running on ubuntu 18.04.

推荐答案

来自

From QDateTime::fromString() (Qt 5.12) documentation:

注意:与该功能的其他版本不同,日期和月份名称必须以用户的本地语言给出.如果用户的语言为英语,则只能使用英语名称.

Note: Unlike the other version of this function, day and month names must be given in the user's local language. It is only possible to use the English names if the user's language is English.

您的系统语言环境必须不是英语,这就是为什么它无法正常工作的原因.

Your system locale must be something other than English, so that's why it fails to work.

这篇关于字符串(__DATE__)的QDateTime无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆