mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-12 18:20:26 +01:00
trunk/branch integration: VxWorks & Wince
This commit is contained in:
parent
35f44e5a2d
commit
fd733bcca4
@ -1,7 +1,7 @@
|
||||
//
|
||||
// Timestamp.cpp
|
||||
//
|
||||
// $Id: //poco/svn/Foundation/src/Timestamp.cpp#2 $
|
||||
// $Id: //poco/1.4/Foundation/src/Timestamp.cpp#2 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: DateTime
|
||||
@ -40,11 +40,121 @@
|
||||
#if defined(POCO_OS_FAMILY_UNIX)
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#if defined(POCO_VXWORKS)
|
||||
#include <timers.h>
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <sys/times.h>
|
||||
#endif
|
||||
#elif defined(POCO_OS_FAMILY_WINDOWS)
|
||||
#include "Poco/UnWindows.h"
|
||||
#if defined(_WIN32_WCE)
|
||||
#include <cmath>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(_WIN32_WCE)
|
||||
|
||||
|
||||
//
|
||||
// See <http://community.opennetcf.com/articles/cf/archive/2007/11/20/getting-a-millisecond-resolution-datetime-under-windows-ce.aspx>
|
||||
// for an explanation of the following code.
|
||||
//
|
||||
// In short: Windows CE system time in most cases only has a resolution of one second.
|
||||
// But we want millisecond resolution.
|
||||
//
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
class TickOffset
|
||||
{
|
||||
public:
|
||||
TickOffset()
|
||||
{
|
||||
SYSTEMTIME st1, st2;
|
||||
std::memset(&st1, 0, sizeof(SYSTEMTIME));
|
||||
std::memset(&st2, 0, sizeof(SYSTEMTIME));
|
||||
GetSystemTime(&st1);
|
||||
while (true)
|
||||
{
|
||||
GetSystemTime(&st2);
|
||||
|
||||
// wait for a rollover
|
||||
if (st1.wSecond != st2.wSecond)
|
||||
{
|
||||
_offset = GetTickCount() % 1000;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void calibrate(int seconds)
|
||||
{
|
||||
SYSTEMTIME st1, st2;
|
||||
systemTime(&st1);
|
||||
|
||||
WORD s = st1.wSecond;
|
||||
int sum = 0;
|
||||
int remaining = seconds;
|
||||
while (remaining > 0)
|
||||
{
|
||||
systemTime(&st2);
|
||||
WORD s2 = st2.wSecond;
|
||||
|
||||
if (s != s2)
|
||||
{
|
||||
remaining--;
|
||||
// store the offset from zero
|
||||
sum += (st2.wMilliseconds > 500) ? (st2.wMilliseconds - 1000) : st2.wMilliseconds;
|
||||
s = st2.wSecond;
|
||||
}
|
||||
}
|
||||
|
||||
// adjust the offset by the average deviation from zero (round to the integer farthest from zero)
|
||||
if (sum < 0)
|
||||
_offset += (int) std::floor(sum / (float)seconds);
|
||||
else
|
||||
_offset += (int) std::ceil(sum / (float)seconds);
|
||||
}
|
||||
|
||||
void systemTime(SYSTEMTIME* pST)
|
||||
{
|
||||
std::memset(pST, 0, sizeof(SYSTEMTIME));
|
||||
|
||||
WORD tick = GetTickCount() % 1000;
|
||||
GetSystemTime(pST);
|
||||
WORD ms = (tick >= _offset) ? (tick - _offset) : (1000 - (_offset - tick));
|
||||
pST->wMilliseconds = ms;
|
||||
}
|
||||
|
||||
void systemTimeAsFileTime(FILETIME* pFT)
|
||||
{
|
||||
SYSTEMTIME st;
|
||||
systemTime(&st);
|
||||
SystemTimeToFileTime(&st, pFT);
|
||||
}
|
||||
|
||||
private:
|
||||
WORD _offset;
|
||||
};
|
||||
|
||||
|
||||
static TickOffset offset;
|
||||
|
||||
|
||||
void GetSystemTimeAsFileTimeWithMillisecondResolution(FILETIME* pFT)
|
||||
{
|
||||
offset.systemTimeAsFileTime(pFT);
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
#endif // defined(_WIN32_WCE)
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@ -111,21 +221,33 @@ void Timestamp::update()
|
||||
{
|
||||
#if defined(POCO_OS_FAMILY_WINDOWS)
|
||||
|
||||
FILETIME ft;
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
|
||||
epoch.LowPart = 0xD53E8000;
|
||||
epoch.HighPart = 0x019DB1DE;
|
||||
FILETIME ft;
|
||||
#if defined(_WIN32_WCE)
|
||||
GetSystemTimeAsFileTimeWithMillisecondResolution(&ft);
|
||||
#else
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
#endif
|
||||
|
||||
ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
|
||||
epoch.LowPart = 0xD53E8000;
|
||||
epoch.HighPart = 0x019DB1DE;
|
||||
|
||||
ULARGE_INTEGER ts;
|
||||
ts.LowPart = ft.dwLowDateTime;
|
||||
ts.HighPart = ft.dwHighDateTime;
|
||||
ts.QuadPart -= epoch.QuadPart;
|
||||
_ts = ts.QuadPart/10;
|
||||
ts.QuadPart -= epoch.QuadPart;
|
||||
_ts = ts.QuadPart/10;
|
||||
|
||||
#elif defined(POCO_VXWORKS)
|
||||
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_REALTIME, &ts))
|
||||
throw SystemException("cannot get time of day");
|
||||
_ts = TimeVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
|
||||
|
||||
#else
|
||||
|
||||
struct timeval tv;
|
||||
struct timeval tv;
|
||||
if (gettimeofday(&tv, NULL))
|
||||
throw SystemException("cannot get time of day");
|
||||
_ts = TimeVal(tv.tv_sec)*resolution() + tv.tv_usec;
|
||||
@ -169,5 +291,4 @@ void Timestamp::toFileTimeNP(UInt32& fileTimeLow, UInt32& fileTimeHigh) const
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
@ -49,13 +49,13 @@ public:
|
||||
{
|
||||
tzset();
|
||||
}
|
||||
|
||||
int timeZone()
|
||||
{
|
||||
#if defined(__APPLE__) || defined(__FreeBSD__) // no timezone global var
|
||||
std::time_t now = std::time(NULL);
|
||||
struct std::tm t;
|
||||
gmtime_r(&now, &t);
|
||||
|
||||
int timeZone()
|
||||
{
|
||||
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(POCO_ANDROID) // no timezone global var
|
||||
std::time_t now = std::time(NULL);
|
||||
struct std::tm t;
|
||||
gmtime_r(&now, &t);
|
||||
std::time_t utc = std::mktime(&t);
|
||||
return now - utc;
|
||||
#elif defined(__CYGWIN__)
|
||||
|
Loading…
Reference in New Issue
Block a user