Qt 4.8
Macros | Enumerations | Functions | Variables
qdatetime.cpp File Reference
#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"
#include <qt_windows.h>
#include <time.h>
#include "qfunctions_wince.h"
#include <private/qcore_mac_p.h>

Go to the source code of this file.

Macros

#define QDTPDEBUG   if (false) qDebug()
 
#define QDTPDEBUGN   if (false) qDebug
 

Enumerations

enum  {
  FIRST_YEAR = -4713, FIRST_MONTH = 1, FIRST_DAY = 2, SECS_PER_DAY = 86400,
  MSECS_PER_DAY = 86400000, SECS_PER_HOUR = 3600, MSECS_PER_HOUR = 3600000, SECS_PER_MIN = 60,
  MSECS_PER_MIN = 60000, JULIAN_DAY_FOR_EPOCH = 2440588
}
 

Functions

static QDate adjustDate (QDate date)
 
static void appendSeparator (QStringList *list, const QString &string, int from, int size, int lastQuote)
 
static int countRepeat (const QString &str, int index, int maxCount)
 Parses the format newFormat. More...
 
static QDate fixedDate (int y, int m, int d)
 
static QString fmtDateTime (const QString &f, const QTime *dt=0, const QDate *dd=0)
 
static int fromShortMonthName (const QString &monthName)
 
static void getDateFromJulianDay (uint julianDay, int *year, int *month, int *day)
 
static QString getFmtString (const QString &f, const QTime *dt=0, const QDate *dd=0, bool am_pm=false)
 
static bool hasUnquotedAP (const QString &f)
 
static uint julianDayFromDate (int year, int month, int day)
 
static uint julianDayFromGregorianDate (int year, int month, int day)
 
static void localToUtc (QDate &date, QTime &time, int isdst)
 
static uint msecsFromDecomposed (int hour, int minute, int sec, int msec=0)
 
QDebug operator<< (QDebug dbg, const QDate &date)
 
QDebug operator<< (QDebug dbg, const QTime &time)
 
QDebug operator<< (QDebug dbg, const QDateTime &date)
 
bool operator== (const QDateTimeParser::SectionNode &s1, const QDateTimeParser::SectionNode &s2)
 
qint64 toMSecsSinceEpoch_helper (qint64 jd, int msecs)
 
static QString unquote (const QString &str)
 
static QDateTimePrivate::Spec utcToLocal (QDate &date, QTime &time)
 

Variables

static const int LowerYear = 1980
 
static const char monthDays [] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
 
static const char *const qt_shortMonthNames []
 
static const int UpperYear = 2037
 

Macro Definition Documentation

◆ QDTPDEBUG

#define QDTPDEBUG   if (false) qDebug()

◆ QDTPDEBUGN

#define QDTPDEBUGN   if (false) qDebug

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
FIRST_YEAR 
FIRST_MONTH 
FIRST_DAY 
SECS_PER_DAY 
MSECS_PER_DAY 
SECS_PER_HOUR 
MSECS_PER_HOUR 
SECS_PER_MIN 
MSECS_PER_MIN 
JULIAN_DAY_FOR_EPOCH 

Definition at line 83 of file qdatetime.cpp.

83  {
84  FIRST_YEAR = -4713,
85  FIRST_MONTH = 1,
86  FIRST_DAY = 2, // ### Qt 5: make FIRST_DAY = 1, by support jd == 0 as valid
87  SECS_PER_DAY = 86400,
88  MSECS_PER_DAY = 86400000,
89  SECS_PER_HOUR = 3600,
90  MSECS_PER_HOUR = 3600000,
91  SECS_PER_MIN = 60,
92  MSECS_PER_MIN = 60000,
93  JULIAN_DAY_FOR_EPOCH = 2440588 // result of julianDayFromGregorianDate(1970, 1, 1)
94 };

Function Documentation

◆ adjustDate()

static QDate adjustDate ( QDate  date)
static

Definition at line 4205 of file qdatetime.cpp.

Referenced by localToUtc(), and utcToLocal().

4206 {
4207  QDate lowerLimit(LowerYear, 1, 2);
4208  QDate upperLimit(UpperYear, 12, 30);
4209 
4210  if (date > lowerLimit && date < upperLimit)
4211  return date;
4212 
4213  int month = date.month();
4214  int day = date.day();
4215 
4216  // neither 1970 nor 2037 are leap years, so make sure date isn't Feb 29
4217  if (month == 2 && day == 29)
4218  --day;
4219 
4220  if (date < lowerLimit)
4221  date.setDate(LowerYear, month, day);
4222  else
4223  date.setDate(UpperYear, month, day);
4224 
4225  return date;
4226 }
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
The QDate class provides date functions.
Definition: qdatetime.h:55
bool setDate(int year, int month, int day)
Sets the date&#39;s year, month, and day.
Definition: qdatetime.cpp:959
static const int UpperYear
Definition: qdatetime.cpp:4203
static const int LowerYear
Definition: qdatetime.cpp:4199

