Error with QDateEdit and current time
-
I have the following example code in c++
QDate fecha = QDate::currentDate(); QDateEdit *entrada = new QDateEdit(fecha); Registro::Aviso(fecha.toString().toStdString()); Registro::Aviso(entrada->date().toString().toStdString());It worked perfectly until September 11, 2022 that shows the date of September 14, 1752, today it works fine again, I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00
Terminal output (September 10, 2022)
[2022-09-10 18:14:14][AVISO] sáb sep. 10 2022 [2022-09-10 18:14:14][AVISO] sáb sep. 10 2022Terminal output (September 11, 2022)
[2022-09-11 18:00:10][AVISO] dom sep. 11 2022 [2022-09-11 18:00:10][AVISO] jue sep. 14 1752Terminal output (September 12, 2022)
[2022-09-12 18:14:45][AVISO] lun sep. 12 2022 [2022-09-12 18:14:45][AVISO] lun sep. 12 2022 -
I have the following example code in c++
QDate fecha = QDate::currentDate(); QDateEdit *entrada = new QDateEdit(fecha); Registro::Aviso(fecha.toString().toStdString()); Registro::Aviso(entrada->date().toString().toStdString());It worked perfectly until September 11, 2022 that shows the date of September 14, 1752, today it works fine again, I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00
Terminal output (September 10, 2022)
[2022-09-10 18:14:14][AVISO] sáb sep. 10 2022 [2022-09-10 18:14:14][AVISO] sáb sep. 10 2022Terminal output (September 11, 2022)
[2022-09-11 18:00:10][AVISO] dom sep. 11 2022 [2022-09-11 18:00:10][AVISO] jue sep. 14 1752Terminal output (September 12, 2022)
[2022-09-12 18:14:45][AVISO] lun sep. 12 2022 [2022-09-12 18:14:45][AVISO] lun sep. 12 2022@Carque said in Error with QDateEdit and current time:
I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00
That means that on Sep 10/11 a local time between 00:00 and 01:00 "does not exist", there was no 00:00 or 00:30, agreed? I imagine in some shape or form you passed such an "illegal" datetime to a Qt datetime routine, and got back "illegal" from it. (See bool QDateTime::isValid() const and the notes there.) That probably returned something like a datetime of
0, and I would imagine that maps to the year 1752, hence your output.I am guessing that
QDatetreats the time element as00:00. You will have to do something about this to get a correct conversion. -
I found an approximation of solution, don't pass the date through the constructor and use the setDate method instead.
because? I dont knowQDate fecha = QDate::currentDate(); QDateEdit *entrada = new QDateEdit(); entrada->setDate(fecha);The other problem with this was
QDate local_date = QDate::fromString("2022-09-11", "yyyy-MM-dd");this returns year 0
instead I had to use
QDate local_date = QDate::fromString("2022-09-11T00:00:00", Qt::ISODate);because as JonB says it is not a valid time, but I am not interested in the time, I am only interested in the date, I am going to have to read the local date and save it as a utc date?.
-
I have the following example code in c++
QDate fecha = QDate::currentDate(); QDateEdit *entrada = new QDateEdit(fecha); Registro::Aviso(fecha.toString().toStdString()); Registro::Aviso(entrada->date().toString().toStdString());It worked perfectly until September 11, 2022 that shows the date of September 14, 1752, today it works fine again, I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00
Terminal output (September 10, 2022)
[2022-09-10 18:14:14][AVISO] sáb sep. 10 2022 [2022-09-10 18:14:14][AVISO] sáb sep. 10 2022Terminal output (September 11, 2022)
[2022-09-11 18:00:10][AVISO] dom sep. 11 2022 [2022-09-11 18:00:10][AVISO] jue sep. 14 1752Terminal output (September 12, 2022)
[2022-09-12 18:14:45][AVISO] lun sep. 12 2022 [2022-09-12 18:14:45][AVISO] lun sep. 12 2022@Carque said in Error with QDateEdit and current time:
I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00
That means that on Sep 10/11 a local time between 00:00 and 01:00 "does not exist", there was no 00:00 or 00:30, agreed? I imagine in some shape or form you passed such an "illegal" datetime to a Qt datetime routine, and got back "illegal" from it. (See bool QDateTime::isValid() const and the notes there.) That probably returned something like a datetime of
0, and I would imagine that maps to the year 1752, hence your output.I am guessing that
QDatetreats the time element as00:00. You will have to do something about this to get a correct conversion. -
@Carque said in Error with QDateEdit and current time:
I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00
That means that on Sep 10/11 a local time between 00:00 and 01:00 "does not exist", there was no 00:00 or 00:30, agreed? I imagine in some shape or form you passed such an "illegal" datetime to a Qt datetime routine, and got back "illegal" from it. (See bool QDateTime::isValid() const and the notes there.) That probably returned something like a datetime of
0, and I would imagine that maps to the year 1752, hence your output.I am guessing that
QDatetreats the time element as00:00. You will have to do something about this to get a correct conversion. -
@JonB How can I save a local date as utc without saving the time? I think I can't because it would end up confusing days. but the invoice do not have hour and I can not invent one
-
@Carque
Sorry, I don't know what you mean. You can't have a "local" date in, or to convert to, UTC without involving the time, because UTC/timezones involve times.@JonB I know but QDateEdit insists on passing me a date from utc to local adding 00:00:00 as the time and it changes me from 2022-09-11 to 2022-09-10, I think the easiest solution is to tell it that this all in utc
m_fecha = new QDateEdit(this); m_fecha->setTimeSpec(Qt::UTC); m_fecha->setDate(QDate::currentDate()); -
@JonB I know but QDateEdit insists on passing me a date from utc to local adding 00:00:00 as the time and it changes me from 2022-09-11 to 2022-09-10, I think the easiest solution is to tell it that this all in utc
m_fecha = new QDateEdit(this); m_fecha->setTimeSpec(Qt::UTC); m_fecha->setDate(QDate::currentDate());@Carque said in Error with QDateEdit and current time:
I think the easiest solution is to tell it that this all in utc
Indeed so! If you can afford to always work in UTC --- and if you're only interested in dates without times that seems reasonable anyway --- you avoid those pesky timezone conversions which can mess up the date.