qwt_date.cpp 15.7 KB
Newer Older
Bryant's avatar
Bryant committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
#include "qwt_date.h"
#include <qdebug.h>
#include <qlocale.h>
#include <math.h>
#include <limits>
#include <limits.h>

#if QT_VERSION >= 0x050000

typedef qint64 QwtJulianDay;
static const QwtJulianDay minJulianDayD = Q_INT64_C( -784350574879 );
static const QwtJulianDay maxJulianDayD = Q_INT64_C( 784354017364 );

#else

// QDate stores the Julian day as unsigned int, but
// but it is QDate::fromJulianDay( int ). That's why
// we have the range [ 1, INT_MAX ]
typedef int QwtJulianDay;
static const QwtJulianDay minJulianDayD = 1;
static const QwtJulianDay maxJulianDayD = std::numeric_limits<int>::max();

#endif

static inline Qt::DayOfWeek qwtFirstDayOfWeek()
{
#if QT_VERSION >= 0x040800
    return QLocale().firstDayOfWeek();
#else

    switch( QLocale().country() )
    {
        case QLocale::Maldives:
            return Qt::Friday;

        case QLocale::Afghanistan:
        case QLocale::Algeria:
        case QLocale::Bahrain:
        case QLocale::Djibouti:
        case QLocale::Egypt:
        case QLocale::Eritrea:
        case QLocale::Ethiopia:
        case QLocale::Iran:
        case QLocale::Iraq:
        case QLocale::Jordan:
        case QLocale::Kenya:
        case QLocale::Kuwait:
        case QLocale::LibyanArabJamahiriya:
        case QLocale::Morocco:
        case QLocale::Oman:
        case QLocale::Qatar:
        case QLocale::SaudiArabia:
        case QLocale::Somalia:
        case QLocale::Sudan:
        case QLocale::Tunisia:
        case QLocale::Yemen:
            return Qt::Saturday;

        case QLocale::AmericanSamoa:
        case QLocale::Argentina:
        case QLocale::Azerbaijan:
        case QLocale::Botswana:
        case QLocale::Canada:
        case QLocale::China:
        case QLocale::FaroeIslands:
        case QLocale::Georgia:
        case QLocale::Greenland:
        case QLocale::Guam:
        case QLocale::HongKong:
        case QLocale::Iceland:
        case QLocale::India:
        case QLocale::Ireland:
        case QLocale::Israel:
        case QLocale::Jamaica:
        case QLocale::Japan:
        case QLocale::Kyrgyzstan:
        case QLocale::Lao:
        case QLocale::Malta:
        case QLocale::MarshallIslands:
        case QLocale::Macau:
        case QLocale::Mongolia:
        case QLocale::NewZealand:
        case QLocale::NorthernMarianaIslands:
        case QLocale::Pakistan:
        case QLocale::Philippines:
        case QLocale::RepublicOfKorea:
        case QLocale::Singapore:
        case QLocale::SyrianArabRepublic:
        case QLocale::Taiwan:
        case QLocale::Thailand:
        case QLocale::TrinidadAndTobago:
        case QLocale::UnitedStates:
        case QLocale::UnitedStatesMinorOutlyingIslands:
        case QLocale::USVirginIslands:
        case QLocale::Uzbekistan:
        case QLocale::Zimbabwe:
            return Qt::Sunday;

        default:
            return Qt::Monday;
    }
#endif
}

static inline void qwtFloorTime( 
    QwtDate::IntervalType intervalType, QDateTime &dt )
{
    // when dt is inside the special hour where DST is ending
    // an hour is no unique. Therefore we have to
    // use UTC time.

    const Qt::TimeSpec timeSpec = dt.timeSpec();

    if ( timeSpec == Qt::LocalTime )
        dt = dt.toTimeSpec( Qt::UTC );

    const QTime t = dt.time();
    switch( intervalType )
    {
        case QwtDate::Second:
        {
            dt.setTime( QTime( t.hour(), t.minute(), t.second() ) );
            break;
        }
        case QwtDate::Minute:
        {
            dt.setTime( QTime( t.hour(), t.minute(), 0 ) );
            break;
        }
        case QwtDate::Hour:
        {
            dt.setTime( QTime( t.hour(), 0, 0 ) );
            break;
        }   
        default:
            break;
    }

    if ( timeSpec == Qt::LocalTime )
        dt = dt.toTimeSpec( Qt::LocalTime );
}

