18 #if QT_VERSION >= 0x050000
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 );
122 return QLocale().firstDayOfWeek();
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 );
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 );
187 static 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;
200 static inline qint64 qwtFloorDiv64( qint64 a,
int b )
208 static inline qint64 qwtFloorDiv(
int a,
int b )
218 static 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;
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 ) );
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 )
300 const int msecsPerDay = 86400000;
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 );
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() )
431 QDateTime dt = dateTime;
433 switch ( intervalType )
448 dt.setTime( QTime( 0, 0 ) );
453 dt.setTime( QTime( 0, 0 ) );
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() )
524 if ( !date.isValid() )
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 );
690 if ( fmt.contains(
'w' ) )
695 return QLocale().toString( dateTime, fmt );