poco/Foundation/src/Timestamp.cpp
Matej Kenda 52959b91da
DB logger sample (#4759)
* sample(DBLogger): New sample to demonstrate DB logging to a file. (#4750)

* sample(DBLogger): Create messages via SQL logger in a thread (#4750)

* sample(DBLogger): Save messages from SQL files to an SQL database (works with SQLite) (#4750)

* chore(Makefile): Data samples depend on PocoUtil (#4750)

* sample(DBLogger): Refactored DBLogger with std::filesystem::directory_iterator and std::thread.

* sample(DBLogger): Add missing include <condition_variable>

* sample(DBLogger): Extracted log scanning and inserting functionality to class SQLLogInserter.

* sample(DBLogger): Create new logging table only when using demo messages option.

* feat(DBLogger): VS projects

* sample(DBLogger): Acquire options from configuration file.

* feat(DBLogger): regenerate VS projects (progen file change)

* sample(DBLogger): Add example DBLogger.properties file.

* sample(DBLogger): Process as much as possible when stopping processing.

* sample(DBLogger): Meaningful defaults in properties file.

* sample(DBLogger): Verify validity of database session on startup.

* sample(DBLogger): Configure demo SQL channel in properties file.

* chore(DBLogger): style and warnings

---------

Co-authored-by: Aleksandar Fabijanic <aleks-f@users.noreply.github.com>
Co-authored-by: Alex Fabijanic <alex@pocoproject.org>
2024-11-18 17:06:20 +01:00

204 lines
3.8 KiB
C++

//
// 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, nullptr))
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