18#if QT_VERSION >= 0x050000
20typedef qint64 QwtJulianDay;
21static const QwtJulianDay minJulianDayD = Q_INT64_C( -784350574879 );
22static const QwtJulianDay maxJulianDayD = Q_INT64_C( 784354017364 );
30typedef int QwtJulianDay;
31static const QwtJulianDay minJulianDayD = 1;
32static const QwtJulianDay maxJulianDayD = std::numeric_limits< int >::max();
36static QString qwtExpandedFormat(
const QString& format,
42 weekNo.setNum( week );
45 if ( weekNo.length() == 1 )
46 weekNoWW += QLatin1Char(
'0' );
51 fmt.replace( QLatin1String(
"ww" ), weekNoWW );
52 fmt.replace( QLatin1Char(
'w' ), weekNo );
54 if ( week == 1 && dateTime.date().month() != 1 )
58 QLatin1String s_yyyy(
"yyyy" );
59 QLatin1String s_yy(
"yy" );
63 bool doReplaceYear = fmt.contains( s_yy );
67 if ( fmt.contains(
'M' ) )
72 doReplaceYear =
false;
81 for (
int i = 0; i < fmt.size(); i++ )
89 if ( numD > 0 && numD <= 2 )
96 if ( numD > 0 && numD <= 2 )
97 doReplaceYear =
false;
103 const QDate dt( dateTime.date().year() + 1, 1, 1 );
104 const QString dtString = QLocale().toString( dt, s_yyyy );
106 if ( fmt.contains( s_yyyy ) )
108 fmt.replace( s_yyyy, dtString );
112 fmt.replace( s_yy, dtString );
120static inline Qt::DayOfWeek qwtFirstDayOfWeek()
122 return QLocale().firstDayOfWeek();
125static inline void qwtFloorTime(
132 const Qt::TimeSpec timeSpec = dt.timeSpec();
134 if ( timeSpec == Qt::LocalTime )
135 dt = dt.toTimeSpec( Qt::UTC );
137 const QTime t = dt.time();
138 switch( intervalType )
142 dt.setTime( QTime( t.hour(), t.minute(), t.second() ) );
147 dt.setTime( QTime( t.hour(), t.minute(), 0 ) );
152 dt.setTime( QTime( t.hour(), 0, 0 ) );
159 if ( timeSpec == Qt::LocalTime )
160 dt = dt.toTimeSpec( Qt::LocalTime );
163static inline QDateTime qwtToTimeSpec(
164 const QDateTime& dt, Qt::TimeSpec spec )
166 if ( dt.timeSpec() == spec )
169 const qint64 jd = dt.date().toJulianDay();
170 if ( jd < 0 || jd >= std::numeric_limits< int >::max() )
178 dt2.setTimeSpec( spec );
182 return dt.toTimeSpec( spec );
187static inline double qwtToJulianDay(
int year,
int month,
int day )
192 const int m1 = ( month - 14 ) / 12;
193 const int m2 = ( 367 * ( month - 2 - 12 * m1 ) ) / 12;
194 const double y1 = std::floor( ( 4900.0 + year + m1 ) / 100 );
196 return std::floor( ( 1461.0 * ( year + 4800 + m1 ) ) / 4 ) + m2
197 - std::floor( ( 3 * y1 ) / 4 ) + day - 32075;
200static inline qint64 qwtFloorDiv64( qint64 a,
int b )
208static inline qint64 qwtFloorDiv(
int a,
int b )
218static inline QDate qwtToDate(
int year,
int month = 1,
int day = 1 )
220#if QT_VERSION >= 0x050000
221 return QDate( year, month, day );
228 const int m1 = ( month - 14 ) / 12;
229 const int m2 = ( 367 * ( month - 2 - 12 * m1 ) ) / 12;
230 const double y1 = std::floor( ( 4900.0 + year + m1 ) / 100 );
232 const double jd = std::floor( ( 1461.0 * ( year + 4800 + m1 ) ) / 4 ) + m2
233 - std::floor( ( 3 * y1 ) / 4 ) + day - 32075;
235 if ( jd > maxJulianDayD )
237 qWarning() <<
"qwtToDate: overflow";
241 return QDate::fromJulianDay(
static_cast< QwtJulianDay
>( jd ) );
245 return QDate( year, month, day );
263 const int msecsPerDay = 86400000;
265 const double days =
static_cast< qint64
>( std::floor( value / msecsPerDay ) );
268 if ( ( jd > maxJulianDayD ) || ( jd < minJulianDayD ) )
270 qWarning() <<
"QwtDate::toDateTime: overflow";
274 const QDate d = QDate::fromJulianDay(
static_cast< QwtJulianDay
>( jd ) );
276 const int msecs =
static_cast< int >( value - days * msecsPerDay );
278 static const QTime timeNull( 0, 0, 0, 0 );
280 QDateTime dt( d, timeNull.addMSecs( msecs ), Qt::UTC );
282 if ( timeSpec == Qt::LocalTime )
283 dt = qwtToTimeSpec( dt, timeSpec );
300 const int msecsPerDay = 86400000;
302 const QDateTime dt = qwtToTimeSpec( dateTime, Qt::UTC );
306 const QTime time = dt.time();
307 const double secs = 3600.0 * time.hour() +
308 60.0 * time.minute() + time.second();
310 return days * msecsPerDay + time.msec() + 1000.0 * secs;
328 QDateTime dt = dateTime;
330 switch ( intervalType )
340 dt = dt.addSecs( 1 );
348 dt = dt.addSecs( 60 );
356 dt = dt.addSecs( 3600 );
362 dt.setTime( QTime( 0, 0 ) );
364 dt = dt.addDays( 1 );
370 dt.setTime( QTime( 0, 0 ) );
372 dt = dt.addDays( 1 );
374 int days = qwtFirstDayOfWeek() - dt.date().dayOfWeek();
378 dt = dt.addDays( days );
384 dt.setTime( QTime( 0, 0 ) );
385 dt.setDate( qwtToDate( dateTime.date().year(),
386 dateTime.date().month() ) );
389 dt = dt.addMonths( 1 );
395 dt.setTime( QTime( 0, 0 ) );
397 const QDate d = dateTime.date();
400 if ( d.month() > 1 || d.day() > 1 || !dateTime.time().isNull() )
406 dt.setDate( qwtToDate( year ) );
431 QDateTime dt = dateTime;
433 switch ( intervalType )
443 qwtFloorTime( intervalType, dt );
448 dt.setTime( QTime( 0, 0 ) );
453 dt.setTime( QTime( 0, 0 ) );
455 int days = dt.date().dayOfWeek() - qwtFirstDayOfWeek();
459 dt = dt.addDays( -days );
465 dt.setTime( QTime( 0, 0 ) );
467 const QDate date = qwtToDate( dt.date().year(),
475 dt.setTime( QTime( 0, 0 ) );
477 const QDate date = qwtToDate( dt.date().year() );
502 if ( !date.isValid() )
503 date = QDate::fromJulianDay( minJulianDayD );
524 if ( !date.isValid() )
525 date = QDate::fromJulianDay( maxJulianDayD );
544 const Qt::DayOfWeek firstDayOfWeek = qwtFirstDayOfWeek();
546 QDate dt0( year, 1, 1 );
549 int days = dt0.dayOfWeek() - firstDayOfWeek;
553 dt0 = dt0.addDays( -days );
560 int d = Qt::Thursday - firstDayOfWeek;
564 if ( dt0.addDays( d ).year() < year )
565 dt0 = dt0.addDays( 7 );
593 if ( date.month() == 12 && date.day() >= 24 )
599 if ( day0.daysTo( date ) < 0 )
607 weekNo = day0.daysTo( date ) / 7 + 1;
611 weekNo = date.weekNumber();
639 switch( dateTime.timeSpec() )
645 case Qt::OffsetFromUTC:
647#if QT_VERSION >= 0x050200
648 seconds = dateTime.offsetFromUtc();
650 seconds = dateTime.utcOffset();
656 const QDateTime dt1( dateTime.date(), dateTime.time(), Qt::UTC );
657 seconds = dateTime.secsTo( dt1 );
687 const QString& format,
Week0Type week0Type )
689 QString fmt = format;
690 if ( fmt.contains(
'w' ) )
692 fmt = qwtExpandedFormat( fmt, dateTime, week0Type );
695 return QLocale().toString( dateTime, fmt );
static QDateTime floor(const QDateTime &, IntervalType)
static QString toString(const QDateTime &, const QString &format, Week0Type)
@ JulianDayForEpoch
The Julian day of "The Epoch".
static QDateTime toDateTime(double value, Qt::TimeSpec=Qt::UTC)
static int weekNumber(const QDate &, Week0Type)
static QDateTime ceil(const QDateTime &, IntervalType)
static QDate dateOfWeek0(int year, Week0Type)
Date of the first day of the first week for a year.
@ Month
The interval is related to months.
@ Day
The interval is related to days.
@ Millisecond
The interval is related to milliseconds.
@ Minute
The interval is related to minutes.
@ Hour
The interval is related to hours.
@ Second
The interval is related to seconds.
@ Week
The interval is related to weeks.
@ Year
The interval is related to years.
static int utcOffset(const QDateTime &)
static double toDouble(const QDateTime &)