📄 qdatetime.cpp
字号:
/****************************************************************************** $Id: qt/src/tools/qdatetime.cpp 2.2.3 edited 2000-10-11 $**** Implementation of date and time classes**** Created : 940124**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of the tools module of the Qt GUI Toolkit.**** This file may be distributed under the terms of the Q Public License** as defined by Trolltech AS of Norway and appearing in the file** LICENSE.QPL included in the packaging of this file.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition** licenses may use this file in accordance with the Qt Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Qt Commercial License Agreements.** See http://www.trolltech.com/qpl/ for QPL licensing information.** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#define gettimeofday __hide_gettimeofday#include "qdatetime.h"#include "qdatastream.h"#include <stdio.h>#include <time.h>#if defined(_OS_WIN32_)#if defined(_CC_BOOL_DEF_)#undef bool#include <windows.h>#define bool int#else#include <windows.h>#endif#elif defined(_OS_MSDOS_)#include <dos.h>#elif defined(_OS_OS2_)#include <os2.h>#elif defined(_OS_UNIX_)#include <sys/time.h>#include <unistd.h>#undef gettimeofdayextern "C" int gettimeofday( struct timeval *, struct timezone * );#endifstatic const uint FIRST_DAY = 2361222; // Julian day for 1752/09/14static const int FIRST_YEAR = 1752; // ### wrong for many countriesstatic const uint SECS_PER_DAY = 86400;static const uint MSECS_PER_DAY = 86400000;static const uint SECS_PER_HOUR = 3600;static const uint MSECS_PER_HOUR= 3600000;static const uint SECS_PER_MIN = 60;static const uint MSECS_PER_MIN = 60000;static const short monthDays[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};// ##### Localize.const char * const QDate::monthNames[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };const char * const QDate::weekdayNames[] ={ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };/***************************************************************************** QDate member functions *****************************************************************************/// REVISED: aavit/*! \class QDate qdatetime.h \brief The QDate class provides date functions. \ingroup time A QDate object contains a calendar date, i.e. year, month, and day numbers in the modern western (Gregorian) calendar. It can read the current date from the system clock. It provides functions for comparing dates and for manipulating a date by adding a number of days. A QDate object is typically created either by giving the year, month and day numbers explicitly, or by using the static function currentDate(), which makes a QDate object which contains the system's clock date. An explicit date can also be set using setYMD(). The year(), month(), and day() functions provide access to the year, month, and day numbers. Also, dayOfWeek() and dayOfYear() functions are provided. The same information is provided in textual format by the toString(), dayName(), and monthName() functions. QDate provides a full set of operators to compare two QDate objects. A date is considered smaller than another if it is earlier than the other. The date a given number of days later than a given date can be found using the addDays() function. Correspondingly, the number of days between two dates can be found using the daysTo() function. The daysInMonth() and daysInYear() functions tell how many days there are in this date's month and year, respectively. The isLeapYear() function tells whether this date is in a leap year. Note that QDate may not be used for date calculations for dates in the remote past, i.e. prior to the introduction of the Gregorian calendar. This calendar was adopted by England Sep. 14. 1752 (hence this is the earliest valid QDate), and subsequently by most other western countries, until 1923. The end of time is reached around 8000AD, by which time we expect Qt to be obsolete. \sa QTime, QDateTime*//*! \fn QDate::QDate() Constructs a null date. Null dates are invalid. \sa isNull(), isValid()*//*! Constructs a date with the year \a y, month \a m and day \a d. \a y must be in the range 1752-ca. 8000, \a m must be in the range 1-12, and \a d must be in the range 1-31. Exception: if \a y is in the range 0-99, it is interpreted as 1900-1999. \sa isValid()*/QDate::QDate( int y, int m, int d ){ jd = 0; setYMD( y, m, d );}/*! \fn bool QDate::isNull() const Returns TRUE if the date is null. A null date is invalid. \sa isValid()*//*! Returns TRUE if this date is valid. \sa isNull()*/bool QDate::isValid() const{ return jd >= FIRST_DAY;}/*! Returns the year (>= 1752) of this date. \sa month(), day()*/int QDate::year() const{ int y, m, d; jul2greg( jd, y, m, d ); return y;}/*! Returns the month (January=1 .. December=12) of this date. \sa year(), day()*/int QDate::month() const{ int y, m, d; jul2greg( jd, y, m, d ); return m;}/*! Returns the day of the month (1..31) of this date. \sa year(), month(), dayOfWeek()*/int QDate::day() const{ int y, m, d; jul2greg( jd, y, m, d ); return d;}/*! Returns the weekday (Monday=1 .. Sunday=7) for this date. \sa day(), dayOfYear()*/int QDate::dayOfWeek() const{ return (((jd+1) % 7) + 6)%7 + 1;}/*! Returns the day of the year (1..365) for this date. \sa day(), dayOfWeek()*/int QDate::dayOfYear() const{ return jd - greg2jul(year(), 1, 1) + 1;}/*! Returns the number of days in the month (28..31) for this date. \sa day(), daysInYear()*/int QDate::daysInMonth() const{ int y, m, d; jul2greg( jd, y, m, d ); if ( m == 2 && leapYear(y) ) return 29; else return monthDays[m];}/*! Returns the number of days in the year (365 or 366) for this date. \sa day(), daysInMonth()*/int QDate::daysInYear() const{ int y, m, d; jul2greg( jd, y, m, d ); return leapYear(y) ? 366 : 365;}/*! Returns the name of the \a month. Month 1 == "Jan", month 2 == "Feb" etc. \sa toString(), dayName()*/QString QDate::monthName( int month ) const{#if defined(CHECK_RANGE) if ( month < 1 || month > 12 ) { qWarning( "QDate::monthName: Parameter out ouf range." ); month = 1; }#endif // ### Remove the fromLatin1 during localization return QString::fromLatin1(monthNames[month-1]);}/*! Returns the name of the \a weekday. Weekday 1 == "Mon", day 2 == "Tue" etc. \sa toString(), monthName()*/QString QDate::dayName( int weekday ) const{#if defined(CHECK_RANGE) if ( weekday < 1 || weekday > 7 ) { qWarning( "QDate::dayName: Parameter out of range." ); weekday = 1; }#endif // ### Remove the fromLatin1 during localization return QString::fromLatin1(weekdayNames[weekday-1]);}/*! Returns the date as a string. The string format is "Sat May 20 1995". This function uses the dayName() and monthName() functions to generate the string. \sa dayName(), monthName()*/QString QDate::toString() const{ int y, m, d; jul2greg( jd, y, m, d ); QString buf = dayName(dayOfWeek()); buf += ' '; buf += monthName(m); QString t; t.sprintf( " %d %d", d, y); buf += t; return buf;}/*! Sets the year \a y, month \a m and day \a d. \a y must be in the range 1752-ca. 8000, \a m must be in the range 1-12, and \a d must be in the range 1-31. Exception: if \a y is in the range 0-99, it is interpreted as 1900-1999. Returns TRUE if the date is valid, otherwise FALSE.*/bool QDate::setYMD( int y, int m, int d ){ if ( !isValid(y,m,d) ) {#if defined(CHECK_RANGE) qWarning( "QDate::setYMD: Invalid date %04d/%02d/%02d", y, m, d );#endif return FALSE; } jd = greg2jul( y, m, d );#if defined(DEBUG) ASSERT( year() == (y > 99 ? y : 1900+y) && month() == m && day() == d );#endif return TRUE;}/*! Returns a QDate object containing a date \a ndays later than the date of this object (or earlier if \a ndays is negative). \sa daysTo()*/QDate QDate::addDays( int ndays ) const{ QDate d; d.jd = jd + ndays; return d;}/*! Returns the number of days from this date to \a d (which is negative if \a d is earlier than this date). Example: \code QDate d1( 1995, 5, 17 ); // May 17th 1995 QDate d2( 1995, 5, 20 ); // May 20th 1995 d1.daysTo( d2 ); // returns 3 d2.daysTo( d1 ); // returns -3 \endcode \sa addDays()*/int QDate::daysTo( const QDate &d ) const{ return d.jd - jd;}/*! \fn bool QDate::operator==( const QDate &d ) const Returns TRUE if this date is equal to \a d, or FALSE if they are different.*//*! \fn bool QDate::operator!=( const QDate &d ) const Returns TRUE if this date is different from \a d, or FALSE if they are equal.*//*! \fn bool QDate::operator<( const QDate &d ) const Returns TRUE if this date is earlier than \a d, otherwise FALSE.*//*! \fn bool QDate::operator<=( const QDate &d ) const Returns TRUE if this date is earlier than or equal to \a d, otherwise FALSE.*//*! \fn bool QDate::operator>( const QDate &d ) const Returns TRUE if this date is later than \a d, otherwise FALSE.*//*! \fn bool QDate::operator>=( const QDate &d ) const Returns TRUE if this date is later than or equal to \a d, otherwise FALSE.*//*! Returns the current date, as reported by the system clock. \sa QTime::currentTime(), QDateTime::currentDateTime()*/QDate QDate::currentDate(){#if defined(_OS_WIN32_) SYSTEMTIME t; GetLocalTime( &t ); QDate d; d.jd = greg2jul( t.wYear, t.wMonth, t.wDay ); return d;#else time_t ltime; time( <ime ); tm *t = localtime( <ime ); QDate d; d.jd = greg2jul( t->tm_year + 1900, t->tm_mon + 1, t->tm_mday ); return d;#endif}/*! Returns TRUE if the specified date (year \a y, month \a m and day \a d) is valid. Example: \code QDate::isValid( 2002, 5, 17 ); // TRUE; May 17th 2002 is OK. QDate::isValid( 2002, 2, 30 ); // FALSE; Feb 30th does not exist QDate::isValid( 2004, 2, 29 ); // TRUE; 2004 is a leap year QDate::isValid( 1202, 6, 6 ); // FALSE; 1202 is pre-Gregorian \endcode Note that a \a y value in the range 00-99 is interpreted as 1900-1999. \sa isNull(), setYMD()*/bool QDate::isValid( int y, int m, int d ){ if ( y >= 0 && y <= 99 )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -