PageRenderTime 56ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/qt/src/corelib/tools/qdatetime.cpp

https://github.com/sosana/phantomjs
C++ | 5931 lines | 3282 code | 538 blank | 2111 comment | 920 complexity | 8e4a04e734c4053bba95b9b259557974 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, CC-BY-SA-4.0, GPL-3.0, LGPL-2.1, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the QtCore module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qplatformdefs.h"
  42. #include "private/qdatetime_p.h"
  43. #include "qdatastream.h"
  44. #include "qset.h"
  45. #include "qlocale.h"
  46. #include "qdatetime.h"
  47. #include "qregexp.h"
  48. #include "qdebug.h"
  49. #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
  50. #include <qt_windows.h>
  51. #endif
  52. #ifndef Q_WS_WIN
  53. #include <locale.h>
  54. #endif
  55. #include <time.h>
  56. #if defined(Q_OS_WINCE)
  57. #include "qfunctions_wince.h"
  58. #endif
  59. //#define QDATETIMEPARSER_DEBUG
  60. #if defined (QDATETIMEPARSER_DEBUG) && !defined(QT_NO_DEBUG_STREAM)
  61. # define QDTPDEBUG qDebug() << QString("%1:%2").arg(__FILE__).arg(__LINE__)
  62. # define QDTPDEBUGN qDebug
  63. #else
  64. # define QDTPDEBUG if (false) qDebug()
  65. # define QDTPDEBUGN if (false) qDebug
  66. #endif
  67. #if defined(Q_WS_MAC)
  68. #include <private/qcore_mac_p.h>
  69. #endif
  70. #if defined(Q_OS_SYMBIAN)
  71. #include <e32std.h>
  72. #include <tz.h>
  73. #endif
  74. QT_BEGIN_NAMESPACE
  75. enum {
  76. FIRST_YEAR = -4713,
  77. FIRST_MONTH = 1,
  78. FIRST_DAY = 2, // ### Qt 5: make FIRST_DAY = 1, by support jd == 0 as valid
  79. SECS_PER_DAY = 86400,
  80. MSECS_PER_DAY = 86400000,
  81. SECS_PER_HOUR = 3600,
  82. MSECS_PER_HOUR = 3600000,
  83. SECS_PER_MIN = 60,
  84. MSECS_PER_MIN = 60000,
  85. JULIAN_DAY_FOR_EPOCH = 2440588 // result of julianDayFromGregorianDate(1970, 1, 1)
  86. };
  87. static inline QDate fixedDate(int y, int m, int d)
  88. {
  89. QDate result(y, m, 1);
  90. result.setDate(y, m, qMin(d, result.daysInMonth()));
  91. return result;
  92. }
  93. static inline uint julianDayFromGregorianDate(int year, int month, int day)
  94. {
  95. // Gregorian calendar starting from October 15, 1582
  96. // Algorithm from Henry F. Fliegel and Thomas C. Van Flandern
  97. return (1461 * (year + 4800 + (month - 14) / 12)) / 4
  98. + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12
  99. - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4
  100. + day - 32075;
  101. }
  102. static uint julianDayFromDate(int year, int month, int day)
  103. {
  104. if (year < 0)
  105. ++year;
  106. if (year > 1582 || (year == 1582 && (month > 10 || (month == 10 && day >= 15)))) {
  107. return julianDayFromGregorianDate(year, month, day);
  108. } else if (year < 1582 || (year == 1582 && (month < 10 || (month == 10 && day <= 4)))) {
  109. // Julian calendar until October 4, 1582
  110. // Algorithm from Frequently Asked Questions about Calendars by Claus Toendering
  111. int a = (14 - month) / 12;
  112. return (153 * (month + (12 * a) - 3) + 2) / 5
  113. + (1461 * (year + 4800 - a)) / 4
  114. + day - 32083;
  115. } else {
  116. // the day following October 4, 1582 is October 15, 1582
  117. return 0;
  118. }
  119. }
  120. static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
  121. {
  122. int y, m, d;
  123. if (julianDay >= 2299161) {
  124. // Gregorian calendar starting from October 15, 1582
  125. // This algorithm is from Henry F. Fliegel and Thomas C. Van Flandern
  126. qulonglong ell, n, i, j;
  127. ell = qulonglong(julianDay) + 68569;
  128. n = (4 * ell) / 146097;
  129. ell = ell - (146097 * n + 3) / 4;
  130. i = (4000 * (ell + 1)) / 1461001;
  131. ell = ell - (1461 * i) / 4 + 31;
  132. j = (80 * ell) / 2447;
  133. d = ell - (2447 * j) / 80;
  134. ell = j / 11;
  135. m = j + 2 - (12 * ell);
  136. y = 100 * (n - 49) + i + ell;
  137. } else {
  138. // Julian calendar until October 4, 1582
  139. // Algorithm from Frequently Asked Questions about Calendars by Claus Toendering
  140. julianDay += 32082;
  141. int dd = (4 * julianDay + 3) / 1461;
  142. int ee = julianDay - (1461 * dd) / 4;
  143. int mm = ((5 * ee) + 2) / 153;
  144. d = ee - (153 * mm + 2) / 5 + 1;
  145. m = mm + 3 - 12 * (mm / 10);
  146. y = dd - 4800 + (mm / 10);
  147. if (y <= 0)
  148. --y;
  149. }
  150. if (year)
  151. *year = y;
  152. if (month)
  153. *month = m;
  154. if (day)
  155. *day = d;
  156. }
  157. static const char monthDays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  158. #ifndef QT_NO_TEXTDATE
  159. static const char * const qt_shortMonthNames[] = {
  160. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  161. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  162. #endif
  163. #ifndef QT_NO_DATESTRING
  164. static QString fmtDateTime(const QString& f, const QTime* dt = 0, const QDate* dd = 0);
  165. #endif
  166. /*****************************************************************************
  167. QDate member functions
  168. *****************************************************************************/
  169. /*!
  170. \since 4.5
  171. \enum QDate::MonthNameType
  172. This enum describes the types of the string representation used
  173. for the month name.
  174. \value DateFormat This type of name can be used for date-to-string formatting.
  175. \value StandaloneFormat This type is used when you need to enumerate months or weekdays.
  176. Usually standalone names are represented in singular forms with
  177. capitalized first letter.
  178. */
  179. /*!
  180. \class QDate
  181. \reentrant
  182. \brief The QDate class provides date functions.
  183. A QDate object contains a calendar date, i.e. year, month, and day
  184. numbers, in the Gregorian calendar. (see \l{QDate G and J} {Use of
  185. Gregorian and Julian Calendars} for dates prior to 15 October
  186. 1582). It can read the current date from the system clock. It
  187. provides functions for comparing dates, and for manipulating
  188. dates. For example, it is possible to add and subtract days,
  189. months, and years to dates.
  190. A QDate object is typically created either by giving the year,
  191. month, and day numbers explicitly. Note that QDate interprets two
  192. digit years as is, i.e., years 0 - 99. A QDate can also be
  193. constructed with the static function currentDate(), which creates
  194. a QDate object containing the system clock's date. An explicit
  195. date can also be set using setDate(). The fromString() function
  196. returns a QDate given a string and a date format which is used to
  197. interpret the date within the string.
  198. The year(), month(), and day() functions provide access to the
  199. year, month, and day numbers. Also, dayOfWeek() and dayOfYear()
  200. functions are provided. The same information is provided in
  201. textual format by the toString(), shortDayName(), longDayName(),
  202. shortMonthName(), and longMonthName() functions.
  203. QDate provides a full set of operators to compare two QDate
  204. objects where smaller means earlier, and larger means later.
  205. You can increment (or decrement) a date by a given number of days
  206. using addDays(). Similarly you can use addMonths() and addYears().
  207. The daysTo() function returns the number of days between two
  208. dates.
  209. The daysInMonth() and daysInYear() functions return how many days
  210. there are in this date's month and year, respectively. The
  211. isLeapYear() function indicates whether a date is in a leap year.
  212. \section1
  213. \target QDate G and J
  214. \section2 Use of Gregorian and Julian Calendars
  215. QDate uses the Gregorian calendar in all locales, beginning
  216. on the date 15 October 1582. For dates up to and including 4
  217. October 1582, the Julian calendar is used. This means there is a
  218. 10-day gap in the internal calendar between the 4th and the 15th
  219. of October 1582. When you use QDateTime for dates in that epoch,
  220. the day after 4 October 1582 is 15 October 1582, and the dates in
  221. the gap are invalid.
  222. The Julian to Gregorian changeover date used here is the date when
  223. the Gregorian calendar was first introduced, by Pope Gregory
  224. XIII. That change was not universally accepted and some localities
  225. only executed it at a later date (if at all). QDateTime
  226. doesn't take any of these historical facts into account. If an
  227. application must support a locale-specific dating system, it must
  228. do so on its own, remembering to convert the dates using the
  229. Julian day.
  230. \section2 No Year 0
  231. There is no year 0. Dates in that year are considered invalid. The
  232. year -1 is the year "1 before Christ" or "1 before current era."
  233. The day before 0001-01-01 is December 31st, 1 BCE.
  234. \section2 Range of Valid Dates
  235. The range of valid dates is from January 2nd, 4713 BCE, to
  236. sometime in the year 11 million CE. The Julian Day returned by
  237. QDate::toJulianDay() is a number in the contiguous range from 1 to
  238. \e{overflow}, even across QDateTime's "date holes". It is suitable
  239. for use in applications that must convert a QDateTime to a date in
  240. another calendar system, e.g., Hebrew, Islamic or Chinese.
  241. \sa QTime, QDateTime, QDateEdit, QDateTimeEdit, QCalendarWidget
  242. */
  243. /*!
  244. \fn QDate::QDate()
  245. Constructs a null date. Null dates are invalid.
  246. \sa isNull(), isValid()
  247. */
  248. /*!
  249. Constructs a date with year \a y, month \a m and day \a d.
  250. If the specified date is invalid, the date is not set and
  251. isValid() returns false. A date before 2 January 4713 B.C. is
  252. considered invalid.
  253. \warning Years 0 to 99 are interpreted as is, i.e., years
  254. 0-99.
  255. \sa isValid()
  256. */
  257. QDate::QDate(int y, int m, int d)
  258. {
  259. setDate(y, m, d);
  260. }
  261. /*!
  262. \fn bool QDate::isNull() const
  263. Returns true if the date is null; otherwise returns false. A null
  264. date is invalid.
  265. \note The behavior of this function is equivalent to isValid().
  266. \sa isValid()
  267. */
  268. /*!
  269. Returns true if this date is valid; otherwise returns false.
  270. \sa isNull()
  271. */
  272. bool QDate::isValid() const
  273. {
  274. return !isNull();
  275. }
  276. /*!
  277. Returns the year of this date. Negative numbers indicate years
  278. before 1 A.D. = 1 C.E., such that year -44 is 44 B.C.
  279. \sa month(), day()
  280. */
  281. int QDate::year() const
  282. {
  283. int y;
  284. getDateFromJulianDay(jd, &y, 0, 0);
  285. return y;
  286. }
  287. /*!
  288. Returns the number corresponding to the month of this date, using
  289. the following convention:
  290. \list
  291. \i 1 = "January"
  292. \i 2 = "February"
  293. \i 3 = "March"
  294. \i 4 = "April"
  295. \i 5 = "May"
  296. \i 6 = "June"
  297. \i 7 = "July"
  298. \i 8 = "August"
  299. \i 9 = "September"
  300. \i 10 = "October"
  301. \i 11 = "November"
  302. \i 12 = "December"
  303. \endlist
  304. \sa year(), day()
  305. */
  306. int QDate::month() const
  307. {
  308. int m;
  309. getDateFromJulianDay(jd, 0, &m, 0);
  310. return m;
  311. }
  312. /*!
  313. Returns the day of the month (1 to 31) of this date.
  314. \sa year(), month(), dayOfWeek()
  315. */
  316. int QDate::day() const
  317. {
  318. int d;
  319. getDateFromJulianDay(jd, 0, 0, &d);
  320. return d;
  321. }
  322. /*!
  323. Returns the weekday (1 = Monday to 7 = Sunday) for this date.
  324. \sa day(), dayOfYear(), Qt::DayOfWeek
  325. */
  326. int QDate::dayOfWeek() const
  327. {
  328. return (jd % 7) + 1;
  329. }
  330. /*!
  331. Returns the day of the year (1 to 365 or 366 on leap years) for
  332. this date.
  333. \sa day(), dayOfWeek()
  334. */
  335. int QDate::dayOfYear() const
  336. {
  337. return jd - julianDayFromDate(year(), 1, 1) + 1;
  338. }
  339. /*!
  340. Returns the number of days in the month (28 to 31) for this date.
  341. \sa day(), daysInYear()
  342. */
  343. int QDate::daysInMonth() const
  344. {
  345. int y, m, d;
  346. getDateFromJulianDay(jd, &y, &m, &d);
  347. if (m == 2 && isLeapYear(y))
  348. return 29;
  349. else
  350. return monthDays[m];
  351. }
  352. /*!
  353. Returns the number of days in the year (365 or 366) for this date.
  354. \sa day(), daysInMonth()
  355. */
  356. int QDate::daysInYear() const
  357. {
  358. int y, m, d;
  359. getDateFromJulianDay(jd, &y, &m, &d);
  360. return isLeapYear(y) ? 366 : 365;
  361. }
  362. /*!
  363. Returns the week number (1 to 53), and stores the year in
  364. *\a{yearNumber} unless \a yearNumber is null (the default).
  365. Returns 0 if the date is invalid.
  366. In accordance with ISO 8601, weeks start on Monday and the first
  367. Thursday of a year is always in week 1 of that year. Most years
  368. have 52 weeks, but some have 53.
  369. *\a{yearNumber} is not always the same as year(). For example, 1
  370. January 2000 has week number 52 in the year 1999, and 31 December
  371. 2002 has week number 1 in the year 2003.
  372. \legalese
  373. Copyright (c) 1989 The Regents of the University of California.
  374. All rights reserved.
  375. Redistribution and use in source and binary forms are permitted
  376. provided that the above copyright notice and this paragraph are
  377. duplicated in all such forms and that any documentation,
  378. advertising materials, and other materials related to such
  379. distribution and use acknowledge that the software was developed
  380. by the University of California, Berkeley. The name of the
  381. University may not be used to endorse or promote products derived
  382. from this software without specific prior written permission.
  383. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  384. IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  385. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  386. \sa isValid()
  387. */
  388. int QDate::weekNumber(int *yearNumber) const
  389. {
  390. if (!isValid())
  391. return 0;
  392. int year = QDate::year();
  393. int yday = dayOfYear() - 1;
  394. int wday = dayOfWeek();
  395. if (wday == 7)
  396. wday = 0;
  397. int w;
  398. for (;;) {
  399. int len;
  400. int bot;
  401. int top;
  402. len = isLeapYear(year) ? 366 : 365;
  403. /*
  404. ** What yday (-3 ... 3) does
  405. ** the ISO year begin on?
  406. */
  407. bot = ((yday + 11 - wday) % 7) - 3;
  408. /*
  409. ** What yday does the NEXT
  410. ** ISO year begin on?
  411. */
  412. top = bot - (len % 7);
  413. if (top < -3)
  414. top += 7;
  415. top += len;
  416. if (yday >= top) {
  417. ++year;
  418. w = 1;
  419. break;
  420. }
  421. if (yday >= bot) {
  422. w = 1 + ((yday - bot) / 7);
  423. break;
  424. }
  425. --year;
  426. yday += isLeapYear(year) ? 366 : 365;
  427. }
  428. if (yearNumber != 0)
  429. *yearNumber = year;
  430. return w;
  431. }
  432. #ifndef QT_NO_TEXTDATE
  433. /*!
  434. \since 4.5
  435. Returns the short name of the \a month for the representation specified
  436. by \a type.
  437. The months are enumerated using the following convention:
  438. \list
  439. \i 1 = "Jan"
  440. \i 2 = "Feb"
  441. \i 3 = "Mar"
  442. \i 4 = "Apr"
  443. \i 5 = "May"
  444. \i 6 = "Jun"
  445. \i 7 = "Jul"
  446. \i 8 = "Aug"
  447. \i 9 = "Sep"
  448. \i 10 = "Oct"
  449. \i 11 = "Nov"
  450. \i 12 = "Dec"
  451. \endlist
  452. The month names will be localized according to the system's default
  453. locale settings.
  454. \sa toString(), longMonthName(), shortDayName(), longDayName()
  455. */
  456. QString QDate::shortMonthName(int month, QDate::MonthNameType type)
  457. {
  458. if (month < 1 || month > 12) {
  459. month = 1;
  460. }
  461. switch (type) {
  462. case QDate::DateFormat:
  463. return QLocale::system().monthName(month, QLocale::ShortFormat);
  464. case QDate::StandaloneFormat:
  465. return QLocale::system().standaloneMonthName(month, QLocale::ShortFormat);
  466. default:
  467. break;
  468. }
  469. return QString();
  470. }
  471. /*!
  472. Returns the short version of the name of the \a month. The
  473. returned name is in normal type which can be used for date formatting.
  474. \sa toString(), longMonthName(), shortDayName(), longDayName()
  475. */
  476. QString QDate::shortMonthName(int month)
  477. {
  478. return shortMonthName(month, QDate::DateFormat);
  479. }
  480. /*!
  481. \since 4.5
  482. Returns the long name of the \a month for the representation specified
  483. by \a type.
  484. The months are enumerated using the following convention:
  485. \list
  486. \i 1 = "January"
  487. \i 2 = "February"
  488. \i 3 = "March"
  489. \i 4 = "April"
  490. \i 5 = "May"
  491. \i 6 = "June"
  492. \i 7 = "July"
  493. \i 8 = "August"
  494. \i 9 = "September"
  495. \i 10 = "October"
  496. \i 11 = "November"
  497. \i 12 = "December"
  498. \endlist
  499. The month names will be localized according to the system's default
  500. locale settings.
  501. \sa toString(), shortMonthName(), shortDayName(), longDayName()
  502. */
  503. QString QDate::longMonthName(int month, MonthNameType type)
  504. {
  505. if (month < 1 || month > 12) {
  506. month = 1;
  507. }
  508. switch (type) {
  509. case QDate::DateFormat:
  510. return QLocale::system().monthName(month, QLocale::LongFormat);
  511. case QDate::StandaloneFormat:
  512. return QLocale::system().standaloneMonthName(month, QLocale::LongFormat);
  513. default:
  514. break;
  515. }
  516. return QString();
  517. }
  518. /*!
  519. Returns the long version of the name of the \a month. The
  520. returned name is in normal type which can be used for date formatting.
  521. \sa toString(), shortMonthName(), shortDayName(), longDayName()
  522. */
  523. QString QDate::longMonthName(int month)
  524. {
  525. if (month < 1 || month > 12) {
  526. month = 1;
  527. }
  528. return QLocale::system().monthName(month, QLocale::LongFormat);
  529. }
  530. /*!
  531. \since 4.5
  532. Returns the short name of the \a weekday for the representation specified
  533. by \a type.
  534. The days are enumerated using the following convention:
  535. \list
  536. \i 1 = "Mon"
  537. \i 2 = "Tue"
  538. \i 3 = "Wed"
  539. \i 4 = "Thu"
  540. \i 5 = "Fri"
  541. \i 6 = "Sat"
  542. \i 7 = "Sun"
  543. \endlist
  544. The day names will be localized according to the system's default
  545. locale settings.
  546. \sa toString(), shortMonthName(), longMonthName(), longDayName()
  547. */
  548. QString QDate::shortDayName(int weekday, MonthNameType type)
  549. {
  550. if (weekday < 1 || weekday > 7) {
  551. weekday = 1;
  552. }
  553. switch (type) {
  554. case QDate::DateFormat:
  555. return QLocale::system().dayName(weekday, QLocale::ShortFormat);
  556. case QDate::StandaloneFormat:
  557. return QLocale::system().standaloneDayName(weekday, QLocale::ShortFormat);
  558. default:
  559. break;
  560. }
  561. return QString();
  562. }
  563. /*!
  564. Returns the short version of the name of the \a weekday. The
  565. returned name is in normal type which can be used for date formatting.
  566. \sa toString(), longDayName(), shortMonthName(), longMonthName()
  567. */
  568. QString QDate::shortDayName(int weekday)
  569. {
  570. if (weekday < 1 || weekday > 7) {
  571. weekday = 1;
  572. }
  573. return QLocale::system().dayName(weekday, QLocale::ShortFormat);
  574. }
  575. /*!
  576. \since 4.5
  577. Returns the long name of the \a weekday for the representation specified
  578. by \a type.
  579. The days are enumerated using the following convention:
  580. \list
  581. \i 1 = "Monday"
  582. \i 2 = "Tuesday"
  583. \i 3 = "Wednesday"
  584. \i 4 = "Thursday"
  585. \i 5 = "Friday"
  586. \i 6 = "Saturday"
  587. \i 7 = "Sunday"
  588. \endlist
  589. The day names will be localized according to the system's default
  590. locale settings.
  591. \sa toString(), shortDayName(), shortMonthName(), longMonthName()
  592. */
  593. QString QDate::longDayName(int weekday, MonthNameType type)
  594. {
  595. if (weekday < 1 || weekday > 7) {
  596. weekday = 1;
  597. }
  598. switch (type) {
  599. case QDate::DateFormat:
  600. return QLocale::system().dayName(weekday, QLocale::LongFormat);
  601. case QDate::StandaloneFormat:
  602. return QLocale::system().standaloneDayName(weekday, QLocale::LongFormat);
  603. default:
  604. break;
  605. }
  606. return QLocale::system().dayName(weekday, QLocale::LongFormat);
  607. }
  608. /*!
  609. Returns the long version of the name of the \a weekday. The
  610. returned name is in normal type which can be used for date formatting.
  611. \sa toString(), shortDayName(), shortMonthName(), longMonthName()
  612. */
  613. QString QDate::longDayName(int weekday)
  614. {
  615. if (weekday < 1 || weekday > 7) {
  616. weekday = 1;
  617. }
  618. return QLocale::system().dayName(weekday, QLocale::LongFormat);
  619. }
  620. #endif //QT_NO_TEXTDATE
  621. #ifndef QT_NO_DATESTRING
  622. /*!
  623. \fn QString QDate::toString(Qt::DateFormat format) const
  624. \overload
  625. Returns the date as a string. The \a format parameter determines
  626. the format of the string.
  627. If the \a format is Qt::TextDate, the string is formatted in
  628. the default way. QDate::shortDayName() and QDate::shortMonthName()
  629. are used to generate the string, so the day and month names will
  630. be localized names using the default locale from the system. An
  631. example of this formatting is "Sat May 20 1995".
  632. If the \a format is Qt::ISODate, the string format corresponds
  633. to the ISO 8601 extended specification for representations of
  634. dates and times, taking the form YYYY-MM-DD, where YYYY is the
  635. year, MM is the month of the year (between 01 and 12), and DD is
  636. the day of the month between 01 and 31.
  637. If the \a format is Qt::SystemLocaleShortDate or
  638. Qt::SystemLocaleLongDate, the string format depends on the locale
  639. settings of the system. Identical to calling
  640. QLocale::system().toString(date, QLocale::ShortFormat) or
  641. QLocale::system().toString(date, QLocale::LongFormat).
  642. If the \a format is Qt::DefaultLocaleShortDate or
  643. Qt::DefaultLocaleLongDate, the string format depends on the
  644. default application locale. This is the locale set with
  645. QLocale::setDefault(), or the system locale if no default locale
  646. has been set. Identical to calling QLocale().toString(date,
  647. QLocale::ShortFormat) or QLocale().toString(date,
  648. QLocale::LongFormat).
  649. If the date is invalid, an empty string will be returned.
  650. \warning The Qt::ISODate format is only valid for years in the
  651. range 0 to 9999. This restriction may apply to locale-aware
  652. formats as well, depending on the locale settings.
  653. \sa shortDayName(), shortMonthName()
  654. */
  655. QString QDate::toString(Qt::DateFormat f) const
  656. {
  657. if (!isValid())
  658. return QString();
  659. int y, m, d;
  660. getDateFromJulianDay(jd, &y, &m, &d);
  661. switch (f) {
  662. case Qt::SystemLocaleDate:
  663. case Qt::SystemLocaleShortDate:
  664. case Qt::SystemLocaleLongDate:
  665. return QLocale::system().toString(*this, f == Qt::SystemLocaleLongDate ? QLocale::LongFormat
  666. : QLocale::ShortFormat);
  667. case Qt::LocaleDate:
  668. case Qt::DefaultLocaleShortDate:
  669. case Qt::DefaultLocaleLongDate:
  670. return QLocale().toString(*this, f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat
  671. : QLocale::ShortFormat);
  672. default:
  673. #ifndef QT_NO_TEXTDATE
  674. case Qt::TextDate:
  675. {
  676. return QString::fromLatin1("%0 %1 %2 %3")
  677. .arg(shortDayName(dayOfWeek()))
  678. .arg(shortMonthName(m))
  679. .arg(d)
  680. .arg(y);
  681. }
  682. #endif
  683. case Qt::ISODate:
  684. {
  685. if (year() < 0 || year() > 9999)
  686. return QString();
  687. QString month(QString::number(m).rightJustified(2, QLatin1Char('0')));
  688. QString day(QString::number(d).rightJustified(2, QLatin1Char('0')));
  689. return QString::number(y) + QLatin1Char('-') + month + QLatin1Char('-') + day;
  690. }
  691. }
  692. }
  693. /*!
  694. Returns the date as a string. The \a format parameter determines
  695. the format of the result string.
  696. These expressions may be used:
  697. \table
  698. \header \i Expression \i Output
  699. \row \i d \i the day as number without a leading zero (1 to 31)
  700. \row \i dd \i the day as number with a leading zero (01 to 31)
  701. \row \i ddd
  702. \i the abbreviated localized day name (e.g. 'Mon' to 'Sun').
  703. Uses QDate::shortDayName().
  704. \row \i dddd
  705. \i the long localized day name (e.g. 'Monday' to 'Sunday').
  706. Uses QDate::longDayName().
  707. \row \i M \i the month as number without a leading zero (1 to 12)
  708. \row \i MM \i the month as number with a leading zero (01 to 12)
  709. \row \i MMM
  710. \i the abbreviated localized month name (e.g. 'Jan' to 'Dec').
  711. Uses QDate::shortMonthName().
  712. \row \i MMMM
  713. \i the long localized month name (e.g. 'January' to 'December').
  714. Uses QDate::longMonthName().
  715. \row \i yy \i the year as two digit number (00 to 99)
  716. \row \i yyyy \i the year as four digit number. If the year is negative,
  717. a minus sign is prepended in addition.
  718. \endtable
  719. All other input characters will be ignored. Any sequence of characters that
  720. are enclosed in singlequotes will be treated as text and not be used as an
  721. expression. Two consecutive singlequotes ("''") are replaced by a singlequote
  722. in the output.
  723. Example format strings (assuming that the QDate is the 20 July
  724. 1969):
  725. \table
  726. \header \o Format \o Result
  727. \row \o dd.MM.yyyy \o 20.07.1969
  728. \row \o ddd MMMM d yy \o Sun July 20 69
  729. \row \o 'The day is' dddd \o The day is Sunday
  730. \endtable
  731. If the datetime is invalid, an empty string will be returned.
  732. \warning The Qt::ISODate format is only valid for years in the
  733. range 0 to 9999. This restriction may apply to locale-aware
  734. formats as well, depending on the locale settings.
  735. \sa QDateTime::toString() QTime::toString()
  736. */
  737. QString QDate::toString(const QString& format) const
  738. {
  739. if (year() > 9999)
  740. return QString();
  741. return fmtDateTime(format, 0, this);
  742. }
  743. #endif //QT_NO_DATESTRING
  744. /*!
  745. \obsolete
  746. Sets the date's year \a y, month \a m, and day \a d.
  747. If \a y is in the range 0 to 99, it is interpreted as 1900 to
  748. 1999.
  749. Use setDate() instead.
  750. */
  751. bool QDate::setYMD(int y, int m, int d)
  752. {
  753. if (uint(y) <= 99)
  754. y += 1900;
  755. return setDate(y, m, d);
  756. }
  757. /*!
  758. \since 4.2
  759. Sets the date's \a year, \a month, and \a day. Returns true if
  760. the date is valid; otherwise returns false.
  761. If the specified date is invalid, the QDate object is set to be
  762. invalid. Any date before 2 January 4713 B.C. is considered
  763. invalid.
  764. \sa isValid()
  765. */
  766. bool QDate::setDate(int year, int month, int day)
  767. {
  768. if (!isValid(year, month, day)) {
  769. jd = 0;
  770. } else {
  771. jd = julianDayFromDate(year, month, day);
  772. }
  773. return jd != 0;
  774. }
  775. /*!
  776. \since 4.5
  777. Extracts the date's year, month, and day, and assigns them to
  778. *\a year, *\a month, and *\a day. The pointers may be null.
  779. \sa year(), month(), day(), isValid()
  780. */
  781. void QDate::getDate(int *year, int *month, int *day)
  782. {
  783. getDateFromJulianDay(jd, year, month, day);
  784. }
  785. /*!
  786. Returns a QDate object containing a date \a ndays later than the
  787. date of this object (or earlier if \a ndays is negative).
  788. \sa addMonths() addYears() daysTo()
  789. */
  790. QDate QDate::addDays(int ndays) const
  791. {
  792. QDate d;
  793. // this is basically "d.jd = jd + ndays" with checks for integer overflow
  794. if (ndays >= 0)
  795. d.jd = (jd + ndays >= jd) ? jd + ndays : 0;
  796. else
  797. d.jd = (jd + ndays < jd) ? jd + ndays : 0;
  798. return d;
  799. }
  800. /*!
  801. Returns a QDate object containing a date \a nmonths later than the
  802. date of this object (or earlier if \a nmonths is negative).
  803. \note If the ending day/month combination does not exist in the
  804. resulting month/year, this function will return a date that is the
  805. latest valid date.
  806. \warning QDate has a date hole around the days introducing the
  807. Gregorian calendar (the days 5 to 14 October 1582, inclusive, do
  808. not exist). If the calculation ends in one of those days, QDate
  809. will return either October 4 or October 15.
  810. \sa addDays() addYears()
  811. */
  812. QDate QDate::addMonths(int nmonths) const
  813. {
  814. if (!isValid())
  815. return QDate();
  816. if (!nmonths)
  817. return *this;
  818. int old_y, y, m, d;
  819. getDateFromJulianDay(jd, &y, &m, &d);
  820. old_y = y;
  821. bool increasing = nmonths > 0;
  822. while (nmonths != 0) {
  823. if (nmonths < 0 && nmonths + 12 <= 0) {
  824. y--;
  825. nmonths+=12;
  826. } else if (nmonths < 0) {
  827. m+= nmonths;
  828. nmonths = 0;
  829. if (m <= 0) {
  830. --y;
  831. m += 12;
  832. }
  833. } else if (nmonths - 12 >= 0) {
  834. y++;
  835. nmonths -= 12;
  836. } else if (m == 12) {
  837. y++;
  838. m = 0;
  839. } else {
  840. m += nmonths;
  841. nmonths = 0;
  842. if (m > 12) {
  843. ++y;
  844. m -= 12;
  845. }
  846. }
  847. }
  848. // was there a sign change?
  849. if ((old_y > 0 && y <= 0) ||
  850. (old_y < 0 && y >= 0))
  851. // yes, adjust the date by +1 or -1 years
  852. y += increasing ? +1 : -1;
  853. // did we end up in the Gregorian/Julian conversion hole?
  854. if (y == 1582 && m == 10 && d > 4 && d < 15)
  855. d = increasing ? 15 : 4;
  856. return fixedDate(y, m, d);
  857. }
  858. /*!
  859. Returns a QDate object containing a date \a nyears later than the
  860. date of this object (or earlier if \a nyears is negative).
  861. \note If the ending day/month combination does not exist in the
  862. resulting year (i.e., if the date was Feb 29 and the final year is
  863. not a leap year), this function will return a date that is the
  864. latest valid date (that is, Feb 28).
  865. \sa addDays(), addMonths()
  866. */
  867. QDate QDate::addYears(int nyears) const
  868. {
  869. if (!isValid())
  870. return QDate();
  871. int y, m, d;
  872. getDateFromJulianDay(jd, &y, &m, &d);
  873. int old_y = y;
  874. y += nyears;
  875. // was there a sign change?
  876. if ((old_y > 0 && y <= 0) ||
  877. (old_y < 0 && y >= 0))
  878. // yes, adjust the date by +1 or -1 years
  879. y += nyears > 0 ? +1 : -1;
  880. return fixedDate(y, m, d);
  881. }
  882. /*!
  883. Returns the number of days from this date to \a d (which is
  884. negative if \a d is earlier than this date).
  885. Example:
  886. \snippet doc/src/snippets/code/src_corelib_tools_qdatetime.cpp 0
  887. \sa addDays()
  888. */
  889. int QDate::daysTo(const QDate &d) const
  890. {
  891. return d.jd - jd;
  892. }
  893. /*!
  894. \fn bool QDate::operator==(const QDate &d) const
  895. Returns true if this date is equal to \a d; otherwise returns
  896. false.
  897. */
  898. /*!
  899. \fn bool QDate::operator!=(const QDate &d) const
  900. Returns true if this date is different from \a d; otherwise
  901. returns false.
  902. */
  903. /*!
  904. \fn bool QDate::operator<(const QDate &d) const
  905. Returns true if this date is earlier than \a d; otherwise returns
  906. false.
  907. */
  908. /*!
  909. \fn bool QDate::operator<=(const QDate &d) const
  910. Returns true if this date is earlier than or equal to \a d;
  911. otherwise returns false.
  912. */
  913. /*!
  914. \fn bool QDate::operator>(const QDate &d) const
  915. Returns true if this date is later than \a d; otherwise returns
  916. false.
  917. */
  918. /*!
  919. \fn bool QDate::operator>=(const QDate &d) const
  920. Returns true if this date is later than or equal to \a d;
  921. otherwise returns false.
  922. */
  923. /*!
  924. \fn QDate::currentDate()
  925. Returns the current date, as reported by the system clock.
  926. \sa QTime::currentTime(), QDateTime::currentDateTime()
  927. */
  928. #ifndef QT_NO_DATESTRING
  929. /*!
  930. \fn QDate QDate::fromString(const QString &string, Qt::DateFormat format)
  931. Returns the QDate represented by the \a string, using the
  932. \a format given, or an invalid date if the string cannot be
  933. parsed.
  934. Note for Qt::TextDate: It is recommended that you use the
  935. English short month names (e.g. "Jan"). Although localized month
  936. names can also be used, they depend on the user's locale settings.
  937. */
  938. QDate QDate::fromString(const QString& s, Qt::DateFormat f)
  939. {
  940. if (s.isEmpty())
  941. return QDate();
  942. switch (f) {
  943. case Qt::ISODate:
  944. {
  945. int year(s.mid(0, 4).toInt());
  946. int month(s.mid(5, 2).toInt());
  947. int day(s.mid(8, 2).toInt());
  948. if (year && month && day)
  949. return QDate(year, month, day);
  950. }
  951. break;
  952. case Qt::SystemLocaleDate:
  953. case Qt::SystemLocaleShortDate:
  954. case Qt::SystemLocaleLongDate:
  955. return fromString(s, QLocale::system().dateFormat(f == Qt::SystemLocaleLongDate ? QLocale::LongFormat
  956. : QLocale::ShortFormat));
  957. case Qt::LocaleDate:
  958. case Qt::DefaultLocaleShortDate:
  959. case Qt::DefaultLocaleLongDate:
  960. return fromString(s, QLocale().dateFormat(f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat
  961. : QLocale::ShortFormat));
  962. default:
  963. #ifndef QT_NO_TEXTDATE
  964. case Qt::TextDate: {
  965. QStringList parts = s.split(QLatin1Char(' '), QString::SkipEmptyParts);
  966. if (parts.count() != 4) {
  967. return QDate();
  968. }
  969. QString monthName = parts.at(1);
  970. int month = -1;
  971. // Assume that English monthnames are the default
  972. for (int i = 0; i < 12; ++i) {
  973. if (monthName == QLatin1String(qt_shortMonthNames[i])) {
  974. month = i + 1;
  975. break;
  976. }
  977. }
  978. // If English names can't be found, search the localized ones
  979. if (month == -1) {
  980. for (int i = 1; i <= 12; ++i) {
  981. if (monthName == QDate::shortMonthName(i)) {
  982. month = i;
  983. break;
  984. }
  985. }
  986. }
  987. if (month < 1 || month > 12) {
  988. return QDate();
  989. }
  990. bool ok;
  991. int day = parts.at(2).toInt(&ok);
  992. if (!ok) {
  993. return QDate();
  994. }
  995. int year = parts.at(3).toInt(&ok);
  996. if (!ok) {
  997. return QDate();
  998. }
  999. return QDate(year, month, day);
  1000. }
  1001. #else
  1002. break;
  1003. #endif
  1004. }
  1005. return QDate();
  1006. }
  1007. /*!
  1008. \fn QDate::fromString(const QString &string, const QString &format)
  1009. Returns the QDate represented by the \a string, using the \a
  1010. format given, or an invalid date if the string cannot be parsed.
  1011. These expressions may be used for the format:
  1012. \table
  1013. \header \i Expression \i Output
  1014. \row \i d \i The day as a number without a leading zero (1 to 31)
  1015. \row \i dd \i The day as a number with a leading zero (01 to 31)
  1016. \row \i ddd
  1017. \i The abbreviated localized day name (e.g. 'Mon' to 'Sun').
  1018. Uses QDate::shortDayName().
  1019. \row \i dddd
  1020. \i The long localized day name (e.g. 'Monday' to 'Sunday').
  1021. Uses QDate::longDayName().
  1022. \row \i M \i The month as a number without a leading zero (1 to 12)
  1023. \row \i MM \i The month as a number with a leading zero (01 to 12)
  1024. \row \i MMM
  1025. \i The abbreviated localized month name (e.g. 'Jan' to 'Dec').
  1026. Uses QDate::shortMonthName().
  1027. \row \i MMMM
  1028. \i The long localized month name (e.g. 'January' to 'December').
  1029. Uses QDate::longMonthName().
  1030. \row \i yy \i The year as two digit number (00 to 99)
  1031. \row \i yyyy \i The year as four digit number. If the year is negative,
  1032. a minus sign is prepended in addition.
  1033. \endtable
  1034. All other input characters will be treated as text. Any sequence
  1035. of characters that are enclosed in single quotes will also be
  1036. treated as text and will not be used as an expression. For example:
  1037. \snippet doc/src/snippets/code/src_corelib_tools_qdatetime.cpp 1
  1038. If the format is not satisfied, an invalid QDate is returned. The
  1039. expressions that don't expect leading zeroes (d, M) will be
  1040. greedy. This means that they will use two digits even if this
  1041. will put them outside the accepted range of values and leaves too
  1042. few digits for other sections. For example, the following format
  1043. string could have meant January 30 but the M will grab two
  1044. digits, resulting in an invalid date:
  1045. \snippet doc/src/snippets/code/src_corelib_tools_qdatetime.cpp 2
  1046. For any field that is not represented in the format the following
  1047. defaults are used:
  1048. \table
  1049. \header \i Field \i Default value
  1050. \row \i Year \i 1900
  1051. \row \i Month \i 1
  1052. \row \i Day \i 1
  1053. \endtable
  1054. The following examples demonstrate the default values:
  1055. \snippet doc/src/snippets/code/src_corelib_tools_qdatetime.cpp 3
  1056. \sa QDateTime::fromString(), QTime::fromString(), QDate::toString(),
  1057. QDateTime::toString(), QTime::toString()
  1058. */
  1059. QDate QDate::fromString(const QString &string, const QString &format)
  1060. {
  1061. QDate date;
  1062. #ifndef QT_BOOTSTRAPPED
  1063. QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString);
  1064. if (dt.parseFormat(format))
  1065. dt.fromString(string, &date, 0);
  1066. #else
  1067. Q_UNUSED(string);
  1068. Q_UNUSED(format);
  1069. #endif
  1070. return date;
  1071. }
  1072. #endif // QT_NO_DATESTRING
  1073. /*!
  1074. \overload
  1075. Returns true if the specified date (\a year, \a month, and \a
  1076. day) is valid; otherwise returns false.
  1077. Example:
  1078. \snippet doc/src/snippets/code/src_corelib_tools_qdatetime.cpp 4
  1079. \sa isNull(), setDate()
  1080. */
  1081. bool QDate::isValid(int year, int month, int day)
  1082. {
  1083. if (year < FIRST_YEAR
  1084. || (year == FIRST_YEAR &&
  1085. (month < FIRST_MONTH
  1086. || (month == FIRST_MONTH && day < FIRST_DAY)))
  1087. || year == 0) // there is no year 0 in the Julian calendar
  1088. return false;
  1089. // passage from Julian to Gregorian calendar
  1090. if (year == 1582 && month == 10 && day > 4 && day < 15)
  1091. return 0;
  1092. return (day > 0 && month > 0 && month <= 12) &&
  1093. (day <= monthDays[month] || (day == 29 && month == 2 && isLeapYear(year)));
  1094. }
  1095. /*!
  1096. \fn bool QDate::isLeapYear(int year)
  1097. Returns true if the specified \a year is a leap year; otherwise
  1098. returns false.
  1099. */
  1100. bool QDate::isLeapYear(int y)
  1101. {
  1102. if (y < 1582) {
  1103. if ( y < 1) { // No year 0 in Julian calendar, so -1, -5, -9 etc are leap years
  1104. ++y;
  1105. }
  1106. return y % 4 == 0;
  1107. } else {
  1108. return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
  1109. }
  1110. }
  1111. /*!
  1112. \internal
  1113. This function has a confusing name and shouldn't be part of the
  1114. API anyway, since we have toJulian() and fromJulian().
  1115. ### Qt 5: remove it
  1116. */
  1117. uint QDate::gregorianToJulian(int y, int m, int d)
  1118. {
  1119. return julianDayFromDate(y, m, d);
  1120. }
  1121. /*!
  1122. \internal
  1123. This function has a confusing name and shouldn't be part of the
  1124. API anyway, since we have toJulian() and fromJulian().
  1125. ### Qt 5: remove it
  1126. */
  1127. void QDate::julianToGregorian(uint jd, int &y, int &m, int &d)
  1128. {
  1129. getDateFromJulianDay(jd, &y, &m, &d);
  1130. }
  1131. /*! \fn static QDate QDate::fromJulianDay(int jd)
  1132. Converts the Julian day \a jd to a QDate.
  1133. \sa toJulianDay()
  1134. */
  1135. /*! \fn int QDate::toJulianDay() const
  1136. Converts the date to a Julian day.
  1137. \sa fromJulianDay()
  1138. */
  1139. /*****************************************************************************
  1140. QTime member functions
  1141. *****************************************************************************/
  1142. /*!
  1143. \class QTime
  1144. \reentrant
  1145. \brief The QTime class provides clock time functions.
  1146. A QTime object contains a clock time, i.e. the number of hours,
  1147. minutes, seconds, and milliseconds since midnight. It can read the
  1148. current time from the system clock and measure a span of elapsed
  1149. time. It provides functions for comparing times and for
  1150. manipulating a time by adding a number of milliseconds.
  1151. QTime uses the 24-hour clock format; it has no concept of AM/PM.
  1152. Unlike QDateTime, QTime knows nothing about time zones or
  1153. daylight savings time (DST).
  1154. A QTime object is typically created either by giving the number
  1155. of hours, minutes, seconds, and milliseconds explicitly, or by
  1156. using the static function currentTime(), which creates a QTime
  1157. object that contains the system's local time. Note that the
  1158. accuracy depends on the accuracy of the underlying operating
  1159. system; not all systems provide 1-millisecond accuracy.
  1160. The hour(), minute(), second(), and msec() functions provide
  1161. access to the number of hours, minutes, seconds, and milliseconds
  1162. of the time. The same information is provided in textual format by
  1163. the toString() function.
  1164. QTime provides a full set of operators to compare two QTime
  1165. objects. One time is considered smaller than another if it is
  1166. earlier than the other.
  1167. The time a given number of seconds or milliseconds later than a
  1168. given time can be found using the addSecs() or addMSecs()
  1169. functions. Correspondingly, the number of seconds or milliseconds
  1170. between two times can be found using secsTo() or msecsTo().
  1171. QTime can be used to measure a span of elapsed time using the
  1172. start(), restart(), and elapsed() functions.
  1173. \sa QDate, QDateTime
  1174. */
  1175. /*!
  1176. \fn QTime::QTime()
  1177. Constructs a null time object. A null time can be a QTime(0, 0, 0, 0)
  1178. (i.e., midnight) object, except that isNull() returns true and isValid()
  1179. returns false.
  1180. \sa isNull(), isValid()
  1181. */
  1182. /*!
  1183. Constructs a time with hour \a h, minute \a m, seconds \a s and
  1184. milliseconds \a ms.
  1185. \a h must be in the range 0 to 23, \a m and \a s must be in the
  1186. range 0 to 59, and \a ms must be in the range 0 to 999.
  1187. \sa isValid()
  1188. */
  1189. QTime::QTime(int h, int m, int s, int ms)
  1190. {
  1191. setHMS(h, m, s, ms);
  1192. }
  1193. /*!
  1194. \fn bool QTime::isNull() const
  1195. Returns true if the time is null (i.e., the QTime object was
  1196. constructed using the default constructor); otherwise returns
  1197. false. A null time is also an invalid time.
  1198. \sa isValid()
  1199. */
  1200. /*!
  1201. Returns true if the time is valid; otherwise returns false. For example,
  1202. the time 23:30:55.746 is valid, but 24:12:30 is invalid.
  1203. \sa isNull()
  1204. */
  1205. bool QTime::isValid() const
  1206. {
  1207. return mds > NullTime && mds < MSECS_PER_DAY;
  1208. }
  1209. /*!
  1210. Returns the hour part (0 to 23) of the time.
  1211. \sa minute(), second(), msec()
  1212. */
  1213. int QTime::hour() const
  1214. {
  1215. return ds() / MSECS_PER_HOUR;
  1216. }
  1217. /*!
  1218. Returns the minute part (0 to 59) of the time.
  1219. \sa hour(), second(), msec()
  1220. */
  1221. int QTime::minute() const
  1222. {
  1223. return (ds() % MSECS_PER_HOUR) / MSECS_PER_MIN;
  1224. }
  1225. /*!
  1226. Returns the second part (0 to 59) of the time.
  1227. \sa hour(), minute(), msec()
  1228. */
  1229. int QTime::second() const
  1230. {
  1231. return (ds() / 1000)%SECS_PER_MIN;
  1232. }
  1233. /*!
  1234. Returns the millisecond part (0 to 999) of the time.
  1235. \sa hour(), minute(), second()
  1236. */
  1237. int QTime::msec() const
  1238. {
  1239. return ds() % 1000;
  1240. }
  1241. #ifndef QT_NO_DATESTRING
  1242. /*!
  1243. \overload
  1244. Returns the time as a string. Milliseconds are not included. The
  1245. \a format parameter determines the format of the string.
  1246. If \a format is Qt::TextDate, the string format is HH:MM:SS; e.g. 1
  1247. second before midnight would be "23:59:59".
  1248. If \a format is Qt::ISODate, the string format corresponds to the
  1249. ISO 8601 extended specification for representations of dates,
  1250. which is also HH:mm:ss. (However, contrary to ISO 8601, dates
  1251. before 15 October 1582 are handled as Julian dates, not Gregorian
  1252. dates. See \l{QDate G and J} {Use of Gregorian and Julian
  1253. Calendars}. This might change in a future version of Qt.)
  1254. If the \a format is Qt::SystemLocaleShortDate or
  1255. Qt::SystemLocaleLongDate, the string format depends on the locale
  1256. settings of the system. Identical to calling
  1257. QLocale::system().toString(time, QLocale::ShortFormat) or
  1258. QLocale::system().toString(time, QLocale::LongFormat).
  1259. If the \a format is Qt::DefaultLocaleShortDate or
  1260. Qt::DefaultLocaleLongDate, the string format depends on the
  1261. default application locale. This is the locale set with
  1262. QLocale::setDefault(), or the system locale if no default locale
  1263. has been set. Identical to calling QLocale().toString(time,
  1264. QLocale::ShortFormat) or QLocale().toString(time,
  1265. QLocale::LongFormat).
  1266. If the time is invalid, an empty string will be returned.
  1267. */
  1268. QString QTime::toString(Qt::DateFormat format) const
  1269. {
  1270. if (!isValid())
  1271. return QString();
  1272. switch (format) {
  1273. case Qt::SystemLocaleDate:
  1274. case Qt::SystemLocaleShortDate:
  1275. case Qt::SystemLocaleLongDate:
  1276. return QLocale::system().toString(*this, format == Qt::SystemLocaleLongDate ? QLocale::LongFormat
  1277. : QLocale::ShortFormat);
  1278. case Qt::LocaleDate:
  1279. case Qt::DefaultLocaleShortDate:
  1280. case Qt::DefaultLocaleLongDate:
  1281. return QLocale().toString(*this, format == Qt::DefaultLocaleLongDate ? QLocale::LongFormat
  1282. : QLocale::ShortFormat);
  1283. default:
  1284. case Qt::ISODate:
  1285. case Qt::TextDate:
  1286. return QString::fromLatin1("%1:%2:%3")
  1287. .arg(hour(), 2, 10, QLatin1Char('0'))
  1288. .arg(minute(), 2, 10, QLatin1Char('0'))
  1289. .arg(second(), 2, 10, QLatin1Char('0'));
  1290. }
  1291. }
  1292. /*!
  1293. Returns the time as a string. The \a format parameter determines
  1294. the format of the result string.
  1295. These expressions may be used:
  1296. \table
  1297. \header \i Expression \i Output
  1298. \row \i h
  1299. \i the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
  1300. \row \i hh
  1301. \i the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
  1302. \row \i H
  1303. \i the hour without a leading zero (0 to 23, even with AM/PM display)
  1304. \row \i HH
  1305. \i the hour with a leading zero (00 to 23, even with AM/PM display)
  1306. \row \i m \i the minute without a leading zero (0 to 59)
  1307. \row \i mm \i the minute with a leading zero (00 to 59)
  1308. \row \i s \i the second without a leading zero (0 to 59)
  1309. \row \i ss \i the second with a leading zero (00 to 59)
  1310. \row \i z \i the milliseconds without leading zeroes (0 to 999)
  1311. \row \i zzz \i the milliseconds with leading zeroes (000 to 999)
  1312. \row \i AP or A
  1313. \i use AM/PM display. \e AP will be replaced by either "AM" or "PM".
  1314. \row \i ap or a
  1315. \i use am/pm display. \e ap will be replaced by either "am" or "pm".
  1316. \row \i t \i the timezone (for example "CEST")
  1317. \endtable
  1318. All other input characters will be ignored. Any sequence of characters that
  1319. are enclosed in singlequotes will be treated as text and not be used as an
  1320. expression. Two consecutive singlequotes ("''") are replaced by a singlequote
  1321. in the output.
  1322. Example format strings (assuming that the QTime is 14:13:09.042)
  1323. \table
  1324. \header \i Format \i Result
  1325. \row \i hh:mm:ss.zzz \i 14:13:09.042
  1326. \row \i h:m:s ap \i 2:13:9 pm
  1327. \row \i H:m:s a \i 14:13:9 pm
  1328. \endtable
  1329. If the datetime is invalid, an empty string will be returned.
  1330. If \a format is empty, the default format "hh:mm:ss" is used.
  1331. \sa QDate::toString() QDateTime::toString()
  1332. */
  1333. QString QTime::toString(const QString& format) const
  1334. {
  1335. return fmtDateTime(format, this, 0);
  1336. }
  1337. #endif //QT_NO_DATESTRING
  1338. /*!
  1339. Sets the time to hour \a h, minute \a m, seconds \a s and
  1340. milliseconds \a ms.
  1341. \a h must be in the range 0 to 23, \a m and \a s must be in the
  1342. range 0 to 59, and \a ms must be in the range 0 to 999.
  1343. Returns true if the set time is valid; otherwise returns false.
  1344. \sa isValid()
  1345. */
  1346. bool QTime::setHMS(int h, int m, int s, int ms)
  1347. {
  1348. #if defined(Q_OS_WINCE)
  1349. startTick = NullTime;
  1350. #endif
  1351. if (!isValid(h,m,s,ms)) {
  1352. mds = NullTime; // make this invalid
  1353. return false;
  1354. }
  1355. mds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms;
  1356. return true;
  1357. }
  1358. /*!
  1359. Returns a QTime object containing a time \a s seconds later
  1360. than the time of this object (or earlier if \a s is negative).
  1361. Note that the time will wrap if it passes midnight.
  1362. Example:
  1363. \snippet doc/src/snippets/code/src_corelib_tools_qdatetime.cpp 5
  1364. \sa addMSecs(), secsTo(), QDateTime::addSecs()
  1365. */
  1366. QTime QTime::addSecs(int s) const
  1367. {
  1368. return addMSecs(s * 1000);
  1369. }
  1370. /*!
  1371. Returns the number of seconds from this time to \a t.
  1372. If \a t is earlier than this time, the number of seconds returned
  1373. is negative.
  1374. Because QTime measures time within a day and there are 86400
  1375. seconds in a day, the result is always between -86400 and 86400.
  1376. secsTo() does not take into account any milliseconds.
  1377. \sa addSecs(), QDateTime::secsTo()
  1378. */
  1379. int QTime::secsTo(const QTime &t) const
  1380. {
  1381. return (t.ds() - ds()) / 1000;
  1382. }
  1383. /*!
  1384. Returns a QTime object containing a time \a ms milliseconds later
  1385. than the time of this object (or earlier if \a ms is negative).
  1386. Note that the time will wrap if it passes midnight. See addSecs()
  1387. for an example.
  1388. \sa addSecs(), msecsTo(), QDateTime::addMSecs()
  1389. */
  1390. QTime QTime::addMSecs(int ms) const
  1391. {
  1392. QTime t;
  1393. if (ms < 0) {
  1394. // % not well-defined for -ve, but / is.
  1395. int negdays = (MSECS_PER_DAY - ms) / MSECS_PER_DAY;
  1396. t.mds = (ds() + ms + negdays * MSECS_PER_DAY) % MSECS_PER_DAY;
  1397. } else {
  1398. t.mds = (ds() + ms) % MSECS_PER_DAY;
  1399. }
  1400. #if defined(Q_OS_WINCE)
  1401. if (startTick > NullTime)
  1402. t.startTick = (startTick + ms) % MSECS_PER_DAY;
  1403. #endif
  1404. return t;
  1405. }
  1406. /*!
  1407. Returns the number of milliseconds from this…

Large files files are truncated, but you can click here to view the full file