summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@nokia.com>2010-03-01 15:52:36 (GMT)
committerThiago Macieira <thiago.macieira@nokia.com>2010-03-17 10:54:20 (GMT)
commit73d10fd0813b19ad71e76419d78bf6e2dfd312d7 (patch)
treee15159d68a495c75f3191f4445e870f0e391cabe /src/corelib/tools
parent06ffa232f1920fa5aa857e644df08ef52b62a0b3 (diff)
downloadQt-73d10fd0813b19ad71e76419d78bf6e2dfd312d7.zip
Qt-73d10fd0813b19ad71e76419d78bf6e2dfd312d7.tar.gz
Qt-73d10fd0813b19ad71e76419d78bf6e2dfd312d7.tar.bz2
Add QDateTime::currentDateTimeUtc and QDateTime::currentMsecsSinceEpoch
Diffstat (limited to 'src/corelib/tools')
-rw-r--r--src/corelib/tools/qdatetime.cpp353
-rw-r--r--src/corelib/tools/qdatetime.h3
-rw-r--r--src/corelib/tools/qelapsedtimer_generic.cpp10
-rw-r--r--src/corelib/tools/tools.pri3
4 files changed, 253 insertions, 116 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index c1027ed..7e03777 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -98,18 +98,23 @@ static inline QDate fixedDate(int y, int m, int d)
return result;
}
+static inline uint julianDayFromGregorianDate(int year, int month, int day)
+{
+ // Gregorian calendar starting from October 15, 1582
+ // Algorithm from Henry F. Fliegel and Thomas C. Van Flandern
+ return (1461 * (year + 4800 + (month - 14) / 12)) / 4
+ + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12
+ - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4
+ + day - 32075;
+}
+
static uint julianDayFromDate(int year, int month, int day)
{
if (year < 0)
++year;
if (year > 1582 || (year == 1582 && (month > 10 || (month == 10 && day >= 15)))) {
- // Gregorian calendar starting from October 15, 1582
- // Algorithm from Henry F. Fliegel and Thomas C. Van Flandern
- return (1461 * (year + 4800 + (month - 14) / 12)) / 4
- + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12
- - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4
- + day - 32075;
+ return julianDayFromGregorianDate(year, month, day);
} else if (year < 1582 || (year == 1582 && (month < 10 || (month == 10 && day <= 4)))) {
// Julian calendar until October 4, 1582
// Algorithm from Frequently Asked Questions about Calendars by Claus Toendering
@@ -1118,46 +1123,12 @@ int QDate::daysTo(const QDate &d) const
*/
/*!
- \overload
+ \fn QDate::currentDate()
Returns the current date, as reported by the system clock.
\sa QTime::currentTime(), QDateTime::currentDateTime()
*/
-QDate QDate::currentDate()
-{
- QDate d;
-#if defined(Q_OS_WIN)
- SYSTEMTIME st;
- memset(&st, 0, sizeof(SYSTEMTIME));
- GetLocalTime(&st);
- d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
-#elif defined(Q_OS_SYMBIAN)
- TTime localTime;
- localTime.HomeTime();
- TDateTime localDateTime = localTime.DateTime();
- // months and days are zero indexed
- d.jd = julianDayFromDate(localDateTime.Year(), localDateTime.Month() + 1, localDateTime.Day() + 1 );
-#else
- // posix compliant system
- time_t ltime;
- time(&ltime);
- tm *t = 0;
-
-#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
- // use the reentrant version of localtime() where available
- tzset();
- tm res;
- t = localtime_r(&ltime, &res);
-#else
- t = localtime(&ltime);
-#endif // !QT_NO_THREAD && _POSIX_THREAD_SAFE_FUNCTIONS
-
- d.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
-#endif
- return d;
-}
-
#ifndef QT_NO_DATESTRING
/*!
\fn QDate QDate::fromString(const QString &string, Qt::DateFormat format)
@@ -1812,7 +1783,7 @@ int QTime::msecsTo(const QTime &t) const
*/
/*!
- \overload
+ \fn QTime::currentTime()
Returns the current time as reported by the system clock.
@@ -1820,53 +1791,6 @@ int QTime::msecsTo(const QTime &t) const
operating system; not all systems provide 1-millisecond accuracy.
*/
-QTime QTime::currentTime()
-{
- QTime ct;
-
-#if defined(Q_OS_WIN)
- SYSTEMTIME st;
- memset(&st, 0, sizeof(SYSTEMTIME));
- GetLocalTime(&st);
- ct.mds = MSECS_PER_HOUR * st.wHour + MSECS_PER_MIN * st.wMinute + 1000 * st.wSecond
- + st.wMilliseconds;
-#if defined(Q_OS_WINCE)
- ct.startTick = GetTickCount() % MSECS_PER_DAY;
-#endif
-#elif defined(Q_OS_SYMBIAN)
- TTime localTime;
- localTime.HomeTime();
- TDateTime localDateTime = localTime.DateTime();
- ct.mds = MSECS_PER_HOUR * localDateTime.Hour() + MSECS_PER_MIN * localDateTime.Minute()
- + 1000 * localDateTime.Second() + (localDateTime.MicroSecond() / 1000);
-#elif defined(Q_OS_UNIX)
- // posix compliant system
- struct timeval tv;
- gettimeofday(&tv, 0);
- time_t ltime = tv.tv_sec;
- tm *t = 0;
-
-#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
- // use the reentrant version of localtime() where available
- tzset();
- tm res;
- t = localtime_r(&ltime, &res);
-#else
- t = localtime(&ltime);
-#endif
- Q_CHECK_PTR(t);
-
- ct.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec
- + tv.tv_usec / 1000;
-#else
- time_t ltime; // no millisecond resolution
- ::time(&ltime);
- const tm *const t = localtime(&ltime);
- ct.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec;
-#endif
- return ct;
-}
-
#ifndef QT_NO_DATESTRING
/*!
\fn QTime QTime::fromString(const QString &string, Qt::DateFormat format)
@@ -2872,63 +2796,280 @@ bool QDateTime::operator<(const QDateTime &other) const
*/
/*!
+ \fn QDateTime QDateTime::currentDateTime()
Returns the current datetime, as reported by the system clock, in
the local time zone.
- \sa QDate::currentDate(), QTime::currentTime(), toTimeSpec()
+ \sa currentDateTimeUtc(), QDate::currentDate(), QTime::currentTime(), toTimeSpec()
*/
-QDateTime QDateTime::currentDateTime()
+/*!
+ \fn QDateTime QDateTime::currentDateTimeUtc()
+ \since 4.7
+ Returns the current datetime, as reported by the system clock, in
+ UTC.
+
+ \sa currentDateTime(), QDate::currentDate(), QTime::currentTime(), toTimeSpec()
+*/
+
+/*!
+ \fn qint64 QDateTime::currentMsecsSinceEpoch()
+ \since 4.7
+
+ Returns the number of milliseconds since 1970-01-01T00:00:00 Universal
+ Coordinated Time. This number is like the POSIX time_t variable, but
+ expressed in milliseconds instead.
+
+ \sa currentDateTime(), currentDateTimeUtc(), toTime_t(), toTimeSpec()
+*/
+
+static inline uint msecsFromDecomposed(int hour, int minute, int sec, int msec = 0)
{
+ return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + 1000 * sec + msec;
+}
+
#if defined(Q_OS_WIN)
+QDate QDate::currentDate()
+{
+ QDate d;
+ SYSTEMTIME st;
+ memset(&st, 0, sizeof(SYSTEMTIME));
+ GetLocalTime(&st);
+ d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
+ return d;
+}
+
+QTime QTime::currentTime()
+{
+ QTime ct;
+ SYSTEMTIME st;
+ memset(&st, 0, sizeof(SYSTEMTIME));
+ GetLocalTime(&st);
+ ct.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
+#if defined(Q_OS_WINCE)
+ ct.startTick = GetTickCount() % MSECS_PER_DAY;
+#endif
+ return ct;
+}
+
+QDateTime QDateTime::currentDateTime()
+{
QDate d;
QTime t;
SYSTEMTIME st;
memset(&st, 0, sizeof(SYSTEMTIME));
GetLocalTime(&st);
d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
- t.mds = MSECS_PER_HOUR * st.wHour + MSECS_PER_MIN * st.wMinute + 1000 * st.wSecond
- + st.wMilliseconds;
+ t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
return QDateTime(d, t);
+}
+
+QDateTime QDateTime::currentDateTimeUtc()
+{
+ QDate d;
+ QTime t;
+ SYSTEMTIME st;
+ memset(&st, 0, sizeof(SYSTEMTIME));
+ GetSystemTime(&st);
+ d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
+ t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
+ return QDateTime(d, t, Qt::UTC);
+}
+
+qint64 QDateTime::currentMsecsSinceEpoch()
+{
+ QDate d;
+ QTime t;
+ SYSTEMTIME st;
+ memset(&st, 0, sizeof(SYSTEMTIME));
+ GetSystemTime(&st);
+
+ return msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds) +
+ qint64(julianDayFromGregorianDate(st.wYear, st.wMonth, st.wDay)
+ - julianDayFromGregorianDate(1970, 1, 1)) * Q_INT64_C(86400000);
+}
+
#elif defined(Q_OS_SYMBIAN)
- return QDateTime(QDate::currentDate(), QTime::currentTime());
+QDate QDate::currentDate()
+{
+ QDate d;
+ TTime localTime;
+ localTime.HomeTime();
+ TDateTime localDateTime = localTime.DateTime();
+ // months and days are zero indexed
+ d.jd = julianDayFromDate(localDateTime.Year(), localDateTime.Month() + 1, localDateTime.Day() + 1 );
+ return d;
+}
+
+QTime QTime::currentTime()
+{
+ QTime ct;
+ TTime localTime;
+ localTime.HomeTime();
+ TDateTime localDateTime = localTime.DateTime();
+ ct.mds = msecsFromDecomposed(localDateTime.Hour(), localDateTime.Minute(),
+ localDateTime.Second(), localDateTime.MicroSecond() / 1000);
+ return ct;
+}
+
+QDateTime QDateTime::currentDateTime()
+{
+ QDate d;
+ QTime ct;
+ TTime localTime;
+ localTime.HomeTime();
+ TDateTime localDateTime = localTime.DateTime();
+ // months and days are zero indexed
+ d.jd = julianDayFromDate(localDateTime.Year(), localDateTime.Month() + 1, localDateTime.Day() + 1);
+ ct.mds = msecsFromDecomposed(localDateTime.Hour(), localDateTime.Minute(),
+ localDateTime.Second(), localDateTime.MicroSecond() / 1000);
+ return QDateTime(d, ct);
+}
+
+QDateTime QDateTime::currentDateTimeUtc()
+{
+ QDate d;
+ QTime ct;
+ TTime gmTime;
+ gmTime.UniversalTime();
+ TDateTime gmtDateTime = gmTime.DateTime();
+ // months and days are zero indexed
+ d.jd = julianDayFromDate(gmtDateTime.Year(), gmtDateTime.Month() + 1, gmtDateTime.Day() + 1);
+ ct.mds = msecsFromDecomposed(gmtDateTime.Hour(), gmtDateTime.Minute(),
+ gmtDateTime.Second(), gmtDateTime.MicroSecond() / 1000);
+ return QDateTime(d, ct, Qt::UTC);
+}
+
+qint64 QDateTime::currentMsecsSinceEpoch()
+{
+ QDate d;
+ QTime ct;
+ TTime gmTime;
+ gmTime.UniversalTime();
+ TDateTime gmtDateTime = gmTime.DateTime();
+
+ // according to the documentation, the value is:
+ // "a date and time as a number of microseconds since midnight, January 1st, 0 AD nominal Gregorian"
+ qint64 value = gmTime.Int64();
+
+ // whereas 1970-01-01T00:00:00 is (in the same representation):
+ static const qint64 unixEpoch = qint64(1970 * 365 + 1970 / 4 - 1970 / 100 + 1970 / 400) * Q_INT64_C(86400)
+ * Q_INT64_C(1000000);
+
+ return (value - unixEpoch) / 1000;
+}
+
+#elif defined(Q_OS_UNIX)
+QDate QDate::currentDate()
+{
+ QDate d;
+ // posix compliant system
+ time_t ltime;
+ time(&ltime);
+ struct tm *t = 0;
+
+#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
+ // use the reentrant version of localtime() where available
+ tzset();
+ struct tm res;
+ t = localtime_r(&ltime, &res);
#else
-#if defined(Q_OS_UNIX)
+ t = localtime(&ltime);
+#endif // !QT_NO_THREAD && _POSIX_THREAD_SAFE_FUNCTIONS
+
+ d.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
+ return d;
+}
+
+QTime QTime::currentTime()
+{
+ QTime ct;
+ // posix compliant system
+ struct timeval tv;
+ gettimeofday(&tv, 0);
+ time_t ltime = tv.tv_sec;
+ struct tm *t = 0;
+
+#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
+ // use the reentrant version of localtime() where available
+ tzset();
+ struct tm res;
+ t = localtime_r(&ltime, &res);
+#else
+ t = localtime(&ltime);
+#endif
+ Q_CHECK_PTR(t);
+
+ ct.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);
+ return ct;
+}
+
+QDateTime QDateTime::currentDateTime()
+{
// posix compliant system
// we have milliseconds
struct timeval tv;
gettimeofday(&tv, 0);
time_t ltime = tv.tv_sec;
- tm *t = 0;
+ struct tm *t = 0;
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
// use the reentrant version of localtime() where available
tzset();
- tm res;
+ struct tm res;
t = localtime_r(&ltime, &res);
#else
t = localtime(&ltime);
#endif
QDateTime dt;
- dt.d->time.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec
- + tv.tv_usec / 1000;
-#else
- time_t ltime; // no millisecond resolution
- ::time(&ltime);
- tm *t = 0;
- localtime(&ltime);
- dt.d->time.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec;
-#endif // Q_OS_UNIX
+ dt.d->time.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);
dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
dt.d->spec = t->tm_isdst > 0 ? QDateTimePrivate::LocalDST :
t->tm_isdst == 0 ? QDateTimePrivate::LocalStandard :
QDateTimePrivate::LocalUnknown;
return dt;
+}
+
+QDateTime QDateTime::currentDateTimeUtc()
+{
+ // posix compliant system
+ // we have milliseconds
+ struct timeval tv;
+ gettimeofday(&tv, 0);
+ time_t ltime = tv.tv_sec;
+ struct tm *t = 0;
+
+#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
+ // use the reentrant version of localtime() where available
+ struct tm res;
+ t = gmtime_r(&ltime, &res);
+#else
+ t = gmtime(&ltime);
#endif
+
+ QDateTime dt;
+ dt.d->time.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);
+
+ dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
+ dt.d->spec = QDateTimePrivate::UTC;
+ return dt;
}
+qint64 QDateTime::currentMsecsSinceEpoch()
+{
+ // posix compliant system
+ // we have milliseconds
+ struct timeval tv;
+ gettimeofday(&tv, 0);
+ return qint64(tv.tv_sec) * Q_INT64_C(1000) + tv.tv_usec / 1000;
+}
+
+#else
+#error "What system is this?"
+#endif
+
/*!
\since 4.2
diff --git a/src/corelib/tools/qdatetime.h b/src/corelib/tools/qdatetime.h
index cb7f5bd..ef5968e 100644
--- a/src/corelib/tools/qdatetime.h
+++ b/src/corelib/tools/qdatetime.h
@@ -204,7 +204,6 @@ private:
friend class QDateTime;
friend class QDateTimePrivate;
- friend class QElapsedTimer;
#ifndef QT_NO_DATASTREAM
friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
@@ -262,11 +261,13 @@ public:
int utcOffset() const;
static QDateTime currentDateTime();
+ static QDateTime currentDateTimeUtc();
#ifndef QT_NO_DATESTRING
static QDateTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
static QDateTime fromString(const QString &s, const QString &format);
#endif
static QDateTime fromTime_t(uint secsSince1Jan1970UTC);
+ static qint64 currentMsecsSinceEpoch();
#ifdef QT3_SUPPORT
inline QT3_SUPPORT void setTime_t(uint secsSince1Jan1970UTC, Qt::TimeSpec spec) {
diff --git a/src/corelib/tools/qelapsedtimer_generic.cpp b/src/corelib/tools/qelapsedtimer_generic.cpp
index db6da46..3feecd6 100644
--- a/src/corelib/tools/qelapsedtimer_generic.cpp
+++ b/src/corelib/tools/qelapsedtimer_generic.cpp
@@ -44,12 +44,6 @@
QT_BEGIN_NAMESPACE
-static qint64 currentDateTimeMsec()
-{
- QDateTime t = QDateTime::currentDateTime();
- return t.toTime_t() * Q_INT64_C(1000) + t.time().msec();
-}
-
QElapsedTimer::ClockType QElapsedTimer::clockType()
{
return SystemTime;
@@ -68,14 +62,14 @@ void QElapsedTimer::start()
qint64 QElapsedTimer::restart()
{
qint64 old = t1;
- t1 = currentDateTimeMsec();
+ t1 = QDateTime::currentMsecsSinceEpoch();
t2 = 0;
return t1 - old;
}
qint64 QElapsedTimer::elapsed() const
{
- return currentDateTimeMsec() - t1;
+ return QDateTime::currentMsecsSinceEpoch() - t1;
}
qint64 QElapsedTimer::msecsSinceReference() const
diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri
index 692fae9..73dae7a 100644
--- a/src/corelib/tools/tools.pri
+++ b/src/corelib/tools/tools.pri
@@ -57,6 +57,7 @@ SOURCES += \
tools/qcryptographichash.cpp \
tools/qdatetime.cpp \
tools/qeasingcurve.cpp \
+ tools/qelapsedtimer.cpp \
tools/qhash.cpp \
tools/qline.cpp \
tools/qlinkedlist.cpp \
@@ -77,13 +78,13 @@ SOURCES += \
tools/qstringlist.cpp \
tools/qtextboundaryfinder.cpp \
tools/qtimeline.cpp \
- tools/qelapsedtimer.cpp \
tools/qvector.cpp \
tools/qvsnprintf.cpp
symbian:SOURCES+=tools/qlocale_symbian.cpp
mac:SOURCES += tools/qelapsedtimer_mac.cpp
+else:symbian:SOURCES += tools/qelapsedtimer_generic.cpp
else:unix:SOURCES += tools/qelapsedtimer_unix.cpp
else:win32:SOURCES += tools/qelapsedtimer_win.cpp
else:SOURCES += tools/qelapsedtimer_generic.cpp