-
Notifications
You must be signed in to change notification settings - Fork 53
Display microsecond timestamps if the log file has them #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
StefanSteiner
wants to merge
5
commits into
tableau:master
Choose a base branch
from
StefanSteiner:dev/ssteiner/support-microseconds
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9b1835c
Added simple microsecond handling
StefanSteiner b89b607
Made a more extensive change to support microsecond timestamps. They …
StefanSteiner e6e8ece
Added a space between the three digits of the millisecond and the las…
StefanSteiner 07cdde1
Revert compiler change
StefanSteiner 67e50f8
Only create the QRegularExpression::microsecondRegex object once and …
StefanSteiner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,284 @@ | ||
| #include "microtimestamp.h" | ||
| #include <QRegularExpression> | ||
| #include <QStringList> | ||
|
|
||
| MicroTimestamp::MicroTimestamp() | ||
| : m_microseconds(0), m_valid(false), m_hasMicros(false) | ||
| { | ||
| } | ||
|
|
||
| MicroTimestamp::MicroTimestamp(const QString ×tampString) | ||
| : m_microseconds(0), m_valid(false), m_hasMicros(false) | ||
| { | ||
| parseTimestamp(timestampString); | ||
| } | ||
|
|
||
| MicroTimestamp::MicroTimestamp(const QDateTime &dateTime) | ||
| : m_dateTime(dateTime), m_microseconds(0), m_valid(dateTime.isValid()), m_hasMicros(false) | ||
| { | ||
| } | ||
|
|
||
| void MicroTimestamp::parseTimestamp(const QString ×tampString) | ||
| { | ||
| if (timestampString.isEmpty()) { | ||
| m_valid = false; | ||
| return; | ||
| } | ||
|
|
||
| // Check for microsecond format: yyyy-MM-ddTHH:mm:ss.zzzzzz (26 chars) | ||
| if (timestampString.length() == 26) { | ||
| // Parse the microsecond timestamp manually | ||
| QRegularExpression regex(R"(^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{6})$)"); | ||
| QRegularExpressionMatch match = regex.match(timestampString); | ||
|
|
||
| if (match.hasMatch()) { | ||
| int year = match.captured(1).toInt(); | ||
| int month = match.captured(2).toInt(); | ||
| int day = match.captured(3).toInt(); | ||
| int hour = match.captured(4).toInt(); | ||
| int minute = match.captured(5).toInt(); | ||
| int second = match.captured(6).toInt(); | ||
| QString microStr = match.captured(7); | ||
|
|
||
| // Create QDateTime with millisecond precision | ||
| int milliseconds = microStr.left(3).toInt(); | ||
| m_microseconds = microStr.right(3).toInt(); // Additional microseconds | ||
|
|
||
| QDate date(year, month, day); | ||
| QTime time(hour, minute, second, milliseconds); | ||
| m_dateTime = QDateTime(date, time); | ||
|
|
||
| m_valid = m_dateTime.isValid(); | ||
| m_hasMicros = true; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Try standard millisecond format: yyyy-MM-ddTHH:mm:ss.zzz (23 chars) | ||
| if (timestampString.length() == 23) { | ||
| m_dateTime = QDateTime::fromString(timestampString, "yyyy-MM-ddTHH:mm:ss.zzz"); | ||
| m_microseconds = 0; | ||
| m_valid = m_dateTime.isValid(); | ||
| m_hasMicros = false; | ||
| return; | ||
| } | ||
|
|
||
| // Try other common formats | ||
| QStringList formats = { | ||
| "yyyy-MM-ddTHH:mm:ss", | ||
| "yyyy-MM-dd HH:mm:ss.zzz", | ||
| "yyyy-MM-dd HH:mm:ss" | ||
| }; | ||
|
|
||
| for (const QString &format : formats) { | ||
| m_dateTime = QDateTime::fromString(timestampString, format); | ||
| if (m_dateTime.isValid()) { | ||
| m_microseconds = 0; | ||
| m_valid = true; | ||
| m_hasMicros = false; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| m_valid = false; | ||
| } | ||
|
|
||
| bool MicroTimestamp::operator<(const MicroTimestamp &other) const | ||
| { | ||
| if (!m_valid || !other.m_valid) { | ||
| return !m_valid && other.m_valid; // Invalid timestamps sort first | ||
| } | ||
|
|
||
| if (m_dateTime != other.m_dateTime) { | ||
| return m_dateTime < other.m_dateTime; | ||
| } | ||
|
|
||
| // If QDateTime is equal, compare microseconds | ||
| return m_microseconds < other.m_microseconds; | ||
| } | ||
|
|
||
| bool MicroTimestamp::operator<=(const MicroTimestamp &other) const | ||
| { | ||
| return *this < other || *this == other; | ||
| } | ||
|
|
||
| bool MicroTimestamp::operator>(const MicroTimestamp &other) const | ||
| { | ||
| return !(*this <= other); | ||
| } | ||
|
|
||
| bool MicroTimestamp::operator>=(const MicroTimestamp &other) const | ||
| { | ||
| return !(*this < other); | ||
| } | ||
|
|
||
| bool MicroTimestamp::operator==(const MicroTimestamp &other) const | ||
| { | ||
| if (!m_valid || !other.m_valid) { | ||
| return m_valid == other.m_valid; | ||
| } | ||
|
|
||
| return m_dateTime == other.m_dateTime && m_microseconds == other.m_microseconds; | ||
| } | ||
|
|
||
| bool MicroTimestamp::operator!=(const MicroTimestamp &other) const | ||
| { | ||
| return !(*this == other); | ||
| } | ||
|
|
||
| QString MicroTimestamp::toString(const QString &format) const | ||
| { | ||
| if (!m_valid) { | ||
| return QString(); | ||
| } | ||
|
|
||
| QString result = m_dateTime.toString(format); | ||
|
|
||
| // If format contains microsecond placeholder and we have microseconds | ||
| if (m_hasMicros && format.contains("zzzzzz")) { | ||
| // Replace the millisecond part with full microsecond precision | ||
| QString msStr = QString("%1").arg(m_dateTime.time().msec(), 3, 10, QChar('0')); | ||
| QString microStr = QString("%1%2").arg(msStr).arg(m_microseconds, 3, 10, QChar('0')); | ||
| result.replace(msStr, microStr); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| QString MicroTimestamp::toDisplayString(bool includeDate) const | ||
| { | ||
| if (!m_valid) { | ||
| return QString(); | ||
| } | ||
|
|
||
| if (m_hasMicros) { | ||
| // Custom formatting for microsecond display with half space separator | ||
| QString dateStr = m_dateTime.date().toString("MM/dd/yyyy"); | ||
| QString timeStr = m_dateTime.time().toString("hh:mm:ss"); | ||
| QString millisStr = QString("%1").arg(m_dateTime.time().msec(), 3, 10, QChar('0')); | ||
| QString microsStr = QString("%1").arg(m_microseconds, 3, 10, QChar('0')); | ||
|
|
||
| // Use regular space to separate milliseconds from microseconds | ||
| QString microStr = QString("%1 %2").arg(millisStr, microsStr); | ||
|
|
||
| if (includeDate) { | ||
| return QString("%1 - %2.%3").arg(dateStr, timeStr, microStr); | ||
| } else { | ||
| return QString("%1.%2").arg(timeStr, microStr); | ||
| } | ||
| } else { | ||
| // Standard formatting for millisecond display | ||
| if (includeDate) { | ||
| return m_dateTime.toString("MM/dd/yyyy - hh:mm:ss.zzz"); | ||
| } else { | ||
| return m_dateTime.toString("hh:mm:ss.zzz"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| QString MicroTimestamp::toCopyString(bool includeDate) const | ||
| { | ||
| if (!m_valid) { | ||
| return QString(); | ||
| } | ||
|
|
||
| if (m_hasMicros) { | ||
| // Format for copying without space separator | ||
| QString dateStr = m_dateTime.date().toString("MM/dd/yyyy"); | ||
| QString timeStr = m_dateTime.time().toString("hh:mm:ss"); | ||
| QString microStr = QString("%1%2") | ||
| .arg(m_dateTime.time().msec(), 3, 10, QChar('0')) | ||
| .arg(m_microseconds, 3, 10, QChar('0')); | ||
|
|
||
| if (includeDate) { | ||
| return QString("%1 - %2.%3").arg(dateStr, timeStr, microStr); | ||
| } else { | ||
| return QString("%1.%2").arg(timeStr, microStr); | ||
| } | ||
| } else { | ||
| // Standard formatting for millisecond display | ||
| if (includeDate) { | ||
| return m_dateTime.toString("MM/dd/yyyy - hh:mm:ss.zzz"); | ||
| } else { | ||
| return m_dateTime.toString("hh:mm:ss.zzz"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| QDateTime MicroTimestamp::toDateTime() const | ||
| { | ||
| return m_dateTime; | ||
| } | ||
|
|
||
| qint64 MicroTimestamp::toMSecsSinceEpoch() const | ||
| { | ||
| if (!m_valid) { | ||
| return 0; | ||
| } | ||
| return m_dateTime.toMSecsSinceEpoch(); | ||
| } | ||
|
|
||
| qint64 MicroTimestamp::toMicroSecsSinceEpoch() const | ||
| { | ||
| if (!m_valid) { | ||
| return 0; | ||
| } | ||
| return m_dateTime.toMSecsSinceEpoch() * 1000 + m_microseconds; | ||
| } | ||
|
|
||
| bool MicroTimestamp::isValid() const | ||
| { | ||
| return m_valid; | ||
| } | ||
|
|
||
| bool MicroTimestamp::hasMicroseconds() const | ||
| { | ||
| return m_hasMicros; | ||
| } | ||
|
|
||
| void MicroTimestamp::registerMetaType() | ||
| { | ||
| qRegisterMetaType<MicroTimestamp>("MicroTimestamp"); | ||
| } | ||
|
|
||
| // QVariant conversion functions | ||
| namespace { | ||
| bool convertToMicroTimestamp(const QVariant &from, MicroTimestamp *to) | ||
StefanSteiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (from.canConvert<MicroTimestamp>()) { | ||
| *to = from.value<MicroTimestamp>(); | ||
| return true; | ||
| } | ||
| if (from.canConvert<QDateTime>()) { | ||
| *to = MicroTimestamp(from.toDateTime()); | ||
| return true; | ||
| } | ||
| if (from.canConvert<QString>()) { | ||
| *to = MicroTimestamp(from.toString()); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| bool convertFromMicroTimestamp(const MicroTimestamp &from, QVariant *to, int targetType) | ||
StefanSteiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| switch (targetType) { | ||
| case QMetaType::QDateTime: | ||
| *to = from.toDateTime(); | ||
| return true; | ||
| case QMetaType::QString: | ||
| *to = from.toDisplayString(true); | ||
| return true; | ||
| case QMetaType::LongLong: | ||
| *to = from.toMicroSecsSinceEpoch(); | ||
| return true; | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static bool microTimestampRegistered = []() { | ||
| MicroTimestamp::registerMetaType(); | ||
| return true; | ||
| }(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #ifndef MICROTIMESTAMP_H | ||
| #define MICROTIMESTAMP_H | ||
|
|
||
| #include <QDateTime> | ||
| #include <QString> | ||
| #include <QVariant> | ||
|
|
||
| class MicroTimestamp | ||
| { | ||
| public: | ||
| MicroTimestamp(); | ||
| explicit MicroTimestamp(const QString ×tampString); | ||
| explicit MicroTimestamp(const QDateTime &dateTime); | ||
|
|
||
| // Comparison operators for sorting | ||
| bool operator<(const MicroTimestamp &other) const; | ||
| bool operator<=(const MicroTimestamp &other) const; | ||
| bool operator>(const MicroTimestamp &other) const; | ||
| bool operator>=(const MicroTimestamp &other) const; | ||
| bool operator==(const MicroTimestamp &other) const; | ||
| bool operator!=(const MicroTimestamp &other) const; | ||
|
|
||
| // Display formatting | ||
| QString toString(const QString &format) const; | ||
| QString toDisplayString(bool includeDate = true) const; | ||
| QString toCopyString(bool includeDate = true) const; // For copying without space separator | ||
|
|
||
| // Conversion functions | ||
| QDateTime toDateTime() const; | ||
| qint64 toMSecsSinceEpoch() const; | ||
| qint64 toMicroSecsSinceEpoch() const; | ||
|
|
||
| // Validation | ||
| bool isValid() const; | ||
| bool hasMicroseconds() const; | ||
|
|
||
| // For QVariant storage | ||
| static void registerMetaType(); | ||
|
|
||
| private: | ||
| QDateTime m_dateTime; | ||
| int m_microseconds; // Additional microseconds (0-999) | ||
| bool m_valid; | ||
| bool m_hasMicros; | ||
|
|
||
| void parseTimestamp(const QString ×tampString); | ||
| }; | ||
|
|
||
| Q_DECLARE_METATYPE(MicroTimestamp) | ||
|
|
||
| #endif // MICROTIMESTAMP_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.