Skip to content

Commit

Permalink
[dateutils] adding func BeforeOrEqual and AfterOrEqual (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Hoen <vincent.hoen@ext.groupepvcp.com>
  • Loading branch information
vhoen and Vincent Hoen authored May 16, 2023
1 parent 53ab038 commit 1f39a76
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dateutils/dateutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ func Ceil(t time.Time) time.Time {
// Substract one second and we have today at 23:59:59
return Floor(t.Add(time.Hour * 24)).Add(time.Second * -1)
}

func BeforeOrEqual(milestone time.Time, date time.Time) bool {
return date.UTC().Before(milestone) || date.UTC().Equal(milestone)
}

func AfterOrEqual(milestone time.Time, date time.Time) bool {
return date.UTC().After(milestone) || date.UTC().Equal(milestone)
}
24 changes: 24 additions & 0 deletions dateutils/dateutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,27 @@ func TestCeil(t *testing.T) {
now := time.Now()
assert.Equal(t, "23:59:59", dateutils.Ceil(now).Format("15:04:05"))
}

func TestBeforeOrEqual(t *testing.T) {
milestone, _ := time.Parse("2006-01-02", "2023-01-01")

dBefore, _ := time.Parse("2006-01-02", "2022-12-31")
dEqual, _ := time.Parse("2006-01-02", "2023-01-01")
dAfter, _ := time.Parse("2006-01-02", "2023-01-31")

assert.Equal(t, true, dateutils.BeforeOrEqual(milestone, dBefore))
assert.Equal(t, true, dateutils.BeforeOrEqual(milestone, dEqual))
assert.Equal(t, false, dateutils.BeforeOrEqual(milestone, dAfter))
}

func TestAfterOrEqual(t *testing.T) {
milestone, _ := time.Parse("2006-01-02", "2023-01-01")

dBefore, _ := time.Parse("2006-01-02", "2022-12-31")
dEqual, _ := time.Parse("2006-01-02", "2023-01-01")
dAfter, _ := time.Parse("2006-01-02", "2023-01-31")

assert.Equal(t, false, dateutils.AfterOrEqual(milestone, dBefore))
assert.Equal(t, true, dateutils.AfterOrEqual(milestone, dEqual))
assert.Equal(t, true, dateutils.AfterOrEqual(milestone, dAfter))
}
26 changes: 26 additions & 0 deletions docs/dateutils/afterorequal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Ceil

`func AfterOrEqual(milestone time.Time, date time.Time) bool`

AfterOrEqual - returns true if a date is equal or after to another date

```go
package main

import (
"github.com/Goldziher/go-utils/dateutils"
"time"
)

func main() {
milestone, _ := time.Parse("2006-01-02", "2023-01-01")

dBefore, _ := time.Parse("2006-01-02", "2022-12-31")
dEqual, _ := time.Parse("2006-01-02", "2023-01-01")
dAfter, _ := time.Parse("2006-01-02", "2023-01-31")

dateutils.AfterOrEqual(milestone, dBefore) // false
dateutils.AfterOrEqual(milestone, dEqual) // true
dateutils.AfterOrEqual(milestone, dAfter) // true
}
```
26 changes: 26 additions & 0 deletions docs/dateutils/beforeorequal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Ceil

`func BeforeOrEqual(milestone time.Time, date time.Time) bool`

BeforeOrEqual - returns true if a date is before or equal to another date

```go
package main

import (
"github.com/Goldziher/go-utils/dateutils"
"time"
)

func main() {
milestone, _ := time.Parse("2006-01-02", "2023-01-01")

dBefore, _ := time.Parse("2006-01-02", "2022-12-31")
dEqual, _ := time.Parse("2006-01-02", "2023-01-01")
dAfter, _ := time.Parse("2006-01-02", "2023-01-31")

dateutils.BeforeOrEqual(milestone, dBefore) // true
dateutils.BeforeOrEqual(milestone, dEqual) // true
dateutils.BeforeOrEqual(milestone, dAfter) // false
}
```
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ nav:
- Date Utils:
- dateutils/floor.md
- dateutils/ceil.md
- dateutils/beforeorequal.md
- dateutils/afterorequal.md
- contributing.md
- license.md
theme:
Expand Down

0 comments on commit 1f39a76

Please sign in to comment.