Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Timestamp comparison is wrong #1037

Copy link
Copy link
Closed
@magmax

Description

@magmax
Issue body actions

Currently, samples.Timestamp implementation for comparisons is:

    def __gt__(self, other: "Timestamp") -> bool:
        return self.sec > other.sec or self.nsec > other.nsec

    def __lt__(self, other: "Timestamp") -> bool:
        return self.sec < other.sec or self.nsec < other.nsec

what might not compare correctly. Example:

>>> a = Timestamp(2, 5)
>>> b = Timestamp(3, 4)
>>> a > b
True

In the example, the first part is 2<3, what is false, forcing to perform the next part, what is 5>4, returning True.

A good implementation could be:

    def __gt__(self, other: "Timestamp") -> bool:
        if self.sec == other.sec:
            return self.nsec > other.nsec
        return self.sec > other.sec

    def __lt__(self, other: "Timestamp") -> bool:
        if self.sec == other.sec:
            return self.nsec < other.nsec
        return self.sec < other.sec

Another option, perhaps even better, is to rely on python's tuples implementation:

    def __gt__(self, other: "Timestamp") -> bool:
        return (self.sec, self.nsec) > (other.sec, other.nsec)

    def __lt__(self, other: "Timestamp") -> bool:
        return (self.sec, self.nsec) < (other.sec, other.nsec)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.