Skip to content

time: add Time.Compare #50770

@jdemeyer

Description

@jdemeyer

There are currently 3 methods to compare time.Time objects: Before(), Equal() and After(). These are analogous to <, == and >. But the equivalents of <= and >= are missing. I suggest adding BeforeEqual() and AfterEqual() to complete this.

I am fully aware that these new methods are not strictly needed: t.BeforeEqual(u) could be written as either t.Before(u) || t.Equal(u) or !t.After(u) or t.Sub(u) <= 0. However, those three are less readable than a simple and obvious t.BeforeEqual(u). I have written code like

for day := begin; /* day <= end */ !day.After(end); day = day.Add(24 * time.Hour) {

where I felt that the comment was needed to clarify the intent.

Implementation of the proposed methods is trivial.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions