📄 qdatetime.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qplatformdefs.h"#include "private/qdatetime_p.h"#include "qdatastream.h"#include "qset.h"#include "qlocale.h"#include "qdatetime.h"#include "qregexp.h"#include "qdebug.h"#if defined(Q_OS_WIN32)#include <windows.h>#endif#ifndef Q_WS_WIN#include <locale.h>#endif#include <time.h>//#define QDATETIMEPARSER_DEBUG#if defined (QDATETIMEPARSER_DEBUG) && !defined(QT_NO_DEBUG_STREAM)# define QDTPDEBUG qDebug() << QString("%1:%2").arg(__FILE__).arg(__LINE__)# define QDTPDEBUGN qDebug#else# define QDTPDEBUG if (false) qDebug()# define QDTPDEBUGN if (false) qDebug#endif#if defined(Q_WS_MAC)#include <private/qcore_mac_p.h>extern QString qt_mac_from_pascal_string(const Str255); // qglobal.cpp#endifenum { FIRST_DAY = 2361222, // Julian day for 1752-09-14 FIRST_YEAR = 1752, SECS_PER_DAY = 86400, MSECS_PER_DAY = 86400000, SECS_PER_HOUR = 3600, MSECS_PER_HOUR = 3600000, SECS_PER_MIN = 60, MSECS_PER_MIN = 60000};static const short monthDays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };#ifndef QT_NO_TEXTDATEstatic const char * const qt_shortMonthNames[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };#endif#ifndef QT_NO_DATESTRINGstatic QString fmtDateTime(const QString& f, const QTime* dt = 0, const QDate* dd = 0);#endif/***************************************************************************** QDate member functions *****************************************************************************//*! \class QDate \reentrant \brief The QDate class provides date functions. \ingroup time \mainclass 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 dates. For example, it is possible to add and subtract days, months, and years to dates. A QDate object is typically created either by giving the year, month, and day numbers explicitly, or by using the static function currentDate() that creates a QDate object containing the system clock's date. An explicit date can also be set using setYMD(). The fromString() function returns a QDate given a string and a date format which is used to interpret the date within the string. 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(), shortDayName(), longDayName(), shortMonthName(), and longMonthName() functions. QDate provides a full set of operators to compare two QDate objects where smaller means earlier, and larger means later. You can increment (or decrement) a date by a given number of days using addDays(). Similarly you can use addMonths() and addYears(). The daysTo() function returns the number of days between two dates. The daysInMonth() and daysInYear() functions return how many days there are in this date's month and year, respectively. The isLeapYear() function indicates whether this date is in a leap year. Note that QDate should not be used for date calculations for dates prior to the introduction of the Gregorian calendar. This calendar was adopted by England from the 14 September 1752 (hence this is the earliest valid QDate), and subsequently by most other Western countries, by 1923. The latest valid date within this scheme is 31 December 7999. \sa QTime QDateTime QDateEdit QDateTimeEdit*//*! \fn QDate::QDate() Constructs a null date. Null dates are invalid. \sa isNull(), isValid()*//*! Constructs a date with year \a y, month \a m and day \a d. \a y must be in the range 1752 to 8000, \a m must be in the range 1 to 12, and \a d must be in the range 1 to 31. \warning If \a y is in the range 0 to 99, it is interpreted as a year in the range 1900 to 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; otherwise returns false. A null date is invalid. \sa isValid()*//*! Returns true if this date is valid; otherwise returns false. \sa isNull()*/bool QDate::isValid() const{ return jd >= FIRST_DAY;}/*! Returns the year (1752 to 8000) of this date. \sa month(), day()*/int QDate::year() const{ int y, m, d; julianToGregorian(jd, y, m, d); return y;}/*! Returns the number corresponding to the month of this date, using the following convention: \list \i 1 = "January" \i 2 = "February" \i 3 = "March" \i 4 = "April" \i 5 = "May" \i 6 = "June" \i 7 = "July" \i 8 = "August" \i 9 = "September" \i 10 = "October" \i 11 = "November" \i 12 = "December" \endlist \sa year(), day()*/int QDate::month() const{ int y, m, d; julianToGregorian(jd, y, m, d); return m;}/*! Returns the day of the month (1 to 31) of this date. \sa year(), month(), dayOfWeek()*/int QDate::day() const{ int y, m, d; julianToGregorian(jd, y, m, d); return d;}/*! Returns the weekday (1 to 7) for this date. \sa day(), dayOfYear(), Qt::DayOfWeek*/int QDate::dayOfWeek() const{ return (jd % 7) + 1;}/*! Returns the day of the year (1 to 365) for this date. \sa day(), dayOfWeek()*/int QDate::dayOfYear() const{ return jd - gregorianToJulian(year(), 1, 1) + 1;}/*! Returns the number of days in the month (28 to 31) for this date. \sa day(), daysInYear()*/int QDate::daysInMonth() const{ int y, m, d; julianToGregorian(jd, y, m, d); if (m == 2 && isLeapYear(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; julianToGregorian(jd, y, m, d); return isLeapYear(y) ? 366 : 365;}/*! Returns the week number (1 to 53), and stores the year in *\a{yearNumber} unless \a yearNumber is null (the default). Returns 0 if the date is invalid. In accordance with ISO 8601, weeks start on Qt::Monday and the first Qt::Thursday of a year is always in week 1 of that year. Most years have 52 weeks, but some have 53. *\a{yearNumber} is not always the same as year(). For example, 1 January 2000 has week number 52 in the year 1999, and 31 December 2002 has week number 1 in the year 2003. \legalese Copyright (c) 1989 The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms are permitted provided that the above copyright notice and this paragraph are duplicated in all such forms and that any documentation, advertising materials, and other materials related to such distribution and use acknowledge that the software was developed by the University of California, Berkeley. The name of the University may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. \sa isValid()*/int QDate::weekNumber(int *yearNumber) const{ if (!isValid()) return 0; int year = QDate::year(); int yday = dayOfYear() - 1; int wday = dayOfWeek(); if (wday == 7) wday = 0; int w; for (;;) { int len; int bot; int top; len = isLeapYear(year) ? 366 : 365; /* ** What yday (-3 ... 3) does ** the ISO year begin on? */ bot = ((yday + 11 - wday) % 7) - 3; /* ** What yday does the NEXT ** ISO year begin on? */ top = bot - (len % 7); if (top < -3) top += 7; top += len; if (yday >= top) { ++year; w = 1; break; } if (yday >= bot) { w = 1 + ((yday - bot) / 7); break; } --year; yday += isLeapYear(year) ? 366 : 365; } if (yearNumber != 0) *yearNumber = year; return w;}#ifndef QT_NO_TEXTDATE/*! Returns the name of the \a month using the following convention: \list \i 1 = "Jan" \i 2 = "Feb" \i 3 = "Mar" \i 4 = "Apr" \i 5 = "May" \i 6 = "Jun" \i 7 = "Jul" \i 8 = "Aug" \i 9 = "Sep" \i 10 = "Oct" \i 11 = "Nov" \i 12 = "Dec" \endlist The month names will be localized according to the system's locale settings. \sa toString(), longMonthName(), shortDayName(), longDayName()*/QString QDate::shortMonthName(int month){ if (month < 1 || month > 12) { qWarning("QDate::shortMonthName: Parameter out ouf range"); month = 1; }#ifndef Q_WS_WIN char buffer[255]; tm tt; memset(&tt, 0, sizeof(tm)); tt.tm_mon = month - 1; const QByteArray lctime(setlocale(LC_TIME, "")); if (strftime(buffer, sizeof(buffer), "%b", &tt)) { setlocale(LC_TIME, lctime.data()); return QString::fromLocal8Bit(buffer); } setlocale(LC_TIME, lctime.data());#else SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); st.wYear = 2000; st.wMonth = month; st.wDay = 1; QT_WA({ const wchar_t mmm_t[] = L"MMM"; // workaround for Borland TCHAR buf[255]; if (GetDateFormat(GetThreadLocale(), 0, &st, mmm_t, buf, 255)) return QString::fromUtf16((ushort*)buf); } , { char buf[255]; if (GetDateFormatA(GetThreadLocale(), 0, &st, "MMM", (char*)&buf, 255)) return QString::fromLocal8Bit(buf); });#endif return QString();}/*! Returns the long name of the \a month using the following convention: \list \i 1 = "January" \i 2 = "February" \i 3 = "March" \i 4 = "April" \i 5 = "May" \i 6 = "June" \i 7 = "July" \i 8 = "August" \i 9 = "September" \i 10 = "October" \i 11 = "November" \i 12 = "December" \endlist The month names will be localized according to the system's locale settings. \sa toString(), shortMonthName(), shortDayName(), longDayName()*/QString QDate::longMonthName(int month){ if (month < 1 || month > 12) { qWarning("QDate::longMonthName: Parameter out ouf range"); month = 1; }#ifndef Q_WS_WIN char buffer[255]; tm tt; memset(&tt, 0, sizeof(tm)); tt.tm_mon = month - 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -