Changeset 11158 in ntrip for trunk/BNC/qwt/qwt_date.cpp
- Timestamp:
- Nov 3, 2026, 10:53:35 AM (3 weeks ago)
- File:
-
- 1 edited
-
trunk/BNC/qwt/qwt_date.cpp (modified) (28 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/BNC/qwt/qwt_date.cpp
r9383 r11158 1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*-*****************************1 /****************************************************************************** 2 2 * Qwt Widget Library 3 3 * Copyright (C) 1997 Josef Wilgen … … 9 9 10 10 #include "qwt_date.h" 11 #include "qwt_math.h" 12 11 13 #include <qdebug.h> 12 14 #include <qlocale.h> 13 #include <math.h> 15 14 16 #include <limits> 15 #include <limits.h>16 17 17 18 #if QT_VERSION >= 0x050000 … … 24 25 25 26 // QDate stores the Julian day as unsigned int, but 26 // but itis QDate::fromJulianDay( int ). That's why27 // there is QDate::fromJulianDay( int ). That's why 27 28 // we have the range [ 1, INT_MAX ] 29 28 30 typedef int QwtJulianDay; 29 31 static const QwtJulianDay minJulianDayD = 1; 30 static const QwtJulianDay maxJulianDayD = std::numeric_limits< int>::max();32 static const QwtJulianDay maxJulianDayD = std::numeric_limits< int >::max(); 31 33 32 34 #endif 33 35 34 static QString qwtExpandedFormat( const QString & format,35 const QDateTime &dateTime, QwtDate::Week0Type week0Type )36 static QString qwtExpandedFormat( const QString& format, 37 const QDateTime& dateTime, QwtDate::Week0Type week0Type ) 36 38 { 37 39 const int week = QwtDate::weekNumber( dateTime.date(), week0Type ); … … 42 44 QString weekNoWW; 43 45 if ( weekNo.length() == 1 ) 44 weekNoWW += "0";46 weekNoWW += QLatin1Char( '0' ); 45 47 46 48 weekNoWW += weekNo; 47 49 48 50 QString fmt = format; 49 fmt.replace( "ww", weekNoWW );50 fmt.replace( "w", weekNo );51 fmt.replace( QLatin1String( "ww" ), weekNoWW ); 52 fmt.replace( QLatin1Char( 'w' ), weekNo ); 51 53 52 54 if ( week == 1 && dateTime.date().month() != 1 ) … … 54 56 // in case of week 1, we might need to increment the year 55 57 56 static QString s_yyyy="yyyy";57 static QString s_yy= "yy";58 QLatin1String s_yyyy( "yyyy" ); 59 QLatin1String s_yy( "yy" ); 58 60 59 61 // week 1 might start in the previous year … … 100 102 { 101 103 const QDate dt( dateTime.date().year() + 1, 1, 1 ); 104 const QString dtString = QLocale().toString( dt, s_yyyy ); 102 105 103 106 if ( fmt.contains( s_yyyy ) ) 104 107 { 105 fmt.replace( s_yyyy, dt .toString( s_yyyy ));108 fmt.replace( s_yyyy, dtString ); 106 109 } 107 110 else 108 111 { 109 fmt.replace( s_yy, dt .toString( s_yyyy ));112 fmt.replace( s_yy, dtString ); 110 113 } 111 114 } … … 117 120 static inline Qt::DayOfWeek qwtFirstDayOfWeek() 118 121 { 119 #if QT_VERSION >= 0x040800120 122 return QLocale().firstDayOfWeek(); 121 #else122 123 switch( QLocale().country() )124 {125 case QLocale::Maldives:126 return Qt::Friday;127 128 case QLocale::Afghanistan:129 case QLocale::Algeria:130 case QLocale::Bahrain:131 case QLocale::Djibouti:132 case QLocale::Egypt:133 case QLocale::Eritrea:134 case QLocale::Ethiopia:135 case QLocale::Iran:136 case QLocale::Iraq:137 case QLocale::Jordan:138 case QLocale::Kenya:139 case QLocale::Kuwait:140 case QLocale::LibyanArabJamahiriya:141 case QLocale::Morocco:142 case QLocale::Oman:143 case QLocale::Qatar:144 case QLocale::SaudiArabia:145 case QLocale::Somalia:146 case QLocale::Sudan:147 case QLocale::Tunisia:148 case QLocale::Yemen:149 return Qt::Saturday;150 151 case QLocale::AmericanSamoa:152 case QLocale::Argentina:153 case QLocale::Azerbaijan:154 case QLocale::Botswana:155 case QLocale::Canada:156 case QLocale::China:157 case QLocale::FaroeIslands:158 case QLocale::Georgia:159 case QLocale::Greenland:160 case QLocale::Guam:161 case QLocale::HongKong:162 case QLocale::Iceland:163 case QLocale::India:164 case QLocale::Ireland:165 case QLocale::Israel:166 case QLocale::Jamaica:167 case QLocale::Japan:168 case QLocale::Kyrgyzstan:169 case QLocale::Lao:170 case QLocale::Malta:171 case QLocale::MarshallIslands:172 case QLocale::Macau:173 case QLocale::Mongolia:174 case QLocale::NewZealand:175 case QLocale::NorthernMarianaIslands:176 case QLocale::Pakistan:177 case QLocale::Philippines:178 case QLocale::RepublicOfKorea:179 case QLocale::Singapore:180 case QLocale::SyrianArabRepublic:181 case QLocale::Taiwan:182 case QLocale::Thailand:183 case QLocale::TrinidadAndTobago:184 case QLocale::UnitedStates:185 case QLocale::UnitedStatesMinorOutlyingIslands:186 case QLocale::USVirginIslands:187 case QLocale::Uzbekistan:188 case QLocale::Zimbabwe:189 return Qt::Sunday;190 191 default:192 return Qt::Monday;193 }194 #endif195 123 } 196 124 197 125 static inline void qwtFloorTime( 198 QwtDate::IntervalType intervalType, QDateTime &dt )126 QwtDate::IntervalType intervalType, QDateTime& dt ) 199 127 { 200 128 // when dt is inside the special hour where DST is ending … … 234 162 235 163 static inline QDateTime qwtToTimeSpec( 236 const QDateTime &dt, Qt::TimeSpec spec )164 const QDateTime& dt, Qt::TimeSpec spec ) 237 165 { 238 166 if ( dt.timeSpec() == spec ) … … 240 168 241 169 const qint64 jd = dt.date().toJulianDay(); 242 if ( jd < 0 || jd >= INT_MAX)170 if ( jd < 0 || jd >= std::numeric_limits< int >::max() ) 243 171 { 244 172 // the conversion between local time and UTC … … 264 192 const int m1 = ( month - 14 ) / 12; 265 193 const int m2 = ( 367 * ( month - 2 - 12 * m1 ) ) / 12; 266 const double y1 = ::floor( ( 4900.0 + year + m1 ) / 100 ); 267 268 return ::floor( ( 1461.0 * ( year + 4800 + m1 ) ) / 4 ) + m2 269 -::floor( ( 3 * y1 ) / 4 ) + day - 32075;194 const double y1 = std::floor( ( 4900.0 + year + m1 ) / 100 ); 195 196 return std::floor( ( 1461.0 * ( year + 4800 + m1 ) ) / 4 ) + m2 197 - std::floor( ( 3 * y1 ) / 4 ) + day - 32075; 270 198 } 271 199 … … 300 228 const int m1 = ( month - 14 ) / 12; 301 229 const int m2 = ( 367 * ( month - 2 - 12 * m1 ) ) / 12; 302 const double y1 = ::floor( ( 4900.0 + year + m1 ) / 100 ); 303 304 const double jd = ::floor( ( 1461.0 * ( year + 4800 + m1 ) ) / 4 ) + m2 305 - ::floor( ( 3 * y1 ) / 4 ) + day - 32075; 230 const double y1 = std::floor( ( 4900.0 + year + m1 ) / 100 ); 231 232 const double jd = std::floor( ( 1461.0 * ( year + 4800 + m1 ) ) / 4 ) + m2 233 - std::floor( ( 3 * y1 ) / 4 ) + day - 32075; 306 234 307 235 if ( jd > maxJulianDayD ) … … 311 239 } 312 240 313 return QDate::fromJulianDay( static_cast<QwtJulianDay>( jd ) ); 241 return QDate::fromJulianDay( static_cast< QwtJulianDay >( jd ) ); 314 242 } 315 243 else … … 321 249 322 250 /*! 323 Translate from double to QDateTime 324 325 \param value Number of milliseconds since the epoch, 251 Translate from double to QDateTime 252 253 \param value Number of milliseconds since the epoch, 326 254 1970-01-01T00:00:00 UTC 327 \param timeSpec Time specification 328 \return Datetime value 329 330 \sa toDouble(), QDateTime::setMSecsSinceEpoch() 331 \note The return datetime for Qt::OffsetFromUTC will be Qt::UTC 255 \param timeSpec Time specification 256 \return Datetime value 257 258 \sa toDouble(), QDateTime::setMSecsSinceEpoch() 259 \note The return datetime for Qt::OffsetFromUTC will be Qt::UTC 332 260 */ 333 261 QDateTime QwtDate::toDateTime( double value, Qt::TimeSpec timeSpec ) … … 335 263 const int msecsPerDay = 86400000; 336 264 337 const double days = static_cast< qint64>(::floor( value / msecsPerDay ) );265 const double days = static_cast< qint64 >( std::floor( value / msecsPerDay ) ); 338 266 339 267 const double jd = QwtDate::JulianDayForEpoch + days; … … 344 272 } 345 273 346 const QDate d = QDate::fromJulianDay( static_cast<QwtJulianDay>( jd ) ); 347 348 const int msecs = static_cast< int>( value - days * msecsPerDay );274 const QDate d = QDate::fromJulianDay( static_cast< QwtJulianDay >( jd ) ); 275 276 const int msecs = static_cast< int >( value - days * msecsPerDay ); 349 277 350 278 static const QTime timeNull( 0, 0, 0, 0 ); … … 359 287 360 288 /*! 361 Translate from QDateTime to double 362 363 \param dateTime Datetime value 364 \return Number of milliseconds since 1970-01-01T00:00:00 UTC has passed. 365 366 \sa toDateTime(), QDateTime::toMSecsSinceEpoch() 367 \warning For values very far below or above 1970-01-01 UTC rounding errors 289 Translate from QDateTime to double 290 291 \param dateTime Datetime value 292 \return Number of milliseconds since 1970-01-01T00:00:00 UTC has passed. 293 294 \sa toDateTime(), QDateTime::toMSecsSinceEpoch() 295 \warning For values very far below or above 1970-01-01 UTC rounding errors 368 296 will happen due to the limited significance of a double. 369 297 */ 370 double QwtDate::toDouble( const QDateTime &dateTime )298 double QwtDate::toDouble( const QDateTime& dateTime ) 371 299 { 372 300 const int msecsPerDay = 86400000; … … 384 312 385 313 /*! 386 Ceil a datetime according the interval type 387 388 \param dateTime Datetime value 389 \param intervalType Interval type, how to ceil. 314 Ceil a datetime according the interval type 315 316 \param dateTime Datetime value 317 \param intervalType Interval type, how to ceil. 390 318 F.e. when intervalType = QwtDate::Months, the result 391 319 will be ceiled to the next beginning of a month 392 \return Ceiled datetime 393 \sa floor() 394 */ 395 QDateTime QwtDate::ceil( const QDateTime &dateTime, IntervalType intervalType )320 \return Ceiled datetime 321 \sa floor() 322 */ 323 QDateTime QwtDate::ceil( const QDateTime& dateTime, IntervalType intervalType ) 396 324 { 397 325 if ( dateTime.date() >= QwtDate::maxDate() ) … … 485 413 486 414 /*! 487 Floor a datetime according the interval type 488 489 \param dateTime Datetime value 490 \param intervalType Interval type, how to ceil. 415 Floor a datetime according the interval type 416 417 \param dateTime Datetime value 418 \param intervalType Interval type, how to ceil. 491 419 F.e. when intervalType = QwtDate::Months, 492 420 the result will be ceiled to the next 493 421 beginning of a month 494 \return Floored datetime 495 \sa floor() 496 */ 497 QDateTime QwtDate::floor( const QDateTime &dateTime,422 \return Floored datetime 423 \sa floor() 424 */ 425 QDateTime QwtDate::floor( const QDateTime& dateTime, 498 426 IntervalType intervalType ) 499 427 { … … 558 486 559 487 /*! 560 Minimum for the supported date range 561 562 The range of valid dates depends on how QDate stores the 563 Julian day internally. 564 565 - For Qt4 it is "Tue Jan 2 -4713" 566 - For Qt5 it is "Thu Jan 1 -2147483648" 567 568 \return minimum of the date range 569 \sa maxDate() 488 Minimum for the supported date range 489 490 The range of valid dates depends on how QDate stores the 491 Julian day internally. 492 493 - For Qt4 it is "Tue Jan 2 -4713" 494 - For Qt5 it is "Thu Jan 1 -2147483648" 495 496 \return minimum of the date range 497 \sa maxDate() 570 498 */ 571 499 QDate QwtDate::minDate() … … 579 507 580 508 /*! 581 Maximum for the supported date range 582 583 The range of valid dates depends on how QDate stores the 584 Julian day internally. 585 586 - For Qt4 it is "Tue Jun 3 5874898" 587 - For Qt5 it is "Tue Dec 31 2147483647" 588 589 \return maximum of the date range 590 \sa minDate() 591 \note The maximum differs between Qt4 and Qt5 509 Maximum for the supported date range 510 511 The range of valid dates depends on how QDate stores the 512 Julian day internally. 513 514 - For Qt4 it is "Tue Jun 3 5874898" 515 - For Qt5 it is "Tue Dec 31 2147483647" 516 517 \return maximum of the date range 518 \sa minDate() 519 \note The maximum differs between Qt4 and Qt5 592 520 */ 593 521 QDate QwtDate::maxDate() … … 601 529 602 530 /*! 603 \brief Date of the first day of the first week for a year 604 605 The first day of a week depends on the current locale 606 ( QLocale::firstDayOfWeek() ). 607 608 \param year Year 609 \param type Option how to identify the first week 610 \return First day of week 0 611 612 \sa QLocale::firstDayOfWeek(), weekNumber() 531 \brief Date of the first day of the first week for a year 532 533 The first day of a week depends on the current locale 534 ( QLocale::firstDayOfWeek() ). 535 536 \param year Year 537 \param type Option how to identify the first week 538 \return First day of week 0 539 540 \sa QLocale::firstDayOfWeek(), weekNumber() 613 541 */ 614 542 QDate QwtDate::dateOfWeek0( int year, Week0Type type ) … … 628 556 { 629 557 // according to ISO 8601 the first week is defined 630 // by the first thursday.558 // by the first Thursday. 631 559 632 560 int d = Qt::Thursday - firstDayOfWeek; … … 642 570 643 571 /*! 644 Find the week number of a date 645 646 - QwtDate::FirstThursday\n 572 Find the week number of a date 573 574 - QwtDate::FirstThursday\n 647 575 Corresponding to ISO 8601 ( see QDate::weekNumber() ). 648 576 649 - QwtDate::FirstDay\n 577 - QwtDate::FirstDay\n 650 578 Number of weeks that have begun since dateOfWeek0(). 651 579 652 \param date Date 653 \param type Option how to identify the first week 654 655 \return Week number, starting with 1 656 */ 657 int QwtDate::weekNumber( const QDate &date, Week0Type type )580 \param date Date 581 \param type Option how to identify the first week 582 583 \return Week number, starting with 1 584 */ 585 int QwtDate::weekNumber( const QDate& date, Week0Type type ) 658 586 { 659 587 int weekNo; … … 695 623 0, dateTime has no offset 696 624 - Qt::OffsetFromUTC 697 returns dateTime. utcOffset()625 returns dateTime.offsetFromUtc() 698 626 - Qt::LocalTime: 699 627 number of seconds from the UTC … … 705 633 \return Offset in seconds 706 634 */ 707 int QwtDate::utcOffset( const QDateTime &dateTime )635 int QwtDate::utcOffset( const QDateTime& dateTime ) 708 636 { 709 637 int seconds = 0; … … 717 645 case Qt::OffsetFromUTC: 718 646 { 647 #if QT_VERSION >= 0x050200 648 seconds = dateTime.offsetFromUtc(); 649 #else 719 650 seconds = dateTime.utcOffset(); 651 #endif 720 652 break; 721 653 } … … 731 663 732 664 /*! 733 Translate a datetime into a string 734 735 Beside the format expressions documented in QDateTime::toString() 736 the following expressions are supported: 737 738 - w\n 665 Translate a datetime into a string 666 667 Beside the format expressions documented in QDateTime::toString() 668 the following expressions are supported: 669 670 - w\n 739 671 week number: ( 1 - 53 ) 740 - ww\n 672 - ww\n 741 673 week number with a leading zero ( 01 - 53 ) 742 674 743 As week 1 usually starts in the previous year a special rule 744 is applied for formats, where the year is expected to match the 745 week number - even if the date belongs to the previous year. 746 747 \param dateTime Datetime value 748 \param format Format string 749 \param week0Type Specification of week 0 750 751 \return Datetime string 752 \sa QDateTime::toString(), weekNumber(), QwtDateScaleDraw 753 */ 754 QString QwtDate::toString( const QDateTime &dateTime,755 const QString & format, Week0Type week0Type )675 As week 1 usually starts in the previous year a special rule 676 is applied for formats, where the year is expected to match the 677 week number - even if the date belongs to the previous year. 678 679 \param dateTime Datetime value 680 \param format Format string 681 \param week0Type Specification of week 0 682 683 \return Datetime string 684 \sa QDateTime::toString(), weekNumber(), QwtDateScaleDraw 685 */ 686 QString QwtDate::toString( const QDateTime& dateTime, 687 const QString& format, Week0Type week0Type ) 756 688 { 757 689 QString fmt = format; … … 761 693 } 762 694 763 return dateTime.toString( fmt );764 } 695 return QLocale().toString( dateTime, fmt ); 696 }
Note:
See TracChangeset
for help on using the changeset viewer.
