86 SECS_PER_DAY = 86400, |
86 SECS_PER_DAY = 86400, |
87 MSECS_PER_DAY = 86400000, |
87 MSECS_PER_DAY = 86400000, |
88 SECS_PER_HOUR = 3600, |
88 SECS_PER_HOUR = 3600, |
89 MSECS_PER_HOUR = 3600000, |
89 MSECS_PER_HOUR = 3600000, |
90 SECS_PER_MIN = 60, |
90 SECS_PER_MIN = 60, |
91 MSECS_PER_MIN = 60000 |
91 MSECS_PER_MIN = 60000, |
|
92 JULIAN_DAY_FOR_EPOCH = 2440588 // result of julianDayFromGregorianDate(1970, 1, 1) |
92 }; |
93 }; |
93 |
94 |
94 static inline QDate fixedDate(int y, int m, int d) |
95 static inline QDate fixedDate(int y, int m, int d) |
95 { |
96 { |
96 QDate result(y, m, 1); |
97 QDate result(y, m, 1); |
97 result.setDate(y, m, qMin(d, result.daysInMonth())); |
98 result.setDate(y, m, qMin(d, result.daysInMonth())); |
98 return result; |
99 return result; |
99 } |
100 } |
100 |
101 |
|
102 static inline uint julianDayFromGregorianDate(int year, int month, int day) |
|
103 { |
|
104 // Gregorian calendar starting from October 15, 1582 |
|
105 // Algorithm from Henry F. Fliegel and Thomas C. Van Flandern |
|
106 return (1461 * (year + 4800 + (month - 14) / 12)) / 4 |
|
107 + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12 |
|
108 - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4 |
|
109 + day - 32075; |
|
110 } |
|
111 |
101 static uint julianDayFromDate(int year, int month, int day) |
112 static uint julianDayFromDate(int year, int month, int day) |
102 { |
113 { |
103 if (year < 0) |
114 if (year < 0) |
104 ++year; |
115 ++year; |
105 |
116 |
106 if (year > 1582 || (year == 1582 && (month > 10 || (month == 10 && day >= 15)))) { |
117 if (year > 1582 || (year == 1582 && (month > 10 || (month == 10 && day >= 15)))) { |
107 // Gregorian calendar starting from October 15, 1582 |
118 return julianDayFromGregorianDate(year, month, day); |
108 // Algorithm from Henry F. Fliegel and Thomas C. Van Flandern |
|
109 return (1461 * (year + 4800 + (month - 14) / 12)) / 4 |
|
110 + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12 |
|
111 - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4 |
|
112 + day - 32075; |
|
113 } else if (year < 1582 || (year == 1582 && (month < 10 || (month == 10 && day <= 4)))) { |
119 } else if (year < 1582 || (year == 1582 && (month < 10 || (month == 10 && day <= 4)))) { |
114 // Julian calendar until October 4, 1582 |
120 // Julian calendar until October 4, 1582 |
115 // Algorithm from Frequently Asked Questions about Calendars by Claus Toendering |
121 // Algorithm from Frequently Asked Questions about Calendars by Claus Toendering |
116 int a = (14 - month) / 12; |
122 int a = (14 - month) / 12; |
117 return (153 * (month + (12 * a) - 3) + 2) / 5 |
123 return (153 * (month + (12 * a) - 3) + 2) / 5 |
1810 Returns true if this time is later than or equal to \a t; |
1786 Returns true if this time is later than or equal to \a t; |
1811 otherwise returns false. |
1787 otherwise returns false. |
1812 */ |
1788 */ |
1813 |
1789 |
1814 /*! |
1790 /*! |
1815 \overload |
1791 \fn QTime::currentTime() |
1816 |
1792 |
1817 Returns the current time as reported by the system clock. |
1793 Returns the current time as reported by the system clock. |
1818 |
1794 |
1819 Note that the accuracy depends on the accuracy of the underlying |
1795 Note that the accuracy depends on the accuracy of the underlying |
1820 operating system; not all systems provide 1-millisecond accuracy. |
1796 operating system; not all systems provide 1-millisecond accuracy. |
1821 */ |
1797 */ |
1822 |
|
1823 QTime QTime::currentTime() |
|
1824 { |
|
1825 QTime ct; |
|
1826 |
|
1827 #if defined(Q_OS_WIN) |
|
1828 SYSTEMTIME st; |
|
1829 memset(&st, 0, sizeof(SYSTEMTIME)); |
|
1830 GetLocalTime(&st); |
|
1831 ct.mds = MSECS_PER_HOUR * st.wHour + MSECS_PER_MIN * st.wMinute + 1000 * st.wSecond |
|
1832 + st.wMilliseconds; |
|
1833 #if defined(Q_OS_WINCE) |
|
1834 ct.startTick = GetTickCount() % MSECS_PER_DAY; |
|
1835 #endif |
|
1836 #elif defined(Q_OS_SYMBIAN) |
|
1837 TTime localTime; |
|
1838 localTime.HomeTime(); |
|
1839 TDateTime localDateTime = localTime.DateTime(); |
|
1840 ct.mds = MSECS_PER_HOUR * localDateTime.Hour() + MSECS_PER_MIN * localDateTime.Minute() |
|
1841 + 1000 * localDateTime.Second() + (localDateTime.MicroSecond() / 1000); |
|
1842 #elif defined(Q_OS_UNIX) |
|
1843 // posix compliant system |
|
1844 struct timeval tv; |
|
1845 gettimeofday(&tv, 0); |
|
1846 time_t ltime = tv.tv_sec; |
|
1847 tm *t = 0; |
|
1848 |
|
1849 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) |
|
1850 // use the reentrant version of localtime() where available |
|
1851 tzset(); |
|
1852 tm res; |
|
1853 t = localtime_r(<ime, &res); |
|
1854 #else |
|
1855 t = localtime(<ime); |
|
1856 #endif |
|
1857 Q_CHECK_PTR(t); |
|
1858 |
|
1859 ct.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec |
|
1860 + tv.tv_usec / 1000; |
|
1861 #else |
|
1862 time_t ltime; // no millisecond resolution |
|
1863 ::time(<ime); |
|
1864 const tm *const t = localtime(<ime); |
|
1865 ct.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec; |
|
1866 #endif |
|
1867 return ct; |
|
1868 } |
|
1869 |
1798 |
1870 #ifndef QT_NO_DATESTRING |
1799 #ifndef QT_NO_DATESTRING |
1871 /*! |
1800 /*! |
1872 \fn QTime QTime::fromString(const QString &string, Qt::DateFormat format) |
1801 \fn QTime QTime::fromString(const QString &string, Qt::DateFormat format) |
1873 |
1802 |
2389 d->spec = QDateTimePrivate::LocalUnknown; |
2319 d->spec = QDateTimePrivate::LocalUnknown; |
2390 break; |
2320 break; |
2391 } |
2321 } |
2392 } |
2322 } |
2393 |
2323 |
2394 static uint toTime_tHelper(const QDate &utcDate, const QTime &utcTime) |
2324 qint64 toMSecsSinceEpoch_helper(qint64 jd, int msecs) |
2395 { |
2325 { |
2396 int days = QDate(1970, 1, 1).daysTo(utcDate); |
2326 qint64 days = jd - JULIAN_DAY_FOR_EPOCH; |
2397 int secs = QTime().secsTo(utcTime); |
2327 qint64 retval = (days * MSECS_PER_DAY) + msecs; |
2398 if (days < 0 || (days == 0 && secs < 0)) |
2328 return retval; |
2399 return uint(-1); |
2329 } |
2400 |
2330 |
2401 qlonglong retval = (qlonglong(days) * SECS_PER_DAY) + secs; |
2331 /*! |
2402 if (retval >= Q_INT64_C(0xFFFFFFFF)) |
2332 \since 4.7 |
2403 return uint(-1); |
2333 |
2404 return uint(retval); |
2334 Returns the datetime as the number of milliseconds that have passed |
2405 } |
2335 since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC). |
2406 |
|
2407 /*! |
|
2408 Returns the datetime as the number of seconds that have passed |
|
2409 since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). |
|
2410 |
2336 |
2411 On systems that do not support time zones, this function will |
2337 On systems that do not support time zones, this function will |
2412 behave as if local time were Qt::UTC. |
2338 behave as if local time were Qt::UTC. |
2413 |
2339 |
2414 \sa setTime_t() |
2340 The behavior for this function is undefined if the datetime stored in |
2415 */ |
2341 this object is not valid. However, for all valid dates, this function |
2416 |
2342 returns a unique value. |
2417 uint QDateTime::toTime_t() const |
2343 |
|
2344 \sa toTime_t(), setMSecsSinceEpoch() |
|
2345 */ |
|
2346 qint64 QDateTime::toMSecsSinceEpoch() const |
2418 { |
2347 { |
2419 QDate utcDate; |
2348 QDate utcDate; |
2420 QTime utcTime; |
2349 QTime utcTime; |
2421 d->getUTC(utcDate, utcTime); |
2350 d->getUTC(utcDate, utcTime); |
2422 |
2351 |
2423 return toTime_tHelper(utcDate, utcTime); |
2352 return toMSecsSinceEpoch_helper(utcDate.jd, utcTime.ds()); |
|
2353 } |
|
2354 |
|
2355 /*! |
|
2356 Returns the datetime as the number of seconds that have passed |
|
2357 since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). |
|
2358 |
|
2359 On systems that do not support time zones, this function will |
|
2360 behave as if local time were Qt::UTC. |
|
2361 |
|
2362 \note This function returns a 32-bit unsigned integer, so it does not |
|
2363 support dates before 1970, but it does support dates after |
|
2364 2038-01-19T03:14:06, which may not be valid time_t values. Be careful |
|
2365 when passing those time_t values to system functions, which could |
|
2366 interpret them as negative dates. |
|
2367 |
|
2368 If the date is outside the range 1970-01-01T00:00:00 to |
|
2369 2106-02-07T06:28:14, this function returns -1 cast to an unsigned integer |
|
2370 (i.e., 0xFFFFFFFF). |
|
2371 |
|
2372 To get an extended range, use toMSecsSinceEpoch(). |
|
2373 |
|
2374 \sa toMSecsSinceEpoch(), setTime_t() |
|
2375 */ |
|
2376 |
|
2377 uint QDateTime::toTime_t() const |
|
2378 { |
|
2379 qint64 retval = toMSecsSinceEpoch() / 1000; |
|
2380 if (quint64(retval) >= Q_UINT64_C(0xFFFFFFFF)) |
|
2381 return uint(-1); |
|
2382 return uint(retval); |
|
2383 } |
|
2384 |
|
2385 /*! |
|
2386 \since 4.7 |
|
2387 |
|
2388 Sets the date and time given the number of \a mulliseconds that have |
|
2389 passed since 1970-01-01T00:00:00.000, Coordinated Universal Time |
|
2390 (Qt::UTC). On systems that do not support time zones this function |
|
2391 will behave as if local time were Qt::UTC. |
|
2392 |
|
2393 Note that there are possible values for \a msecs that lie outside the |
|
2394 valid range of QDateTime, both negative and positive. The behavior of |
|
2395 this function is undefined for those values. |
|
2396 |
|
2397 \sa toMSecsSinceEpoch(), setTime_t() |
|
2398 */ |
|
2399 void QDateTime::setMSecsSinceEpoch(qint64 msecs) |
|
2400 { |
|
2401 detach(); |
|
2402 |
|
2403 QDateTimePrivate::Spec oldSpec = d->spec; |
|
2404 |
|
2405 int ddays = msecs / MSECS_PER_DAY; |
|
2406 msecs %= MSECS_PER_DAY; |
|
2407 if (msecs < 0) { |
|
2408 // negative |
|
2409 --ddays; |
|
2410 msecs += MSECS_PER_DAY; |
|
2411 } |
|
2412 |
|
2413 d->date = QDate(1970, 1, 1).addDays(ddays); |
|
2414 d->time = QTime().addMSecs(msecs); |
|
2415 d->spec = QDateTimePrivate::UTC; |
|
2416 |
|
2417 if (oldSpec != QDateTimePrivate::UTC) |
|
2418 d->spec = d->getLocal(d->date, d->time); |
2424 } |
2419 } |
2425 |
2420 |
2426 /*! |
2421 /*! |
2427 \fn void QDateTime::setTime_t(uint seconds) |
2422 \fn void QDateTime::setTime_t(uint seconds) |
2428 |
2423 |
2870 Returns true if this datetime is later than or equal to the |
2892 Returns true if this datetime is later than or equal to the |
2871 \a other datetime; otherwise returns false. |
2893 \a other datetime; otherwise returns false. |
2872 */ |
2894 */ |
2873 |
2895 |
2874 /*! |
2896 /*! |
|
2897 \fn QDateTime QDateTime::currentDateTime() |
2875 Returns the current datetime, as reported by the system clock, in |
2898 Returns the current datetime, as reported by the system clock, in |
2876 the local time zone. |
2899 the local time zone. |
2877 |
2900 |
2878 \sa QDate::currentDate(), QTime::currentTime(), toTimeSpec() |
2901 \sa currentDateTimeUtc(), QDate::currentDate(), QTime::currentTime(), toTimeSpec() |
2879 */ |
2902 */ |
|
2903 |
|
2904 /*! |
|
2905 \fn QDateTime QDateTime::currentDateTimeUtc() |
|
2906 \since 4.7 |
|
2907 Returns the current datetime, as reported by the system clock, in |
|
2908 UTC. |
|
2909 |
|
2910 \sa currentDateTime(), QDate::currentDate(), QTime::currentTime(), toTimeSpec() |
|
2911 */ |
|
2912 |
|
2913 /*! |
|
2914 \fn qint64 QDateTime::currentMSecsSinceEpoch() |
|
2915 \since 4.7 |
|
2916 |
|
2917 Returns the number of milliseconds since 1970-01-01T00:00:00 Universal |
|
2918 Coordinated Time. This number is like the POSIX time_t variable, but |
|
2919 expressed in milliseconds instead. |
|
2920 |
|
2921 \sa currentDateTime(), currentDateTimeUtc(), toTime_t(), toTimeSpec() |
|
2922 */ |
|
2923 |
|
2924 static inline uint msecsFromDecomposed(int hour, int minute, int sec, int msec = 0) |
|
2925 { |
|
2926 return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + 1000 * sec + msec; |
|
2927 } |
|
2928 |
|
2929 #if defined(Q_OS_WIN) |
|
2930 QDate QDate::currentDate() |
|
2931 { |
|
2932 QDate d; |
|
2933 SYSTEMTIME st; |
|
2934 memset(&st, 0, sizeof(SYSTEMTIME)); |
|
2935 GetLocalTime(&st); |
|
2936 d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay); |
|
2937 return d; |
|
2938 } |
|
2939 |
|
2940 QTime QTime::currentTime() |
|
2941 { |
|
2942 QTime ct; |
|
2943 SYSTEMTIME st; |
|
2944 memset(&st, 0, sizeof(SYSTEMTIME)); |
|
2945 GetLocalTime(&st); |
|
2946 ct.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); |
|
2947 #if defined(Q_OS_WINCE) |
|
2948 ct.startTick = GetTickCount() % MSECS_PER_DAY; |
|
2949 #endif |
|
2950 return ct; |
|
2951 } |
2880 |
2952 |
2881 QDateTime QDateTime::currentDateTime() |
2953 QDateTime QDateTime::currentDateTime() |
2882 { |
2954 { |
2883 #if defined(Q_OS_WIN) |
|
2884 QDate d; |
2955 QDate d; |
2885 QTime t; |
2956 QTime t; |
2886 SYSTEMTIME st; |
2957 SYSTEMTIME st; |
2887 memset(&st, 0, sizeof(SYSTEMTIME)); |
2958 memset(&st, 0, sizeof(SYSTEMTIME)); |
2888 GetLocalTime(&st); |
2959 GetLocalTime(&st); |
2889 d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay); |
2960 d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay); |
2890 t.mds = MSECS_PER_HOUR * st.wHour + MSECS_PER_MIN * st.wMinute + 1000 * st.wSecond |
2961 t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); |
2891 + st.wMilliseconds; |
|
2892 return QDateTime(d, t); |
2962 return QDateTime(d, t); |
|
2963 } |
|
2964 |
|
2965 QDateTime QDateTime::currentDateTimeUtc() |
|
2966 { |
|
2967 QDate d; |
|
2968 QTime t; |
|
2969 SYSTEMTIME st; |
|
2970 memset(&st, 0, sizeof(SYSTEMTIME)); |
|
2971 GetSystemTime(&st); |
|
2972 d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay); |
|
2973 t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); |
|
2974 return QDateTime(d, t, Qt::UTC); |
|
2975 } |
|
2976 |
|
2977 qint64 QDateTime::currentMSecsSinceEpoch() |
|
2978 { |
|
2979 QDate d; |
|
2980 QTime t; |
|
2981 SYSTEMTIME st; |
|
2982 memset(&st, 0, sizeof(SYSTEMTIME)); |
|
2983 GetSystemTime(&st); |
|
2984 |
|
2985 return msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds) + |
|
2986 qint64(julianDayFromGregorianDate(st.wYear, st.wMonth, st.wDay) |
|
2987 - julianDayFromGregorianDate(1970, 1, 1)) * Q_INT64_C(86400000); |
|
2988 } |
|
2989 |
2893 #elif defined(Q_OS_SYMBIAN) |
2990 #elif defined(Q_OS_SYMBIAN) |
2894 return QDateTime(QDate::currentDate(), QTime::currentTime()); |
2991 QDate QDate::currentDate() |
|
2992 { |
|
2993 QDate d; |
|
2994 TTime localTime; |
|
2995 localTime.HomeTime(); |
|
2996 TDateTime localDateTime = localTime.DateTime(); |
|
2997 // months and days are zero indexed |
|
2998 d.jd = julianDayFromDate(localDateTime.Year(), localDateTime.Month() + 1, localDateTime.Day() + 1 ); |
|
2999 return d; |
|
3000 } |
|
3001 |
|
3002 QTime QTime::currentTime() |
|
3003 { |
|
3004 QTime ct; |
|
3005 TTime localTime; |
|
3006 localTime.HomeTime(); |
|
3007 TDateTime localDateTime = localTime.DateTime(); |
|
3008 ct.mds = msecsFromDecomposed(localDateTime.Hour(), localDateTime.Minute(), |
|
3009 localDateTime.Second(), localDateTime.MicroSecond() / 1000); |
|
3010 return ct; |
|
3011 } |
|
3012 |
|
3013 QDateTime QDateTime::currentDateTime() |
|
3014 { |
|
3015 QDate d; |
|
3016 QTime ct; |
|
3017 TTime localTime; |
|
3018 localTime.HomeTime(); |
|
3019 TDateTime localDateTime = localTime.DateTime(); |
|
3020 // months and days are zero indexed |
|
3021 d.jd = julianDayFromDate(localDateTime.Year(), localDateTime.Month() + 1, localDateTime.Day() + 1); |
|
3022 ct.mds = msecsFromDecomposed(localDateTime.Hour(), localDateTime.Minute(), |
|
3023 localDateTime.Second(), localDateTime.MicroSecond() / 1000); |
|
3024 return QDateTime(d, ct); |
|
3025 } |
|
3026 |
|
3027 QDateTime QDateTime::currentDateTimeUtc() |
|
3028 { |
|
3029 QDate d; |
|
3030 QTime ct; |
|
3031 TTime gmTime; |
|
3032 gmTime.UniversalTime(); |
|
3033 TDateTime gmtDateTime = gmTime.DateTime(); |
|
3034 // months and days are zero indexed |
|
3035 d.jd = julianDayFromDate(gmtDateTime.Year(), gmtDateTime.Month() + 1, gmtDateTime.Day() + 1); |
|
3036 ct.mds = msecsFromDecomposed(gmtDateTime.Hour(), gmtDateTime.Minute(), |
|
3037 gmtDateTime.Second(), gmtDateTime.MicroSecond() / 1000); |
|
3038 return QDateTime(d, ct, Qt::UTC); |
|
3039 } |
|
3040 |
|
3041 qint64 QDateTime::currentMSecsSinceEpoch() |
|
3042 { |
|
3043 QDate d; |
|
3044 QTime ct; |
|
3045 TTime gmTime; |
|
3046 gmTime.UniversalTime(); |
|
3047 TDateTime gmtDateTime = gmTime.DateTime(); |
|
3048 |
|
3049 // according to the documentation, the value is: |
|
3050 // "a date and time as a number of microseconds since midnight, January 1st, 0 AD nominal Gregorian" |
|
3051 qint64 value = gmTime.Int64(); |
|
3052 |
|
3053 // whereas 1970-01-01T00:00:00 is (in the same representation): |
|
3054 // ((1970 * 365) + (1970 / 4) - (1970 / 100) + (1970 / 400) - 13) * 86400 * 1000000 |
|
3055 static const qint64 unixEpoch = Q_INT64_C(0xdcddb30f2f8000); |
|
3056 |
|
3057 return (value - unixEpoch) / 1000; |
|
3058 } |
|
3059 |
|
3060 #elif defined(Q_OS_UNIX) |
|
3061 QDate QDate::currentDate() |
|
3062 { |
|
3063 QDate d; |
|
3064 // posix compliant system |
|
3065 time_t ltime; |
|
3066 time(<ime); |
|
3067 struct tm *t = 0; |
|
3068 |
|
3069 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) |
|
3070 // use the reentrant version of localtime() where available |
|
3071 tzset(); |
|
3072 struct tm res; |
|
3073 t = localtime_r(<ime, &res); |
2895 #else |
3074 #else |
2896 #if defined(Q_OS_UNIX) |
3075 t = localtime(<ime); |
|
3076 #endif // !QT_NO_THREAD && _POSIX_THREAD_SAFE_FUNCTIONS |
|
3077 |
|
3078 d.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); |
|
3079 return d; |
|
3080 } |
|
3081 |
|
3082 QTime QTime::currentTime() |
|
3083 { |
|
3084 QTime ct; |
|
3085 // posix compliant system |
|
3086 struct timeval tv; |
|
3087 gettimeofday(&tv, 0); |
|
3088 time_t ltime = tv.tv_sec; |
|
3089 struct tm *t = 0; |
|
3090 |
|
3091 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) |
|
3092 // use the reentrant version of localtime() where available |
|
3093 tzset(); |
|
3094 struct tm res; |
|
3095 t = localtime_r(<ime, &res); |
|
3096 #else |
|
3097 t = localtime(<ime); |
|
3098 #endif |
|
3099 Q_CHECK_PTR(t); |
|
3100 |
|
3101 ct.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000); |
|
3102 return ct; |
|
3103 } |
|
3104 |
|
3105 QDateTime QDateTime::currentDateTime() |
|
3106 { |
2897 // posix compliant system |
3107 // posix compliant system |
2898 // we have milliseconds |
3108 // we have milliseconds |
2899 struct timeval tv; |
3109 struct timeval tv; |
2900 gettimeofday(&tv, 0); |
3110 gettimeofday(&tv, 0); |
2901 time_t ltime = tv.tv_sec; |
3111 time_t ltime = tv.tv_sec; |
2902 tm *t = 0; |
3112 struct tm *t = 0; |
2903 |
3113 |
2904 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) |
3114 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) |
2905 // use the reentrant version of localtime() where available |
3115 // use the reentrant version of localtime() where available |
2906 tzset(); |
3116 tzset(); |
2907 tm res; |
3117 struct tm res; |
2908 t = localtime_r(<ime, &res); |
3118 t = localtime_r(<ime, &res); |
2909 #else |
3119 #else |
2910 t = localtime(<ime); |
3120 t = localtime(<ime); |
2911 #endif |
3121 #endif |
2912 |
3122 |
2913 QDateTime dt; |
3123 QDateTime dt; |
2914 dt.d->time.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec |
3124 dt.d->time.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000); |
2915 + tv.tv_usec / 1000; |
|
2916 #else |
|
2917 time_t ltime; // no millisecond resolution |
|
2918 ::time(<ime); |
|
2919 tm *t = 0; |
|
2920 localtime(<ime); |
|
2921 dt.d->time.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec; |
|
2922 #endif // Q_OS_UNIX |
|
2923 |
3125 |
2924 dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); |
3126 dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); |
2925 dt.d->spec = t->tm_isdst > 0 ? QDateTimePrivate::LocalDST : |
3127 dt.d->spec = t->tm_isdst > 0 ? QDateTimePrivate::LocalDST : |
2926 t->tm_isdst == 0 ? QDateTimePrivate::LocalStandard : |
3128 t->tm_isdst == 0 ? QDateTimePrivate::LocalStandard : |
2927 QDateTimePrivate::LocalUnknown; |
3129 QDateTimePrivate::LocalUnknown; |
2928 return dt; |
3130 return dt; |
|
3131 } |
|
3132 |
|
3133 QDateTime QDateTime::currentDateTimeUtc() |
|
3134 { |
|
3135 // posix compliant system |
|
3136 // we have milliseconds |
|
3137 struct timeval tv; |
|
3138 gettimeofday(&tv, 0); |
|
3139 time_t ltime = tv.tv_sec; |
|
3140 struct tm *t = 0; |
|
3141 |
|
3142 #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) |
|
3143 // use the reentrant version of localtime() where available |
|
3144 struct tm res; |
|
3145 t = gmtime_r(<ime, &res); |
|
3146 #else |
|
3147 t = gmtime(<ime); |
2929 #endif |
3148 #endif |
2930 } |
3149 |
|
3150 QDateTime dt; |
|
3151 dt.d->time.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000); |
|
3152 |
|
3153 dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); |
|
3154 dt.d->spec = QDateTimePrivate::UTC; |
|
3155 return dt; |
|
3156 } |
|
3157 |
|
3158 qint64 QDateTime::currentMSecsSinceEpoch() |
|
3159 { |
|
3160 // posix compliant system |
|
3161 // we have milliseconds |
|
3162 struct timeval tv; |
|
3163 gettimeofday(&tv, 0); |
|
3164 return qint64(tv.tv_sec) * Q_INT64_C(1000) + tv.tv_usec / 1000; |
|
3165 } |
|
3166 |
|
3167 #else |
|
3168 #error "What system is this?" |
|
3169 #endif |
2931 |
3170 |
2932 /*! |
3171 /*! |
2933 \since 4.2 |
3172 \since 4.2 |
2934 |
3173 |
2935 Returns a datetime whose date and time are the number of \a seconds |
3174 Returns a datetime whose date and time are the number of \a seconds |