static inline QDateTime qwtToTimeSpec( 
    const QDateTime &dt, Qt::TimeSpec spec )
{
    if ( dt.timeSpec() == spec )
        return dt;

    const qint64 jd = dt.date().toJulianDay();
    if ( jd < 0 || jd >= INT_MAX )
    {
        // the conversion between local time and UTC
        // is internally limited. To avoid
        // overflows we simply ignore the difference
        // for those dates

        QDateTime dt2 = dt;
        dt2.setTimeSpec( spec );
        return dt2;
    }

    return dt.toTimeSpec( spec );
}

static inline double qwtToJulianDay( int year, int month, int day )
{
    // code from QDate but using doubles to avoid overflows
    // for large values

    const int m1 = ( month - 14 ) / 12;
    const int m2 = ( 367 * ( month - 2 - 12 * m1 ) ) / 12;
    const double y1 = ::floor( ( 4900.0 + year + m1 ) / 100 );

    return ::floor( ( 1461.0 * ( year + 4800 + m1 ) ) / 4 ) + m2
            - ::floor( ( 3 * y1 ) / 4 ) + day - 32075;
}

static inline qint64 qwtFloorDiv64( qint64 a, int b )
{
    if ( a < 0 )
        a -= b - 1;

    return a / b;
}

static inline qint64 qwtFloorDiv( int a, int b )
{
    if ( a < 0 )
        a -= b - 1;
        
    return a / b;
}   

static inline QDate qwtToDate( int year, int month = 1, int day = 1 )
{
#if QT_VERSION >= 0x050000
    return QDate( year, month, day );
#else
    if ( year > 100000 )
    {
        // code from QDate but using doubles to avoid overflows
        // for large values

        const int m1 = ( month - 14 ) / 12;
        const int m2 = ( 367 * ( month - 2 - 12 * m1 ) ) / 12;
        const double y1 = ::floor( ( 4900.0 + year + m1 ) / 100 );

        const double jd = ::floor( ( 1461.0 * ( year + 4800 + m1 ) ) / 4 ) + m2
            - ::floor( ( 3 * y1 ) / 4 ) + day - 32075;

        if ( jd > maxJulianDayD )
        {
            qWarning() << "qwtToDate: overflow";
            return QDate();
        }

        return QDate::fromJulianDay( static_cast<QwtJulianDay>( jd ) );
    }
    else
    {
        return QDate( year, month, day );
    }
#endif
}

/*!
  Translate from double to QDateTime

  \param value Number of milliseconds since the epoch, 
               1970-01-01T00:00:00 UTC
  \param timeSpec Time specification
  \return Datetime value

  \sa toDouble(), QDateTime::setMSecsSinceEpoch()
  \note The return datetime for Qt::OffsetFromUTC will be Qt::UTC
 */
QDateTime QwtDate::toDateTime( double value, Qt::TimeSpec timeSpec )
{
    const int msecsPerDay = 86400000;

    const double days = static_cast<qint64>( ::floor( value / msecsPerDay ) );

    const double jd = QwtDate::JulianDayForEpoch + days;
    if ( ( jd > maxJulianDayD ) || ( jd < minJulianDayD ) )
    {
        qWarning() << "QwtDate::toDateTime: overflow";
        return QDateTime();
    }

    const QDate d = QDate::fromJulianDay( static_cast<QwtJulianDay>( jd ) );

    const int msecs = static_cast<int>( value - days * msecsPerDay );

    static const QTime timeNull( 0, 0, 0, 0 );

    QDateTime dt( d, timeNull.addMSecs( msecs ), Qt::UTC );

    if ( timeSpec == Qt::LocalTime )
        dt = qwtToTimeSpec( dt, timeSpec );

    return dt;
}

/*!
  Translate from QDateTime to double

  \param dateTime Datetime value
  \return Number of milliseconds since 1970-01-01T00:00:00 UTC has passed.

  \sa toDateTime(), QDateTime::toMSecsSinceEpoch()
  \warning For values very far below or above 1970-01-01 UTC rounding errors
           will happen due to the limited significance of a double.
 */
double QwtDate::toDouble( const QDateTime &dateTime )
{
    const int msecsPerDay = 86400000;

    const QDateTime dt = qwtToTimeSpec( dateTime, Qt::UTC );

    const double days = dt.date().toJulianDay() - QwtDate::JulianDayForEpoch;

    const QTime time = dt.time();
    const double secs = 3600.0 * time.hour() + 
        60.0 * time.minute() + time.second();

    return days * msecsPerDay + time.msec() + 1000.0 * secs;
}

