Skip to content

Commit 0b3c9d2

Browse files
authored
feat(SQLChannel): add times property to select UTC or local timestamps (#5399)
* feat(SQLChannel): add times property to select UTC or local timestamps #4925 * fix(SQLChannel): initialize _flush to DEFAULT_FLUSH_SECONDS #5400
1 parent 3a3e8e5 commit 0b3c9d2

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

Data/include/Poco/Data/SQLChannel.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ class Data_API SQLChannel: public Poco::Channel, Poco::Runnable
192192
/// during an extened period of inactivity. This setting ensures that
193193
/// unlogged entries are flushed in such circumstances, even when the
194194
/// minimum batch size was not reached.
195-
/// Zero value means no flushing.
195+
/// Zero value means no flushing. Defaults to 10 seconds.
196+
///
197+
/// * times Whether timestamps in SQL statements written to the file
198+
/// are in UTC ("UTC", default) or local time ("local").
196199

197200
std::string getProperty(const std::string& name) const override;
198201
/// Returns the value of the property with the given name.
@@ -226,6 +229,7 @@ class Data_API SQLChannel: public Poco::Channel, Poco::Runnable
226229
static const std::string PROP_DIRECTORY;
227230
static const std::string PROP_FILE;
228231
static const std::string PROP_FLUSH;
232+
static const std::string PROP_TIMES;
229233

230234
protected:
231235
~SQLChannel() override;
@@ -280,6 +284,10 @@ class Data_API SQLChannel: public Poco::Channel, Poco::Runnable
280284
/// Returns true if there are unflushed log entries
281285
/// and the flush timer has expired.
282286

287+
std::string formatTimestamp(const DateTime& dt) const;
288+
/// Returns the timestamp formatted in UTC or local time,
289+
/// depending on the "times" property.
290+
283291
mutable Poco::FastMutex _mutex;
284292

285293
std::string _connector;
@@ -296,6 +304,7 @@ class Data_API SQLChannel: public Poco::Channel, Poco::Runnable
296304
std::atomic<bool> _bulk;
297305
std::atomic<bool> _throw;
298306
std::atomic<int> _flush;
307+
std::atomic<bool> _localTime;
299308

300309
// members for log entry cache
301310
std::deque<std::string> _source;

Data/src/SQLChannel.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const std::string SQLChannel::PROP_THROW("throw");
5050
const std::string SQLChannel::PROP_DIRECTORY("directory");
5151
const std::string SQLChannel::PROP_FILE("file");
5252
const std::string SQLChannel::PROP_FLUSH("flush");
53+
const std::string SQLChannel::PROP_TIMES("times");
5354

5455

5556
const std::string SQLChannel::SQL_INSERT_STMT = "INSERT INTO %s " \
@@ -66,6 +67,8 @@ SQLChannel::SQLChannel():
6667
_maxSQL(DEFAULT_MAX_SQL_SIZE),
6768
_bulk(true),
6869
_throw(false),
70+
_flush(DEFAULT_FLUSH_SECONDS),
71+
_localTime(false),
6972
_pid(),
7073
_tid(),
7174
_priority(),
@@ -98,6 +101,8 @@ SQLChannel::SQLChannel(const std::string& connector,
98101
_maxSQL(maxSQL),
99102
_bulk(false),
100103
_throw(false),
104+
_flush(DEFAULT_FLUSH_SECONDS),
105+
_localTime(false),
101106
_pid(),
102107
_tid(),
103108
_priority(),
@@ -330,6 +335,14 @@ void SQLChannel::reconnect()
330335
}
331336

332337

338+
std::string SQLChannel::formatTimestamp(const DateTime& dt) const
339+
{
340+
if (_localTime)
341+
return DateTimeFormatter::format(LocalDateTime(dt), "%Y-%m-%d %H:%M:%S.%i");
342+
return DateTimeFormatter::format(dt, "%Y-%m-%d %H:%M:%S.%i");
343+
}
344+
345+
333346
size_t SQLChannel::logToFile(bool flush)
334347
{
335348
if (_source.empty()) return 0u;
@@ -386,7 +399,7 @@ size_t SQLChannel::logToFile(bool flush)
386399
int idx = 0;
387400
for (; it != end; ++idx)
388401
{
389-
std::string dt = DateTimeFormatter::format(_dateTime[idx], "%Y-%m-%d %H:%M:%S.%i");
402+
std::string dt = formatTimestamp(_dateTime[idx]);
390403
tmp.str("");
391404
tmp << "('" << *it << "','" <<
392405
names[idx] << "'," <<
@@ -660,6 +673,10 @@ void SQLChannel::setProperty(const std::string& name, const std::string& value)
660673
{
661674
_flush.store(NumberParser::parse(value));
662675
}
676+
else if (name == PROP_TIMES)
677+
{
678+
_localTime = (value == "local");
679+
}
663680
else
664681
{
665682
Channel::setProperty(name, value);
@@ -734,6 +751,10 @@ std::string SQLChannel::getProperty(const std::string& name) const
734751
{
735752
return std::to_string(_flush);
736753
}
754+
else if (name == PROP_TIMES)
755+
{
756+
return _localTime ? "local" : "UTC";
757+
}
737758
else
738759
{
739760
return Channel::getProperty(name);

Data/testsuite/src/DataTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,12 +1584,19 @@ void DataTest::testSQLChannel()
15841584
}
15851585
}
15861586

1587+
assertEqual(int(SQLChannel::DEFAULT_FLUSH_SECONDS), NumberParser::parse(pChannel->getProperty("flush")));
1588+
15871589
constexpr int mcount{10};
15881590
constexpr int batch{3};
15891591
pChannel->setProperty("minBatch", std::to_string(batch));
15901592
constexpr int flush{1};
15911593
pChannel->setProperty("flush", std::to_string(flush));
15921594
assertEqual(flush, NumberParser::parse(pChannel->getProperty("flush")));
1595+
assertTrue(pChannel->getProperty("times") == "UTC");
1596+
pChannel->setProperty("times", "local");
1597+
assertTrue(pChannel->getProperty("times") == "local");
1598+
pChannel->setProperty("times", "UTC");
1599+
assertTrue(pChannel->getProperty("times") == "UTC");
15931600
for (int i = 0; i < mcount; i++)
15941601
{
15951602
Message msgInfA("InformationSource", Poco::format("%d Informational sync message", i), Message::PRIO_INFORMATION);

0 commit comments

Comments
 (0)