| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- //
- // Timestamp.cpp
- //
- // Library: Foundation
- // Package: DateTime
- // Module: Timestamp
- //
- // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
- // and Contributors.
- //
- // SPDX-License-Identifier: BSL-1.0
- //
- #include "Poco/Timestamp.h"
- #include "Poco/Timespan.h"
- #include "Poco/Exception.h"
- #include <algorithm>
- #undef min
- #undef max
- #include <limits>
- #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"
- #endif
- #ifndef POCO_HAVE_CLOCK_GETTIME
- #if (defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME)) || defined(POCO_VXWORKS) || defined(__QNX__)
- #ifndef __APPLE__ // See GitHub issue #1453 - not available before Mac OS 10.12/iOS 10
- #define POCO_HAVE_CLOCK_GETTIME
- #endif
- #endif
- #endif
- namespace Poco {
- const Timestamp::TimeVal Timestamp::TIMEVAL_MIN = std::numeric_limits<Timestamp::TimeVal>::min();
- const Timestamp::TimeVal Timestamp::TIMEVAL_MAX = std::numeric_limits<Timestamp::TimeVal>::max();
- Timestamp::Timestamp()
- {
- update();
- }
- Timestamp::Timestamp(TimeVal tv)
- {
- _ts = tv;
- }
- Timestamp::Timestamp(const Timestamp& other)
- {
- _ts = other._ts;
- }
- Timestamp::~Timestamp()
- {
- }
- Timestamp& Timestamp::operator = (const Timestamp& other)
- {
- _ts = other._ts;
- return *this;
- }
- Timestamp& Timestamp::operator = (TimeVal tv)
- {
- _ts = tv;
- return *this;
- }
- void Timestamp::swap(Timestamp& timestamp) noexcept
- {
- std::swap(_ts, timestamp._ts);
- }
- Timestamp Timestamp::fromEpochTime(std::time_t t)
- {
- return Timestamp(TimeVal(t)*resolution());
- }
- Timestamp Timestamp::fromUtcTime(UtcTimeVal val)
- {
- val -= (TimeDiff(0x01b21dd2) << 32) + 0x13814000;
- val /= 10;
- return Timestamp(val);
- }
- 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;
- ULARGE_INTEGER ts;
- ts.LowPart = ft.dwLowDateTime;
- ts.HighPart = ft.dwHighDateTime;
- ts.QuadPart -= epoch.QuadPart;
- _ts = ts.QuadPart/10;
- #elif defined(POCO_HAVE_CLOCK_GETTIME)
- 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;
- if (gettimeofday(&tv, NULL))
- throw SystemException("cannot get time of day");
- _ts = TimeVal(tv.tv_sec)*resolution() + tv.tv_usec;
- #endif
- }
- Timestamp Timestamp::operator + (const Timespan& span) const
- {
- return *this + span.totalMicroseconds();
- }
- Timestamp Timestamp::operator - (const Timespan& span) const
- {
- return *this - span.totalMicroseconds();
- }
- Timestamp& Timestamp::operator += (const Timespan& span)
- {
- return *this += span.totalMicroseconds();
- }
- Timestamp& Timestamp::operator -= (const Timespan& span)
- {
- return *this -= span.totalMicroseconds();
- }
- #if defined(_WIN32)
- Timestamp Timestamp::fromFileTimeNP(UInt32 fileTimeLow, UInt32 fileTimeHigh)
- {
- 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 = fileTimeLow;
- ts.HighPart = fileTimeHigh;
- ts.QuadPart -= epoch.QuadPart;
- return Timestamp(ts.QuadPart/10);
- }
- void Timestamp::toFileTimeNP(UInt32& fileTimeLow, UInt32& fileTimeHigh) const
- {
- 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.QuadPart = _ts*10;
- ts.QuadPart += epoch.QuadPart;
- fileTimeLow = ts.LowPart;
- fileTimeHigh = ts.HighPart;
- }
- #endif
- } // namespace Poco
|