/*!
  Ceil a datetime according the interval type

  \param dateTime Datetime value
  \param intervalType Interval type, how to ceil. 
                      F.e. when intervalType = QwtDate::Months, the result
                      will be ceiled to the next beginning of a month
  \return Ceiled datetime
  \sa floor()
 */
QDateTime QwtDate::ceil( const QDateTime &dateTime, IntervalType intervalType )
{
    if ( dateTime.date() >= QwtDate::maxDate() )
        return dateTime;

    QDateTime dt = dateTime;

    switch ( intervalType )
    {
        case QwtDate::Millisecond:
        {
            break;
        }
        case QwtDate::Second:
        {
            qwtFloorTime( QwtDate::Second, dt );
            if ( dt < dateTime )
                dt.addSecs( 1 );

            break;
        }
        case QwtDate::Minute:
        {
            qwtFloorTime( QwtDate::Minute, dt );
            if ( dt < dateTime )
                dt.addSecs( 60 );

            break;
        }
        case QwtDate::Hour:
        {
            qwtFloorTime( QwtDate::Hour, dt );
            if ( dt < dateTime )
                dt.addSecs( 3600 );

            break;
        }
        case QwtDate::Day:
        {
            dt.setTime( QTime( 0, 0 ) );
            if ( dt < dateTime )
                dt = dt.addDays( 1 );

            break;
        }
        case QwtDate::Week:
        {
            dt.setTime( QTime( 0, 0 ) );
            if ( dt < dateTime )
                dt = dt.addDays( 1 );

            int days = qwtFirstDayOfWeek() - dt.date().dayOfWeek();
            if ( days < 0 )
                days += 7;

            dt = dt.addDays( days );

            break;
        }
        case QwtDate::Month:
        {
            dt.setTime( QTime( 0, 0 ) );
            dt.setDate( qwtToDate( dateTime.date().year(), 
                dateTime.date().month() ) );

            if ( dt < dateTime )
                dt.addMonths( 1 );

            break;
        }
        case QwtDate::Year:
        {
            dt.setTime( QTime( 0, 0 ) );

            const QDate d = dateTime.date();

            int year = d.year();
            if ( d.month() > 1 || d.day() > 1 || !dateTime.time().isNull() )
                year++;

            if ( year == 0 )
                year++; // there is no year 0

            dt.setDate( qwtToDate( year ) );
            break;
        }
    }

    return dt;
}

/*!
  Floor a datetime according the interval type

  \param dateTime Datetime value
  \param intervalType Interval type, how to ceil. 
                      F.e. when intervalType = QwtDate::Months,
                      the result will be ceiled to the next 
                      beginning of a month
  \return Floored datetime
  \sa floor()
 */
QDateTime QwtDate::floor( const QDateTime &dateTime, 
    IntervalType intervalType )
{
    if ( dateTime.date() <= QwtDate::minDate() )
        return dateTime;

    QDateTime dt = dateTime;

    switch ( intervalType )
    {
        case QwtDate::Millisecond:
        {
            break;
        }
        case QwtDate::Second:
        case QwtDate::Minute:
        case QwtDate::Hour:
        {
            qwtFloorTime( intervalType, dt );
            break;
        }
        case QwtDate::Day:
        {
            dt.setTime( QTime( 0, 0 ) );
            break;
        }
        case QwtDate::Week:
        {
            dt.setTime( QTime( 0, 0 ) );

            int days = dt.date().dayOfWeek() - qwtFirstDayOfWeek();
            if ( days < 0 )
                days += 7;

            dt = dt.addDays( -days );

            break;
        }
        case QwtDate::Month:
        {
            dt.setTime( QTime( 0, 0 ) );

            const QDate date = qwtToDate( dt.date().year(), 
                dt.date().month() );
            dt.setDate( date );

            break;
        }
        case QwtDate::Year:
        {
            dt.setTime( QTime( 0, 0 ) );

            const QDate date = qwtToDate( dt.date().year() );
            dt.setDate( date );

            break;
        }
    }

    return dt;
}

/*!
  Minimum for the supported date range

  The range of valid dates depends on how QDate stores the 
  Julian day internally.

  - For Qt4 it is "Tue Jan 2 -4713"
  - For Qt5 it is "Thu Jan 1 -2147483648"

  \return minimum of the date range
  \sa maxDate()
 */