◆ appendSeparator()

static void appendSeparator ( QStringList list,
const QString string,
int  from,
int  size,
int  lastQuote 
)
inlinestatic

Definition at line 4760 of file qdatetime.cpp.

Referenced by QDateTimeParser::parseFormat().

4761 {
4762  QString str(string.mid(from, size));
4763  if (lastQuote >= from)
4764  str = unquote(str);
4765  list->append(str);
4766 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
static QString unquote(const QString &str)
Definition: qdatetime.cpp:4715

◆ countRepeat()

static int countRepeat ( const QString str,
int  index,
int  maxCount 
)
inlinestatic

Parses the format newFormat.

Warning
This function is not part of the public interface.

If successful, returns true and sets up the format. Else keeps the old format and returns false.

Definition at line 4749 of file qdatetime.cpp.

Referenced by QDateTimeParser::parseFormat().

4750 {
4751  int count = 1;
4752  const QChar ch(str.at(index));
4753  const int max = qMin(index + maxCount, str.size());
4754  while (index + count < max && str.at(index + count) == ch) {
4755  ++count;
4756  }
4757  return count;
4758 }
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
quint16 index

◆ fixedDate()

static QDate fixedDate ( int  y,
int  m,
int  d 
)
inlinestatic

Definition at line 96 of file qdatetime.cpp.

Referenced by QDate::addMonths(), and QDate::addYears().

97 {
98  QDate result(y, m, 1);
99  result.setDate(y, m, qMin(d, result.daysInMonth()));
100  return result;
101 }
double d
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
The QDate class provides date functions.
Definition: qdatetime.h:55

◆ fmtDateTime()

static QString fmtDateTime ( const QString f,
const QTime dt = 0,
const QDate dd = 0 
)
static

Definition at line 4133 of file qdatetime.cpp.

Referenced by QDate::toString(), QTime::toString(), and QDateTime::toString().

4134 {
4135  const QLatin1Char quote('\'');
4136  if (f.isEmpty())
4137  return QString();
4138  if (dt && !dt->isValid())
4139  return QString();
4140  if (dd && !dd->isValid())
4141  return QString();
4142 
4143  const bool ap = hasUnquotedAP(f);
4144 
4145  QString buf;
4146  QString frm;
4147  QChar status(QLatin1Char('0'));
4148 
4149  for (int i = 0; i < (int)f.length(); ++i) {
4150  if (f.at(i) == quote) {
4151  if (status == quote) {
4152  if (i > 0 && f.at(i - 1) == quote)
4153  buf += QLatin1Char('\'');
4154  status = QLatin1Char('0');
4155  } else {
4156  if (!frm.isEmpty()) {
4157  buf += getFmtString(frm, dt, dd, ap);
4158  frm.clear();
4159  }
4160  status = quote;
4161  }
4162  } else if (status == quote) {
4163  buf += f.at(i);
4164  } else if (f.at(i) == status) {
4165  if ((ap) && ((f.at(i) == QLatin1Char('P')) || (f.at(i) == QLatin1Char('p'))))
4166  status = QLatin1Char('0');
4167  frm += f.at(i);
4168  } else {
4169  buf += getFmtString(frm, dt, dd, ap);
4170  frm.clear();
4171  if ((f.at(i) == QLatin1Char('h')) || (f.at(i) == QLatin1Char('m'))
4172  || (f.at(i) == QLatin1Char('H'))
4173  || (f.at(i) == QLatin1Char('s')) || (f.at(i) == QLatin1Char('z'))) {
4174  status = f.at(i);
4175  frm += f.at(i);
4176  } else if ((f.at(i) == QLatin1Char('d')) || (f.at(i) == QLatin1Char('M')) || (f.at(i) == QLatin1Char('y'))) {
4177  status = f.at(i);
4178  frm += f.at(i);
4179  } else if ((ap) && (f.at(i) == QLatin1Char('A'))) {
4180  status = QLatin1Char('P');
4181  frm += f.at(i);
4182  } else if((ap) && (f.at(i) == QLatin1Char('a'))) {
4183  status = QLatin1Char('p');
4184  frm += f.at(i);
4185  } else {
4186  buf += f.at(i);
4187  status = QLatin1Char('0');
4188  }
4189  }
4190  }
4191 
4192  buf += getFmtString(frm, dt, dd, ap);
4193 
4194  return buf;
4195 }
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static QString getFmtString(const QString &f, const QTime *dt=0, const QDate *dd=0, bool am_pm=false)
Definition: qdatetime.cpp:4026
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static bool hasUnquotedAP(const QString &f)
Definition: qdatetime.cpp:4005
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
bool isValid() const
Returns true if the time is valid; otherwise returns false.
Definition: qdatetime.cpp:1566

◆ fromShortMonthName()

static int fromShortMonthName ( const QString monthName)
static

Definition at line 3459 of file qdatetime.cpp.

Referenced by QDateTime::fromString().

3460 {
3461  // Assume that English monthnames are the default
3462  for (int i = 0; i < 12; ++i) {
3463  if (monthName == QLatin1String(qt_shortMonthNames[i]))
3464  return i + 1;
3465  }
3466  // If English names can't be found, search the localized ones
3467  for (int i = 1; i <= 12; ++i) {
3468  if (monthName == QDate::shortMonthName(i))
3469  return i;
3470  }
3471  return -1;
3472 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static const char *const qt_shortMonthNames[]
Definition: qdatetime.cpp:176
static QString shortMonthName(int month)
Returns the short version of the name of the month.
Definition: qdatetime.cpp:591

◆ getDateFromJulianDay()

static void getDateFromJulianDay ( uint  julianDay,
int *  year,
int *  month,
int *  day 
)
static

Definition at line 133 of file qdatetime.cpp.

Referenced by QDate::addMonths(), QDate::addYears(), QDate::day(), QDate::daysInMonth(), QDate::daysInYear(), QDate::getDate(), QDate::julianToGregorian(), QDate::month(), QDateTimeParser::parse(), QDate::toString(), and QDate::year().

134 {
135  int y, m, d;
136 
137  if (julianDay >= 2299161) {
138  // Gregorian calendar starting from October 15, 1582
139  // This algorithm is from Henry F. Fliegel and Thomas C. Van Flandern
140  qulonglong ell, n, i, j;
141  ell = qulonglong(julianDay) + 68569;
142  n = (4 * ell) / 146097;
143  ell = ell - (146097 * n + 3) / 4;
144  i = (4000 * (ell + 1)) / 1461001;
145  ell = ell - (1461 * i) / 4 + 31;
146  j = (80 * ell) / 2447;
147  d = ell - (2447 * j) / 80;
148  ell = j / 11;
149  m = j + 2 - (12 * ell);
150  y = 100 * (n - 49) + i + ell;
151  } else {
152  // Julian calendar until October 4, 1582
153  // Algorithm from Frequently Asked Questions about Calendars by Claus Toendering
154  julianDay += 32082;
155  int dd = (4 * julianDay + 3) / 1461;
156  int ee = julianDay - (1461 * dd) / 4;
157  int mm = ((5 * ee) + 2) / 153;
158  d = ee - (153 * mm + 2) / 5 + 1;
159  m = mm + 3 - 12 * (mm / 10);
160  y = dd - 4800 + (mm / 10);
161  if (y <= 0)
162  --y;
163  }
164  if (year)
165  *year = y;
166  if (month)
167  *month = m;
168  if (day)
169  *day = d;
170 }
double d
Definition: qnumeric_p.h:62
quint64 qulonglong
Definition: qglobal.h:952

◆ getFmtString()

static QString getFmtString ( const QString f,
const QTime dt = 0,
const QDate dd = 0,
bool  am_pm = false 
)
static

Definition at line 4026 of file qdatetime.cpp.

Referenced by fmtDateTime().

4027 {
4028  if (f.isEmpty())
4029  return QString();
4030 
4031  QString buf = f;
4032  int removed = 0;
4033 
4034  if (dt) {
4035  if (f.startsWith(QLatin1String("hh")) || f.startsWith(QLatin1String("HH"))) {
4036  const bool hour12 = f.at(0) == QLatin1Char('h') && am_pm;
4037  if (hour12 && dt->hour() > 12)
4038  buf = QString::number(dt->hour() - 12).rightJustified(2, QLatin1Char('0'), true);
4039  else if (hour12 && dt->hour() == 0)
4040  buf = QLatin1String("12");
4041  else
4042  buf = QString::number(dt->hour()).rightJustified(2, QLatin1Char('0'), true);
4043  removed = 2;
4044  } else if (f.at(0) == QLatin1Char('h') || f.at(0) == QLatin1Char('H')) {
4045  const bool hour12 = f.at(0) == QLatin1Char('h') && am_pm;
4046  if (hour12 && dt->hour() > 12)
4047  buf = QString::number(dt->hour() - 12);
4048  else if (hour12 && dt->hour() == 0)
4049  buf = QLatin1String("12");
4050  else
4051  buf = QString::number(dt->hour());
4052  removed = 1;
4053  } else if (f.startsWith(QLatin1String("mm"))) {
4054  buf = QString::number(dt->minute()).rightJustified(2, QLatin1Char('0'), true);
4055  removed = 2;
4056  } else if (f.at(0) == (QLatin1Char('m'))) {
4057  buf = QString::number(dt->minute());
4058  removed = 1;
4059  } else if (f.startsWith(QLatin1String("ss"))) {
4060  buf = QString::number(dt->second()).rightJustified(2, QLatin1Char('0'), true);
4061  removed = 2;
4062  } else if (f.at(0) == QLatin1Char('s')) {
4063  buf = QString::number(dt->second());
4064  } else if (f.startsWith(QLatin1String("zzz"))) {
4065  buf = QString::number(dt->msec()).rightJustified(3, QLatin1Char('0'), true);
4066  removed = 3;
4067  } else if (f.at(0) == QLatin1Char('z')) {
4068  buf = QString::number(dt->msec());
4069  removed = 1;
4070  } else if (f.at(0).toUpper() == QLatin1Char('A')) {
4071  const bool upper = f.at(0) == QLatin1Char('A');
4072  buf = dt->hour() < 12 ? QLatin1String("am") : QLatin1String("pm");
4073  if (upper)
4074  buf = buf.toUpper();
4075  if (f.size() > 1 && f.at(1).toUpper() == QLatin1Char('P') &&
4076  f.at(0).isUpper() == f.at(1).isUpper()) {
4077  removed = 2;
4078  } else {
4079  removed = 1;
4080  }
4081  }
4082  }
4083 
4084  if (dd) {
4085  if (f.startsWith(QLatin1String("dddd"))) {
4086  buf = dd->longDayName(dd->dayOfWeek());
4087  removed = 4;
4088  } else if (f.startsWith(QLatin1String("ddd"))) {
4089  buf = dd->shortDayName(dd->dayOfWeek());
4090  removed = 3;
4091  } else if (f.startsWith(QLatin1String("dd"))) {
4092  buf = QString::number(dd->day()).rightJustified(2, QLatin1Char('0'), true);
4093  removed = 2;
4094  } else if (f.at(0) == QLatin1Char('d')) {
4095  buf = QString::number(dd->day());
4096  removed = 1;
4097  } else if (f.startsWith(QLatin1String("MMMM"))) {
4098  buf = dd->longMonthName(dd->month());
4099  removed = 4;
4100  } else if (f.startsWith(QLatin1String("MMM"))) {
4101  buf = dd->shortMonthName(dd->month());
4102  removed = 3;
4103  } else if (f.startsWith(QLatin1String("MM"))) {
4104  buf = QString::number(dd->month()).rightJustified(2, QLatin1Char('0'), true);
4105  removed = 2;
4106  } else if (f.at(0) == QLatin1Char('M')) {
4107  buf = QString::number(dd->month());
4108  removed = 1;
4109  } else if (f.startsWith(QLatin1String("yyyy"))) {
4110  const int year = dd->year();
4111  buf = QString::number(qAbs(year)).rightJustified(4, QLatin1Char('0'));
4112  if(year > 0)
4113  removed = 4;
4114  else
4115  {
4116  buf.prepend(QLatin1Char('-'));
4117  removed = 5;
4118  }
4119 
4120  } else if (f.startsWith(QLatin1String("yy"))) {
4121  buf = QString::number(dd->year()).right(2).rightJustified(2, QLatin1Char('0'));
4122  removed = 2;
4123  }
4124  }
4125  if (removed == 0 || removed >= f.size()) {
4126  return buf;
4127  }
4128 
4129  return buf + getFmtString(f.mid(removed), dt, dd, am_pm);
4130 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
int dayOfWeek() const
Returns the weekday (1 = Monday to 7 = Sunday) for this date.
Definition: qdatetime.cpp:408
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
QString & prepend(QChar c)
Definition: qstring.h:261
QString toUpper() const Q_REQUIRED_RESULT
Returns an uppercase copy of the string.
Definition: qstring.cpp:5483
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int msec() const
Returns the millisecond part (0 to 999) of the time.
Definition: qdatetime.cpp:1611
static QString shortDayName(int weekday)
Returns the short version of the name of the weekday.
Definition: qdatetime.cpp:709
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
static QString longMonthName(int month)
Returns the long version of the name of the month.
Definition: qdatetime.cpp:651
static QString getFmtString(const QString &f, const QTime *dt=0, const QDate *dd=0, bool am_pm=false)
Definition: qdatetime.cpp:4026
QString rightJustified(int width, QChar fill=QLatin1Char(' '), bool trunc=false) const Q_REQUIRED_RESULT
Returns a string of size() width that contains the fill character followed by the string...
Definition: qstring.cpp:5357
QTextStream & right(QTextStream &stream)
Calls QTextStream::setFieldAlignment(QTextStream::AlignRight) on stream and returns stream...
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
int second() const
Returns the second part (0 to 59) of the time.
Definition: qdatetime.cpp:1600
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QChar toUpper() const
Returns the uppercase equivalent if the character is lowercase or titlecase; otherwise returns the ch...
Definition: qchar.cpp:1287
int minute() const
Returns the minute part (0 to 59) of the time.
Definition: qdatetime.cpp:1589
bool isUpper() const
Returns true if the character is an uppercase letter, i.
Definition: qchar.h:273
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
static QString shortMonthName(int month)
Returns the short version of the name of the month.
Definition: qdatetime.cpp:591
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
static QString longDayName(int weekday)
Returns the long version of the name of the weekday.
Definition: qdatetime.cpp:767
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
int hour() const
Returns the hour part (0 to 23) of the time.
Definition: qdatetime.cpp:1578

◆ hasUnquotedAP()

static bool hasUnquotedAP ( const QString f)
static

Definition at line 4005 of file qdatetime.cpp.

Referenced by fmtDateTime().

4006 {
4007  const QLatin1Char quote('\'');
4008  bool inquote = false;
4009  const int max = f.size();
4010  for (int i=0; i<max; ++i) {
4011  if (f.at(i) == quote) {
4012  inquote = !inquote;
4013  } else if (!inquote && f.at(i).toUpper() == QLatin1Char('A')) {
4014  return true;
4015  }
4016  }
4017  return false;
4018 }
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
QChar toUpper() const
Returns the uppercase equivalent if the character is lowercase or titlecase; otherwise returns the ch...
Definition: qchar.cpp:1287
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ julianDayFromDate()

static uint julianDayFromDate ( int  year,
int  month,
int  day 
)
static

Definition at line 113 of file qdatetime.cpp.

Referenced by QDate::currentDate(), QDateTime::currentDateTime(), QDateTime::currentDateTimeUtc(), QDateTime::currentMSecsSinceEpoch(), QDate::dayOfYear(), QDate::gregorianToJulian(), and QDate::setDate().

114 {
115  if (year < 0)
116  ++year;
117 
118  if (year > 1582 || (year == 1582 && (month > 10 || (month == 10 && day >= 15)))) {
119  return julianDayFromGregorianDate(year, month, day);
120  } else if (year < 1582 || (year == 1582 && (month < 10 || (month == 10 && day <= 4)))) {
121  // Julian calendar until October 4, 1582
122  // Algorithm from Frequently Asked Questions about Calendars by Claus Toendering
123  int a = (14 - month) / 12;
124  return (153 * (month + (12 * a) - 3) + 2) / 5
125  + (1461 * (year + 4800 - a)) / 4
126  + day - 32083;
127  } else {
128  // the day following October 4, 1582 is October 15, 1582
129  return 0;
130  }
131 }
long ASN1_INTEGER_get ASN1_INTEGER * a
static uint julianDayFromGregorianDate(int year, int month, int day)
Definition: qdatetime.cpp:103

◆ julianDayFromGregorianDate()

static uint julianDayFromGregorianDate ( int  year,
int  month,
int  day 
)
inlinestatic

Definition at line 103 of file qdatetime.cpp.

Referenced by QDateTime::currentMSecsSinceEpoch(), and julianDayFromDate().

104 {
105  // Gregorian calendar starting from October 15, 1582
106  // Algorithm from Henry F. Fliegel and Thomas C. Van Flandern
107  return (1461 * (year + 4800 + (month - 14) / 12)) / 4
108  + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12
109  - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4
110  + day - 32075;
111 }

◆ localToUtc()

static void localToUtc ( QDate date,
QTime time,
int  isdst 
)
static

Definition at line 4316 of file qdatetime.cpp.

Referenced by QDateTimePrivate::getUTC().

4317 {
4318  if (!date.isValid())
4319  return;
4320 
4321  QDate fakeDate = adjustDate(date);
4322 
4323  tm localTM;
4324  localTM.tm_sec = time.second();
4325  localTM.tm_min = time.minute();
4326  localTM.tm_hour = time.hour();
4327  localTM.tm_mday = fakeDate.day();
4328  localTM.tm_mon = fakeDate.month() - 1;
4329  localTM.tm_year = fakeDate.year() - 1900;
4330  localTM.tm_isdst = (int)isdst;
4331 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
4332  time_t secsSince1Jan1970UTC = (toMSecsSinceEpoch_helper(fakeDate.toJulianDay(), QTime().msecsTo(time)) / 1000);
4333 #else
4334 #if defined(Q_OS_WIN)
4335  _tzset();
4336 #endif
4337  time_t secsSince1Jan1970UTC = mktime(&localTM);
4338 #ifdef Q_OS_QNX
4339  //mktime sometimes fails on QNX. Following workaround converts the date and time then manually
4340  if (secsSince1Jan1970UTC == (time_t)-1) {
4341  QDateTime tempTime = QDateTime(date, time, Qt::UTC);;
4342  tempTime = tempTime.addMSecs(timezone * 1000);
4343  date = tempTime.date();
4344  time = tempTime.time();
4345  return;
4346  }
4347 #endif
4348 #endif
4349  tm *brokenDown = 0;
4350 #if defined(Q_OS_WINCE)
4351  tm res;
4352  FILETIME localTime = time_tToFt(secsSince1Jan1970UTC);
4353  SYSTEMTIME sysTime;
4354  FileTimeToSystemTime(&localTime, &sysTime);
4355  FILETIME resultTime;
4356  LocalFileTimeToFileTime(&localTime , &resultTime);
4357  FileTimeToSystemTime(&resultTime , &sysTime);
4358  res.tm_sec = sysTime.wSecond;
4359  res.tm_min = sysTime.wMinute;
4360  res.tm_hour = sysTime.wHour;
4361  res.tm_mday = sysTime.wDay;
4362  res.tm_mon = sysTime.wMonth - 1;
4363  res.tm_year = sysTime.wYear - 1900;
4364  res.tm_isdst = (int)isdst;
4365  brokenDown = &res;
4366 #elif defined(Q_OS_SYMBIAN)
4367  // months and days are zero index based
4368  _LIT(KUnixEpoch, "19700000:000000.000000");
4369  TTimeIntervalSeconds tTimeIntervalSecsSince1Jan1970UTC(secsSince1Jan1970UTC);
4370  TTime epochTTime;
4371  TInt err = epochTTime.Set(KUnixEpoch);
4372  tm res;
4373  if(err == KErrNone) {
4374  TTime localTTime = epochTTime + tTimeIntervalSecsSince1Jan1970UTC;
4375  RTz tz;
4376  if (KErrNone == tz.Connect()) {
4377  if (KErrNone == tz.ConvertToUniversalTime(localTTime)) {
4378  TDateTime utcDateTime = localTTime.DateTime();
4379  res.tm_sec = utcDateTime.Second();
4380  res.tm_min = utcDateTime.Minute();
4381  res.tm_hour = utcDateTime.Hour();
4382  res.tm_mday = utcDateTime.Day() + 1; // non-zero based index for tm struct
4383  res.tm_mon = utcDateTime.Month();
4384  res.tm_year = utcDateTime.Year() - 1900;
4385  res.tm_isdst = (int)isdst;
4386  brokenDown = &res;
4387  }
4388  tz.Close();
4389  }
4390  }
4391 #elif !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
4392  // use the reentrant version of gmtime() where available
4393  tm res;
4394  brokenDown = gmtime_r(&secsSince1Jan1970UTC, &res);
4395 #elif defined(_MSC_VER) && _MSC_VER >= 1400
4396  tm res;
4397  if (!_gmtime64_s(&res, &secsSince1Jan1970UTC))
4398  brokenDown = &res;
4399 #else
4400  brokenDown = gmtime(&secsSince1Jan1970UTC);
4401 #endif // !QT_NO_THREAD && _POSIX_THREAD_SAFE_FUNCTIONS
4402  if (!brokenDown) {
4403  date = QDate(1970, 1, 1);
4404  time = QTime();
4405  } else {
4406  int deltaDays = fakeDate.daysTo(date);
4407  date = QDate(brokenDown->tm_year + 1900, brokenDown->tm_mon + 1, brokenDown->tm_mday);
4408  time = QTime(brokenDown->tm_hour, brokenDown->tm_min, brokenDown->tm_sec, time.msec());
4409  date = date.addDays(deltaDays);
4410  }
4411 }
int tm_min
int daysTo(const QDate &) const
Returns the number of days from this date to d (which is negative if d is earlier than this date)...
Definition: qdatetime.cpp:1111
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
int msecsTo(const QTime &) const
Returns the number of milliseconds from this time to t.
Definition: qdatetime.cpp:1832
qint64 toMSecsSinceEpoch_helper(qint64 jd, int msecs)
Definition: qdatetime.cpp:2449
int tm_mday
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
The QDate class provides date functions.
Definition: qdatetime.h:55
int msec() const
Returns the millisecond part (0 to 999) of the time.
Definition: qdatetime.cpp:1611
int tm_year
The QTime class provides clock time functions.
Definition: qdatetime.h:148
unsigned long time_t
int tm_mon
int second() const
Returns the second part (0 to 59) of the time.
Definition: qdatetime.cpp:1600
QDateTime addMSecs(qint64 msecs) const
Returns a QDateTime object containing a datetime msecs miliseconds later than the datetime of this ob...
Definition: qdatetime.cpp:2881
int minute() const
Returns the minute part (0 to 59) of the time.
Definition: qdatetime.cpp:1589
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
int toJulianDay() const
Converts the date to a Julian day.
Definition: qdatetime.h:134
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
static QDate adjustDate(QDate date)
Definition: qdatetime.cpp:4205
int tm_sec
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
QTime time() const
Returns the time part of the datetime.
Definition: qdatetime.cpp:2368
int tm_hour
int tm_isdst
#define time_tToFt(a)
int hour() const
Returns the hour part (0 to 23) of the time.
Definition: qdatetime.cpp:1578

◆ msecsFromDecomposed()

static uint msecsFromDecomposed ( int  hour,
int  minute,
int  sec,
int  msec = 0 
)
inlinestatic

Definition at line 3109 of file qdatetime.cpp.

Referenced by QDateTime::currentDateTime(), QDateTime::currentDateTimeUtc(), QDateTime::currentMSecsSinceEpoch(), and QTime::currentTime().

3110 {
3111  return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + 1000 * sec + msec;
3112 }

◆ operator<<() [1/3]

QDebug operator<< ( QDebug  dbg,
const QDate date 
)

Definition at line 4436 of file qdatetime.cpp.

4437 {
4438  dbg.nospace() << "QDate(" << date.toString() << ')';
4439  return dbg.space();
4440 }
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
QString toString(Qt::DateFormat f=Qt::TextDate) const
Returns the date as a string.
Definition: qdatetime.cpp:823

◆ operator<<() [2/3]

QDebug operator<< ( QDebug  dbg,
const QTime time 
)

Definition at line 4442 of file qdatetime.cpp.

4443 {
4444  dbg.nospace() << "QTime(" << time.toString() << ')';
4445  return dbg.space();
4446 }
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
QString toString(Qt::DateFormat f=Qt::TextDate) const
Returns the time as a string.
Definition: qdatetime.cpp:1653
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91

◆ operator<<() [3/3]

QDebug operator<< ( QDebug  dbg,
const QDateTime date 
)

Definition at line 4448 of file qdatetime.cpp.

4449 {
4450  dbg.nospace() << "QDateTime(" << date.toString() << ')';
4451  return dbg.space();
4452 }
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
QString toString(Qt::DateFormat f=Qt::TextDate) const
Returns the datetime as a string in the format given.
Definition: qdatetime.cpp:2628
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91

◆ operator==()

bool operator== ( const QDateTimeParser::SectionNode s1,
const QDateTimeParser::SectionNode s2 
)

Definition at line 6137 of file qdatetime.cpp.

6138 {
6139  return (s1.type == s2.type) && (s1.pos == s2.pos) && (s1.count == s2.count);
6140 }

◆ toMSecsSinceEpoch_helper()

qint64 toMSecsSinceEpoch_helper ( qint64  jd,
int  msecs 
)

Definition at line 2449 of file qdatetime.cpp.

Referenced by localToUtc(), QDateTime::toMSecsSinceEpoch(), and utcToLocal().

2450 {
2451  qint64 days = jd - JULIAN_DAY_FOR_EPOCH;
2452  qint64 retval = (days * MSECS_PER_DAY) + msecs;
2453  return retval;
2454 }
__int64 qint64
Definition: qglobal.h:942

◆ unquote()

static QString unquote ( const QString str)
static
Warning
This function is not part of the public interface. helper function for parseFormat. removes quotes that are not escaped and removes the escaping on those that are escaped

Definition at line 4715 of file qdatetime.cpp.

Referenced by appendSeparator(), and QDateTimeParser::parseFormat().

4716 {
4717  const QChar quote(QLatin1Char('\''));
4718  const QChar slash(QLatin1Char('\\'));
4719  const QChar zero(QLatin1Char('0'));
4720  QString ret;
4721  QChar status(zero);
4722  const int max = str.size();
4723  for (int i=0; i<max; ++i) {
4724  if (str.at(i) == quote) {
4725  if (status != quote) {
4726  status = quote;
4727  } else if (!ret.isEmpty() && str.at(i - 1) == slash) {
4728  ret[ret.size() - 1] = quote;
4729  } else {
4730  status = zero;
4731  }
4732  } else {
4733  ret += str.at(i);
4734  }
4735  }
4736  return ret;
4737 }
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static const int zero
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ utcToLocal()

static QDateTimePrivate::Spec utcToLocal ( QDate date,
QTime time 
)
static

Definition at line 4228 of file qdatetime.cpp.

Referenced by QDateTimePrivate::getLocal().

4229 {
4230  QDate fakeDate = adjustDate(date);
4231 
4232  // won't overflow because of fakeDate
4233  time_t secsSince1Jan1970UTC = toMSecsSinceEpoch_helper(fakeDate.toJulianDay(), QTime().msecsTo(time)) / 1000;
4234  tm *brokenDown = 0;
4235 
4236 #if defined(Q_OS_WINCE)
4237  tm res;
4238  FILETIME utcTime = time_tToFt(secsSince1Jan1970UTC);
4239  FILETIME resultTime;
4240  FileTimeToLocalFileTime(&utcTime , &resultTime);
4241  SYSTEMTIME sysTime;
4242  FileTimeToSystemTime(&resultTime , &sysTime);
4243 
4244  res.tm_sec = sysTime.wSecond;
4245  res.tm_min = sysTime.wMinute;
4246  res.tm_hour = sysTime.wHour;
4247  res.tm_mday = sysTime.wDay;
4248  res.tm_mon = sysTime.wMonth - 1;
4249  res.tm_year = sysTime.wYear - 1900;
4250  brokenDown = &res;
4251 #elif defined(Q_OS_SYMBIAN)
4252  // months and days are zero index based
4253  _LIT(KUnixEpoch, "19700000:000000.000000");
4254  TTimeIntervalSeconds tTimeIntervalSecsSince1Jan1970UTC(secsSince1Jan1970UTC);
4255  TTime epochTTime;
4256  TInt err = epochTTime.Set(KUnixEpoch);
4257  tm res;
4258  if(err == KErrNone) {
4259  TTime utcTTime = epochTTime + tTimeIntervalSecsSince1Jan1970UTC;
4260  CTrapCleanup *cleanup = CTrapCleanup::New(); // needed to avoid crashes in apps that previously were able to use this function in static data initialization
4261  TRAP(err,
4262  RTz tz;
4263  User::LeaveIfError(tz.Connect());
4264  CleanupClosePushL(tz);
4265  CTzId *tzId = tz.GetTimeZoneIdL();
4266  CleanupStack::PushL(tzId);
4267  res.tm_isdst = tz.IsDaylightSavingOnL(*tzId,utcTTime);
4268  User::LeaveIfError(tz.ConvertToLocalTime(utcTTime));
4269  CleanupStack::PopAndDestroy(tzId);
4270  CleanupStack::PopAndDestroy(&tz));
4271  delete cleanup;
4272  if (KErrNone == err) {
4273  TDateTime localDateTime = utcTTime.DateTime();
4274  res.tm_sec = localDateTime.Second();
4275  res.tm_min = localDateTime.Minute();
4276  res.tm_hour = localDateTime.Hour();
4277  res.tm_mday = localDateTime.Day() + 1; // non-zero based index for tm struct
4278  res.tm_mon = localDateTime.Month();
4279  res.tm_year = localDateTime.Year() - 1900;
4280  // Symbian's timezone server doesn't know how to handle DST before year 1997
4281  if (res.tm_year < 97)
4282  res.tm_isdst = -1;
4283  brokenDown = &res;
4284  }
4285  }
4286 #elif !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
4287  // use the reentrant version of localtime() where available
4288  tzset();
4289  tm res;
4290  brokenDown = localtime_r(&secsSince1Jan1970UTC, &res);
4291 #elif defined(_MSC_VER) && _MSC_VER >= 1400
4292  tm res;
4293  if (!_localtime64_s(&res, &secsSince1Jan1970UTC))
4294  brokenDown = &res;
4295 #else
4296  brokenDown = localtime(&secsSince1Jan1970UTC);
4297 #endif
4298  if (!brokenDown) {
4299  date = QDate(1970, 1, 1);
4300  time = QTime();
4302  } else {
4303  int deltaDays = fakeDate.daysTo(date);
4304  date = QDate(brokenDown->tm_year + 1900, brokenDown->tm_mon + 1, brokenDown->tm_mday);
4305  time = QTime(brokenDown->tm_hour, brokenDown->tm_min, brokenDown->tm_sec, time.msec());
4306  date = date.addDays(deltaDays);
4307  if (brokenDown->tm_isdst > 0)
4309  else if (brokenDown->tm_isdst < 0)
4311  else
4313  }
4314 }
int tm_min
int daysTo(const QDate &) const
Returns the number of days from this date to d (which is negative if d is earlier than this date)...
Definition: qdatetime.cpp:1111
int msecsTo(const QTime &) const
Returns the number of milliseconds from this time to t.
Definition: qdatetime.cpp:1832
qint64 toMSecsSinceEpoch_helper(qint64 jd, int msecs)
Definition: qdatetime.cpp:2449
int tm_mday
The QDate class provides date functions.
Definition: qdatetime.h:55
int msec() const
Returns the millisecond part (0 to 999) of the time.
Definition: qdatetime.cpp:1611
int tm_year
The QTime class provides clock time functions.
Definition: qdatetime.h:148
unsigned long time_t
int tm_mon
int toJulianDay() const
Converts the date to a Julian day.
Definition: qdatetime.h:134
static QDate adjustDate(QDate date)
Definition: qdatetime.cpp:4205
static void cleanup()
Definition: qpicture.cpp:1508
int tm_sec
int tm_hour
int tm_isdst
#define time_tToFt(a)

Variable Documentation

◆ LowerYear

const int LowerYear = 1980
static

Definition at line 4199 of file qdatetime.cpp.

Referenced by adjustDate().

◆ monthDays

const char monthDays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
static

Definition at line 173 of file qdatetime.cpp.

Referenced by QDate::daysInMonth(), and QDate::isValid().

◆ qt_shortMonthNames

const char* const qt_shortMonthNames[]
static
Initial value:
= {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }

Definition at line 176 of file qdatetime.cpp.

Referenced by fromShortMonthName(), and QDate::fromString().

◆ UpperYear

const int UpperYear = 2037
static

Definition at line 4203 of file qdatetime.cpp.

Referenced by adjustDate().