Skip to content

Commit

Permalink
Save timestamps with full precision
Browse files Browse the repository at this point in the history
Based on blole's pull request #7
(jpnurmi#7).
  • Loading branch information
CyberShadow committed Apr 27, 2017
1 parent fe0f368 commit 533c2e0
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions clientbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class CClientBufferMod : public CModule
bool ParseMessage(const CString& line, CNick& nick, CString& cmd, CString& target) const;
timeval GetTimestamp(const CString& identifier, const CString& target);
timeval GetTimestamp(const CBuffer& buffer) const;
bool SetTimestamp(const CString& identifier, const CString& target, const timeval& tv);
bool HasSeenTimestamp(const CString& identifier, const CString& target, const timeval& tv);
bool UpdateTimestamp(const CString& identifier, const CString& target, const timeval& tv);
void UpdateTimestamp(const CClient* client, const CString& target);
Expand Down Expand Up @@ -264,10 +265,15 @@ bool CClientBufferMod::ParseMessage(const CString& line, CNick& nick, CString& c

timeval CClientBufferMod::GetTimestamp(const CString& identifier, const CString& target)
{
CString timestamp = GetNV(identifier + "/" + target);

long long sec = 0;
long usec = 0;
std::sscanf(timestamp.c_str(), "%lld.%06ld", &sec, &usec);

timeval tv;
double timestamp = GetNV(identifier + "/" + target).ToDouble();
tv.tv_sec = timestamp;
tv.tv_usec = (timestamp - tv.tv_sec) * 1000000;
tv.tv_sec = (time_t)sec;
tv.tv_usec = (suseconds_t)usec;
return tv;
}

Expand All @@ -276,6 +282,13 @@ timeval CClientBufferMod::GetTimestamp(const CBuffer& buffer) const
return buffer.GetBufLine(buffer.Size() - 1).GetTime();
}

bool CClientBufferMod::SetTimestamp(const CString& identifier, const CString& target, const timeval& tv)
{
char timestamp[32];
std::snprintf(timestamp, 32, "%lld.%06ld", (long long)tv.tv_sec, (long)tv.tv_usec);
return SetNV(identifier + "/" + target, timestamp);
}

bool CClientBufferMod::HasSeenTimestamp(const CString& identifier, const CString& target, const timeval& tv)
{
const timeval seen = GetTimestamp(identifier, target);
Expand All @@ -284,10 +297,8 @@ bool CClientBufferMod::HasSeenTimestamp(const CString& identifier, const CString

bool CClientBufferMod::UpdateTimestamp(const CString& identifier, const CString& target, const timeval& tv)
{
if (!HasSeenTimestamp(identifier, target, tv)) {
double timestamp = tv.tv_sec + tv.tv_usec / 1000000.0;
return SetNV(identifier + "/" + target, CString(timestamp));
}
if (!HasSeenTimestamp(identifier, target, tv))
return SetTimestamp(identifier, target, tv);
return false;
}

Expand Down

0 comments on commit 533c2e0

Please sign in to comment.