QDate QwtDate::minDate()
{
    static QDate date;
    if ( !date.isValid() )
        date = QDate::fromJulianDay( minJulianDayD );

    return date;
}

/*!
  Maximum for the supported date range

  The range of valid dates depends on how QDate stores the 
  Julian day internally.

  - For Qt4 it is "Tue Jun 3 5874898"
  - For Qt5 it is "Tue Dec 31 2147483647"

  \return maximum of the date range
  \sa minDate()
  \note The maximum differs between Qt4 and Qt5
 */
QDate QwtDate::maxDate()
{
    static QDate date;
    if ( !date.isValid() )
        date = QDate::fromJulianDay( maxJulianDayD );

    return date;
}

/*!
  \brief Date of the first day of the first week for a year

  The first day of a week depends on the current locale
  ( QLocale::firstDayOfWeek() ). 

  \param year Year
  \param type Option how to identify the first week
  \return First day of week 0

  \sa QLocale::firstDayOfWeek(), weekNumber()
 */ 
QDate QwtDate::dateOfWeek0( int year, Week0Type type )
{
    const Qt::DayOfWeek firstDayOfWeek = qwtFirstDayOfWeek();

    QDate dt0( year, 1, 1 );

    // floor to the first day of the week
    int days = dt0.dayOfWeek() - firstDayOfWeek;
    if ( days < 0 )
        days += 7;

    dt0 = dt0.addDays( -days );

    if ( type == QwtDate::FirstThursday )
    {
        // according to ISO 8601 the first week is defined
        // by the first thursday. 

        int d = Qt::Thursday - firstDayOfWeek;
        if ( d < 0 )
            d += 7;

        if ( dt0.addDays( d ).year() < year )
            dt0 = dt0.addDays( 7 );
    }

    return dt0;
}

/*!
  Find the week number of a date

  - QwtDate::FirstThursday\n
    Corresponding to ISO 8601 ( see QDate::weekNumber() ). 

  - QwtDate::FirstDay\n
    Number of weeks that have begun since dateOfWeek0().

  \param date Date
  \param type Option how to identify the first week

  \return Week number, starting with 1
 */
int QwtDate::weekNumber( const QDate &date, Week0Type type )
{
    int weekNo;

    if ( type == QwtDate::FirstDay )
    {
        const QDate day0 = dateOfWeek0( date.year(), type );
        weekNo = day0.daysTo( date ) / 7 + 1;
    }
    else
    {
        weekNo = date.weekNumber();
    }

    return weekNo;
}

/*!
   Offset in seconds from Coordinated Universal Time

   The offset depends on the time specification of dateTime:

   - Qt::UTC
     0, dateTime has no offset
   - Qt::OffsetFromUTC
     returns dateTime.utcOffset()
   - Qt::LocalTime:
     number of seconds from the UTC

   For Qt::LocalTime the offset depends on the timezone and
   daylight savings.

   \param dateTime Datetime value
   \return Offset in seconds
 */
int QwtDate::utcOffset( const QDateTime &dateTime )
{
    int seconds = 0;

    switch( dateTime.timeSpec() )
    {
        case Qt::UTC:
        {
            break;
        }
        case Qt::OffsetFromUTC:
        {
            seconds = dateTime.utcOffset();
        }
        default:
        {
            const QDateTime dt1( dateTime.date(), dateTime.time(), Qt::UTC );
            seconds = dateTime.secsTo( dt1 );
        }
    }

    return seconds;
}

/*!
  Translate a datetime into a string

  Beside the format expressions documented in QDateTime::toString()
  the following expressions are supported:

  - w\n
    week number: ( 1 - 53 )
  - ww\n
    week number with a leading zero ( 01 - 53 )

  \param dateTime Datetime value
  \param format Format string
  \param week0Type Specification of week 0

  \return Datetime string
  \sa QDateTime::toString(), weekNumber(), QwtDateScaleDraw
 */
QString QwtDate::toString( const QDateTime &dateTime,
    const QString & format, Week0Type week0Type )
{
    QString weekNo;
    weekNo.setNum( QwtDate::weekNumber( dateTime.date(), week0Type ) );

    QString weekNoWW;
    if ( weekNo.length() == 1 )
        weekNoWW += "0";
    weekNoWW += weekNo;

    QString fmt = format;
    fmt.replace( "ww", weekNoWW );
    fmt.replace( "w", weekNo );

    return dateTime.toString( fmt );
}