PDA

View Full Version : Negative dates in QDateTIme



tuli
31st March 2015, 20:58
Hi, I need a datetime widget that goes a few thousand years into the past and the future. But no matter what i tried, I never get close to negative dates. Usually the hard limit is around 1666.

For example:



QDateTime dt = QDateTime::fromString("01.01.1555 00:00");
ui.datetime->setMinimumDateTime(dt);
ui.datetime->setDateTime(dt);

ui.datetime is a QDateTimeEdit. This doesn't result in the year 1555, but at UTC 0 at 1970!

I can manually lower the bar with the GUI a little further, but not much.


Can Qt handle such small dates at all?

ChrisW67
31st March 2015, 21:22
QDateTime is quite capable of covering a range of several million years in both directions (most if not all platforms). My first guess is that the string to date conversion has failed because you have given it a string without a format specification, given you an invalid QDateTime, and that is what the widget is displaying. You could check QDateTime::isValid() , provide a format string, or just avoid that possibility.

Try this


QDateTime dt(QDate(1555, 1, 1));

tuli
31st March 2015, 21:54
That works for 1555, but there is still some arbitrary limit nearby:

QDateTime dt(QDate(12, 1, 1));
ui.datetime->setMinimumDateTime(dt);
ui.datetime->setDateTime(dt);

will display 1752. :/

jefftee
31st March 2015, 22:49
From the doc for QDateTimeEdit, it says this about the minimumDate property:



By default, this property contains a date that refers to September 14, 1752. The minimum date must be at least the first day in year 100, otherwise setMinimumDate() has no effect.

wysota
31st March 2015, 23:00
To quote QDate docs:


Range of Valid Dates

Dates are stored internally as a Julian Day number, an integer count of every day in a contiguous range, with 24 November 4714 BCE in the Gregorian calendar being Julian Day 0 (1 January 4713 BCE in the Julian calendar). As well as being an efficient and accurate way of storing an absolute date, it is suitable for converting a Date into other calendar systems such as Hebrew, Islamic or Chinese. The Julian Day number can be obtained using QDate::toJulianDay() and can be set using QDate::fromJulianDay().

The range of dates able to be stored by QDate as a Julian Day number is for technical reasons limited to between -784350574879 and 784354017364, which means from before 2 billion BCE to after 2 billion CE.

tuli
1st April 2015, 11:13
So essentially QDate can handle a lot, but QDateTimeEdit cannot.

I guess the user will then have to manually type in the date into a textbox. :/

anda_skoa
1st April 2015, 14:05
You could check if the date input widgets from KDE Widget Addons http://inqlude.org/libraries/kwidgetsaddons.html have fewer restrictions.

Cheers,
_

ChrisW67
1st April 2015, 21:03
The 14 September 1752 default lower limit for the date edit is when then British Empire (A large proportion of the world) adopted the Gregorian calendar (14 Sept followed 2 Sept). I don't believe that QDateTime makes any allowance for the 11 day difference for dates prior to that, for the different start day for the year (1751 started 25 March ended 31 Dec) , or for the variation of all of that with location. If that matters to you then you will not be able to use QDateTime alone anyway.

wysota
1st April 2015, 21:53
The 14 September 1752 default lower limit for the date edit is when then British Empire (A large proportion of the world) adopted the Gregorian calendar (14 Sept followed 2 Sept).
Being one of the last European countries to do it ;)