-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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,41 @@ | ||
package datetime | ||
|
||
import "time" | ||
|
||
type Period struct { | ||
Begin time.Time `json:"start" yaml:"start"` | ||
End time.Time `json:"end" yaml:"end"` | ||
} | ||
|
||
const maxNsec = 999999999 | ||
|
||
func NewPeriod(a, b time.Time) Period { | ||
return Period{Begin: StartOfDay(a), End: EndOfDay(b)} | ||
} | ||
|
||
func StartOfDay(t time.Time) time.Time { | ||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC) | ||
} | ||
|
||
func EndOfDay(t time.Time) time.Time { | ||
return time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, maxNsec, time.UTC) | ||
} | ||
|
||
func MonthOf(t time.Time) Period { | ||
var ( | ||
begin = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC) | ||
finalDay = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, time.UTC).Day() | ||
end = time.Date(t.Year(), t.Month(), finalDay, 23, 59, 59, maxNsec, time.UTC) | ||
) | ||
|
||
return Period{Begin: begin, End: end} | ||
} | ||
|
||
func PreviousMonthOf(t time.Time) Period { | ||
return MonthOf(t.AddDate(0, -1, 0)) | ||
} | ||
|
||
func (p Period) Within(o Period) bool { | ||
return (p.Begin.Equal(o.Begin) || p.Begin.After(o.Begin)) && | ||
(p.End.Equal(o.End) || p.End.Before(o.End)) | ||
} |
This file contains 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,47 @@ | ||
package datetime_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/blue-health/blue-go-toolbox/datetime" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPreviousMonthOf(t *testing.T) { | ||
testCases := []struct { | ||
in time.Time | ||
out datetime.Period | ||
}{ | ||
{in: time.Date(2022, time.July, 1, 0, 0, 0, 0, time.UTC), out: datetime.Period{ | ||
Begin: time.Date(2022, time.June, 1, 0, 0, 0, 0, time.UTC), | ||
End: time.Date(2022, time.June, 30, 23, 59, 59, 999999999, time.UTC), | ||
}}, | ||
{in: time.Date(2022, time.August, 1, 0, 0, 0, 0, time.UTC), out: datetime.Period{ | ||
Begin: time.Date(2022, time.July, 1, 0, 0, 0, 0, time.UTC), | ||
End: time.Date(2022, time.July, 31, 23, 59, 59, 999999999, time.UTC), | ||
}}, | ||
{in: time.Date(2022, time.September, 3, 0, 0, 0, 0, time.UTC), out: datetime.Period{ | ||
Begin: time.Date(2022, time.August, 1, 0, 0, 0, 0, time.UTC), | ||
End: time.Date(2022, time.August, 31, 23, 59, 59, 999999999, time.UTC), | ||
}}, | ||
{in: time.Date(2022, time.September, 15, 0, 0, 0, 0, time.UTC), out: datetime.Period{ | ||
Begin: time.Date(2022, time.August, 1, 0, 0, 0, 0, time.UTC), | ||
End: time.Date(2022, time.August, 31, 23, 59, 59, 999999999, time.UTC), | ||
}}, | ||
{in: time.Date(2022, time.September, 30, 0, 0, 0, 0, time.UTC), out: datetime.Period{ | ||
Begin: time.Date(2022, time.August, 1, 0, 0, 0, 0, time.UTC), | ||
End: time.Date(2022, time.August, 31, 23, 59, 59, 999999999, time.UTC), | ||
}}, | ||
{in: time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC), out: datetime.Period{ | ||
Begin: time.Date(2022, time.December, 1, 0, 0, 0, 0, time.UTC), | ||
End: time.Date(2022, time.December, 31, 23, 59, 59, 999999999, time.UTC), | ||
}}, | ||
} | ||
|
||
for _, c := range testCases { | ||
t.Run(c.in.Format(time.RFC1123), func(t *testing.T) { | ||
require.Equal(t, c.out, datetime.PreviousMonthOf(c.in)) | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 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