mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
add x/timex.GetMonthDays, GetMonthEnd and x/jsonx.SimpleDates type and x/jsonx.GetSimpleDateRange function
This commit is contained in:
parent
c80c5903ab
commit
b727f4b143
|
@ -23,6 +23,8 @@ Developers are not forced to upgrade if they don't really need it. Upgrade whene
|
||||||
|
|
||||||
Changes apply to `main` branch.
|
Changes apply to `main` branch.
|
||||||
|
|
||||||
|
- Add `x/jsonx.GetSimpleDateRange(date, jsonx.WeekRange, time.Monday, time.Sunday)` which returns all dates between the given range and start/end weekday values for WeekRange.
|
||||||
|
- Add `x/timex.GetMonthDays` and `x/timex.GetMonthEnd` functions.
|
||||||
- Add `iris.CookieDomain` and `iris.CookieOverride` cookie options to handle [#2309](https://github.com/kataras/iris/issues/2309).
|
- Add `iris.CookieDomain` and `iris.CookieOverride` cookie options to handle [#2309](https://github.com/kataras/iris/issues/2309).
|
||||||
- New `x/errors.ErrorCodeName.MapErrorFunc`, `x/errors.ErrorCodeName.MapErrors` methods and `x/errors.HandleError` package-level function.
|
- New `x/errors.ErrorCodeName.MapErrorFunc`, `x/errors.ErrorCodeName.MapErrors` methods and `x/errors.HandleError` package-level function.
|
||||||
|
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -15,7 +15,7 @@ require (
|
||||||
github.com/fatih/structs v1.1.0
|
github.com/fatih/structs v1.1.0
|
||||||
github.com/flosch/pongo2/v4 v4.0.2
|
github.com/flosch/pongo2/v4 v4.0.2
|
||||||
github.com/golang/snappy v0.0.4
|
github.com/golang/snappy v0.0.4
|
||||||
github.com/gomarkdown/markdown v0.0.0-20231115200524-a660076da3fd
|
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47
|
||||||
github.com/google/uuid v1.5.0
|
github.com/google/uuid v1.5.0
|
||||||
github.com/gorilla/securecookie v1.1.2
|
github.com/gorilla/securecookie v1.1.2
|
||||||
github.com/iris-contrib/httpexpect/v2 v2.15.2
|
github.com/iris-contrib/httpexpect/v2 v2.15.2
|
||||||
|
|
4
go.sum
generated
4
go.sum
generated
|
@ -76,8 +76,8 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
|
||||||
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
github.com/gomarkdown/markdown v0.0.0-20231115200524-a660076da3fd h1:PppHBegd3uPZ3Y/Iax/2mlCFJm1w4Qf/zP1MdW4ju2o=
|
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47 h1:k4Tw0nt6lwro3Uin8eqoET7MDA4JnT8YgbCjc/g5E3k=
|
||||||
github.com/gomarkdown/markdown v0.0.0-20231115200524-a660076da3fd/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
|
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
|
||||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
|
|
@ -2,9 +2,12 @@ package jsonx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/kataras/iris/v12/x/timex"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SimpleDateLayout represents the "year-month-day" Go time format.
|
// SimpleDateLayout represents the "year-month-day" Go time format.
|
||||||
|
@ -77,6 +80,21 @@ func (t SimpleDate) IsZero() bool {
|
||||||
return t.ToTime().IsZero()
|
return t.ToTime().IsZero()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add returns the date of "t" plus "d".
|
||||||
|
func (t SimpleDate) Add(d time.Duration) SimpleDate {
|
||||||
|
return SimpleDateFromTime(t.ToTime().Add(d))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t SimpleDate) CountPastDays(pastDate SimpleDate) int {
|
||||||
|
t1, t2 := t.ToTime(), pastDate.ToTime()
|
||||||
|
return int(t1.Sub(t2).Hours() / 24)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equal reports back if "t" and "d" equals to the same date.
|
||||||
|
func (t SimpleDate) Equal(d SimpleDate) bool {
|
||||||
|
return t.String() == d.String()
|
||||||
|
}
|
||||||
|
|
||||||
// After reports whether the time instant t is after u.
|
// After reports whether the time instant t is after u.
|
||||||
func (t SimpleDate) After(d2 SimpleDate) bool {
|
func (t SimpleDate) After(d2 SimpleDate) bool {
|
||||||
t1, t2 := t.ToTime(), d2.ToTime()
|
t1, t2 := t.ToTime(), d2.ToTime()
|
||||||
|
@ -134,3 +152,114 @@ func (t *SimpleDate) Scan(src interface{}) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Slice of SimpleDate.
|
||||||
|
type SimpleDates []SimpleDate
|
||||||
|
|
||||||
|
// First returns the first element of the date slice.
|
||||||
|
func (t SimpleDates) First() SimpleDate {
|
||||||
|
if len(t) == 0 {
|
||||||
|
return SimpleDate{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return t[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Last returns the last element of the date slice.
|
||||||
|
func (t SimpleDates) Last() SimpleDate {
|
||||||
|
if len(t) == 0 {
|
||||||
|
return SimpleDate{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return t[len(t)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// DateStrings returns a slice of string representation of the dates.
|
||||||
|
func (t SimpleDates) DateStrings() []string {
|
||||||
|
list := make([]string, 0, len(t))
|
||||||
|
for _, d := range t {
|
||||||
|
list = append(list, d.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan completes the pg and native sql driver.Scanner interface.
|
||||||
|
func (t *SimpleDates) Scan(src interface{}) error {
|
||||||
|
if src == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var data []byte
|
||||||
|
switch v := src.(type) {
|
||||||
|
case []byte:
|
||||||
|
data = v
|
||||||
|
case string:
|
||||||
|
data = []byte(v)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("simple dates: scan: invalid type of: %T", src)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := json.Unmarshal(data, t)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value completes the pg and native sql driver.Valuer interface.
|
||||||
|
func (t SimpleDates) Value() (driver.Value, error) {
|
||||||
|
if len(t) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := json.Marshal(t)
|
||||||
|
return b, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Contains reports if the "date" exists inside "t".
|
||||||
|
func (t SimpleDates) Contains(date SimpleDate) bool {
|
||||||
|
for _, v := range t {
|
||||||
|
if v.Equal(date) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// DateRangeType is the type of the date range.
|
||||||
|
type DateRangeType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DayRange is the date range type of a day.
|
||||||
|
DayRange DateRangeType = "day"
|
||||||
|
// MonthRange is the date range type of a month.
|
||||||
|
MonthRange DateRangeType = "month"
|
||||||
|
// WeekRange is the date range type of a week.
|
||||||
|
WeekRange DateRangeType = "week"
|
||||||
|
// YearRange is the date range type of a year.
|
||||||
|
YearRange DateRangeType = "year"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetSimpleDateRange returns a slice of SimpleDate between "start" and "end" pf "date"
|
||||||
|
// based on given "typ" (WeekRange, MonthRange...).
|
||||||
|
//
|
||||||
|
// Example Code:
|
||||||
|
// date := jsonx.SimpleDateFromTime(time.Now())
|
||||||
|
// dates := jsonx.GetSimpleDateRange(date, jsonx.WeekRange, time.Monday, time.Sunday)
|
||||||
|
func GetSimpleDateRange(date SimpleDate, typ DateRangeType, startWeekday, endWeekday time.Weekday) SimpleDates {
|
||||||
|
var dates []time.Time
|
||||||
|
switch typ {
|
||||||
|
case WeekRange:
|
||||||
|
dates = timex.GetWeekdays(date.ToTime(), startWeekday, endWeekday)
|
||||||
|
case MonthRange:
|
||||||
|
dates = timex.GetMonthDays(date.ToTime())
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("invalid DateRangeType given: %s", typ))
|
||||||
|
}
|
||||||
|
|
||||||
|
simpleDates := make(SimpleDates, len(dates))
|
||||||
|
for i, t := range dates {
|
||||||
|
simpleDates[i] = SimpleDateFromTime(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
return simpleDates
|
||||||
|
}
|
||||||
|
|
|
@ -116,6 +116,18 @@ func GetMonthStart(now time.Time) time.Time {
|
||||||
return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMonthEnd returns the date of the last month day of the current now's month.
|
||||||
|
func GetMonthEnd(now time.Time) time.Time {
|
||||||
|
now = now.UTC()
|
||||||
|
// Add one month to the current date and subtract one day
|
||||||
|
return time.Date(now.Year(), now.Month()+1, 0, 0, 0, 0, 0, now.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMonthDays returns the range between first and last days the current month.
|
||||||
|
func GetMonthDays(now time.Time) (dates []time.Time) {
|
||||||
|
return Between(GetMonthStart(now), GetMonthEnd(now))
|
||||||
|
}
|
||||||
|
|
||||||
// GetYearStart returns the date of the first year of the current now's year.
|
// GetYearStart returns the date of the first year of the current now's year.
|
||||||
func GetYearStart(now time.Time) time.Time {
|
func GetYearStart(now time.Time) time.Time {
|
||||||
return time.Date(now.Year(), 1, 1, 0, 0, 0, 0, now.Location())
|
return time.Date(now.Year(), 1, 1, 0, 0, 0, 0, now.Location())
|
||||||
|
|
|
@ -4,12 +4,12 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris/v12/x/jsonx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const ISO8601Layout = "2006-01-02T15:04:05"
|
||||||
|
|
||||||
func TestMonthAndYearStart(t *testing.T) {
|
func TestMonthAndYearStart(t *testing.T) {
|
||||||
now, err := time.Parse(jsonx.ISO8601Layout, "2021-04-21T00:00:00")
|
now, err := time.Parse(ISO8601Layout, "2021-04-21T00:00:00")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -25,171 +25,6 @@ func TestMonthAndYearStart(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetWeekdays(t *testing.T) {
|
|
||||||
var tests = []struct {
|
|
||||||
Date string
|
|
||||||
ExpectedDates []string
|
|
||||||
Start time.Weekday
|
|
||||||
End time.Weekday
|
|
||||||
}{
|
|
||||||
{ // 1.
|
|
||||||
Date: "2021-02-04T00:00:00",
|
|
||||||
Start: time.Monday,
|
|
||||||
End: time.Sunday,
|
|
||||||
ExpectedDates: []string{
|
|
||||||
"2021-02-01 00:00:00 +0000 UTC",
|
|
||||||
"2021-02-02 00:00:00 +0000 UTC",
|
|
||||||
"2021-02-03 00:00:00 +0000 UTC",
|
|
||||||
"2021-02-04 00:00:00 +0000 UTC",
|
|
||||||
"2021-02-05 00:00:00 +0000 UTC",
|
|
||||||
"2021-02-06 00:00:00 +0000 UTC",
|
|
||||||
"2021-02-07 00:00:00 +0000 UTC",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ // 2. It's monday.
|
|
||||||
Date: "2022-01-17T00:00:00",
|
|
||||||
Start: time.Monday,
|
|
||||||
End: time.Sunday,
|
|
||||||
ExpectedDates: []string{
|
|
||||||
"2022-01-17 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-18 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-19 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-20 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-21 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-22 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-23 00:00:00 +0000 UTC",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ // 3. Test all other days by order.
|
|
||||||
Date: "2022-01-18T00:00:00",
|
|
||||||
Start: time.Monday,
|
|
||||||
End: time.Sunday,
|
|
||||||
ExpectedDates: []string{
|
|
||||||
"2022-01-17 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-18 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-19 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-20 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-21 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-22 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-23 00:00:00 +0000 UTC",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ // 4.
|
|
||||||
Date: "2022-01-19T00:00:00",
|
|
||||||
Start: time.Monday,
|
|
||||||
End: time.Sunday,
|
|
||||||
ExpectedDates: []string{
|
|
||||||
"2022-01-17 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-18 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-19 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-20 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-21 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-22 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-23 00:00:00 +0000 UTC",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ // 5.
|
|
||||||
Date: "2022-01-20T00:00:00",
|
|
||||||
Start: time.Monday,
|
|
||||||
End: time.Sunday,
|
|
||||||
ExpectedDates: []string{
|
|
||||||
"2022-01-17 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-18 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-19 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-20 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-21 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-22 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-23 00:00:00 +0000 UTC",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ // 6.
|
|
||||||
Date: "2022-01-21T00:00:00",
|
|
||||||
Start: time.Monday,
|
|
||||||
End: time.Sunday,
|
|
||||||
ExpectedDates: []string{
|
|
||||||
"2022-01-17 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-18 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-19 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-20 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-21 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-22 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-23 00:00:00 +0000 UTC",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ // 7.
|
|
||||||
Date: "2022-01-22T00:00:00",
|
|
||||||
Start: time.Monday,
|
|
||||||
End: time.Sunday,
|
|
||||||
ExpectedDates: []string{
|
|
||||||
"2022-01-17 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-18 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-19 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-20 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-21 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-22 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-23 00:00:00 +0000 UTC",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ // 8. Sunday.
|
|
||||||
Date: "2022-01-23T00:00:00",
|
|
||||||
Start: time.Monday,
|
|
||||||
End: time.Sunday,
|
|
||||||
ExpectedDates: []string{
|
|
||||||
"2022-01-17 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-18 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-19 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-20 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-21 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-22 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-23 00:00:00 +0000 UTC",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ // 9. Test 1st Jenuary (Saturday) .
|
|
||||||
Date: "2022-01-01T00:00:00",
|
|
||||||
Start: time.Monday,
|
|
||||||
End: time.Sunday,
|
|
||||||
ExpectedDates: []string{
|
|
||||||
"2021-12-27 00:00:00 +0000 UTC", // monday.
|
|
||||||
"2021-12-28 00:00:00 +0000 UTC",
|
|
||||||
"2021-12-29 00:00:00 +0000 UTC",
|
|
||||||
"2021-12-30 00:00:00 +0000 UTC",
|
|
||||||
"2021-12-31 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-01 00:00:00 +0000 UTC",
|
|
||||||
"2022-01-02 00:00:00 +0000 UTC", // sunday.
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ // 10. Test 2021-12-31 (Friday) from sunday to saturday.
|
|
||||||
Date: "2022-01-01T00:00:00",
|
|
||||||
Start: time.Sunday,
|
|
||||||
End: time.Saturday,
|
|
||||||
ExpectedDates: []string{
|
|
||||||
"2021-12-26 00:00:00 +0000 UTC", // sunday.
|
|
||||||
"2021-12-27 00:00:00 +0000 UTC", // monday.
|
|
||||||
"2021-12-28 00:00:00 +0000 UTC",
|
|
||||||
"2021-12-29 00:00:00 +0000 UTC",
|
|
||||||
"2021-12-30 00:00:00 +0000 UTC",
|
|
||||||
"2021-12-31 00:00:00 +0000 UTC", // friday.
|
|
||||||
"2022-01-01 00:00:00 +0000 UTC", // saturday.
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range tests {
|
|
||||||
tt := tests[i]
|
|
||||||
t.Run(fmt.Sprintf("%s[%d]", t.Name(), i+1), func(t *testing.T) {
|
|
||||||
now, err := time.Parse(jsonx.ISO8601Layout, tt.Date)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dates := GetWeekdays(now, tt.Start, tt.End)
|
|
||||||
checkDates(t, "", tt.ExpectedDates, dates)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// t.Logf("[%s] Current day of the week: %s", now.String(), now.Weekday().String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetWeekEnd(t *testing.T) {
|
func TestGetWeekEnd(t *testing.T) {
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
End time.Weekday
|
End time.Weekday
|
||||||
|
@ -228,13 +63,13 @@ func TestGetWeekEnd(t *testing.T) {
|
||||||
tt := tests[i]
|
tt := tests[i]
|
||||||
t.Run(fmt.Sprintf("%s[%d]", t.Name(), i+1), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%s[%d]", t.Name(), i+1), func(t *testing.T) {
|
||||||
for j, date := range tt.Dates {
|
for j, date := range tt.Dates {
|
||||||
now, err := time.Parse(jsonx.ISO8601Layout, date)
|
now, err := time.Parse(ISO8601Layout, date)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
weekEndDate := GetWeekEnd(now, tt.End)
|
weekEndDate := GetWeekEnd(now, tt.End)
|
||||||
if got := weekEndDate.Format(jsonx.ISO8601Layout); got != tt.ExpectedDateEnd {
|
if got := weekEndDate.Format(ISO8601Layout); got != tt.ExpectedDateEnd {
|
||||||
t.Fatalf("[%d] expected week end date: %s but got: %s ", j+1, tt.ExpectedDateEnd, got)
|
t.Fatalf("[%d] expected week end date: %s but got: %s ", j+1, tt.ExpectedDateEnd, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -243,13 +78,13 @@ func TestGetWeekEnd(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetWeekDate(t *testing.T) {
|
func TestGetWeekDate(t *testing.T) {
|
||||||
now, err := jsonx.ParseSimpleDate("2022-02-10")
|
now, err := time.Parse(ISO8601Layout, "2022-02-10T00:00:00")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
Now jsonx.SimpleDate
|
Now time.Time
|
||||||
Start time.Weekday
|
Start time.Weekday
|
||||||
End time.Weekday
|
End time.Weekday
|
||||||
Weekday time.Weekday
|
Weekday time.Weekday
|
||||||
|
@ -317,16 +152,195 @@ func TestGetWeekDate(t *testing.T) {
|
||||||
for i := range tests {
|
for i := range tests {
|
||||||
tt := tests[i]
|
tt := tests[i]
|
||||||
t.Run(fmt.Sprintf("%s[%s]", t.Name(), tt.Weekday.String()), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%s[%s]", t.Name(), tt.Weekday.String()), func(t *testing.T) {
|
||||||
weekDate := GetWeekDate(tt.Now.ToTime(), tt.Weekday, tt.Start, tt.End)
|
weekDate := GetWeekDate(tt.Now, tt.Weekday, tt.Start, tt.End)
|
||||||
if got := weekDate.Format(jsonx.ISO8601Layout); got != tt.ExpectedDate {
|
if got := weekDate.Format(ISO8601Layout); got != tt.ExpectedDate {
|
||||||
t.Fatalf("[%d] expected week date: %s but got: %s ", i+1, tt.ExpectedDate, got)
|
t.Fatalf("[%d] expected week date: %s but got: %s ", i+1, tt.ExpectedDate, got)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetMonthDays(t *testing.T) {
|
||||||
|
now, err := time.Parse(ISO8601Layout, "2023-12-12T00:00:00")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dates := GetMonthDays(now)
|
||||||
|
expectedDates := []string{
|
||||||
|
"2023-12-01 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-02 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-03 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-04 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-05 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-06 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-07 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-08 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-09 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-10 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-11 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-12 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-13 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-14 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-15 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-16 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-17 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-18 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-19 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-20 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-21 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-22 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-23 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-24 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-25 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-26 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-27 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-28 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-29 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-30 00:00:00 +0000 UTC",
|
||||||
|
"2023-12-31 00:00:00 +0000 UTC",
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, d := range dates {
|
||||||
|
if expectedDates[i] != d.String() {
|
||||||
|
t.Fatalf("expected: %s but got: %s", expectedDates[i], d.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetWeekdays(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
Date string
|
||||||
|
ExpectedDates []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Date: "2021-02-04T00:00:00",
|
||||||
|
ExpectedDates: []string{
|
||||||
|
"2021-02-01 00:00:00 +0000 UTC",
|
||||||
|
"2021-02-02 00:00:00 +0000 UTC",
|
||||||
|
"2021-02-03 00:00:00 +0000 UTC",
|
||||||
|
"2021-02-04 00:00:00 +0000 UTC",
|
||||||
|
"2021-02-05 00:00:00 +0000 UTC",
|
||||||
|
"2021-02-06 00:00:00 +0000 UTC",
|
||||||
|
"2021-02-07 00:00:00 +0000 UTC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ // It's monday.
|
||||||
|
Date: "2022-01-17T00:00:00",
|
||||||
|
ExpectedDates: []string{
|
||||||
|
"2022-01-17 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-18 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-19 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-20 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-21 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-22 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-23 00:00:00 +0000 UTC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ // Test all other days by order.
|
||||||
|
Date: "2022-01-18T00:00:00",
|
||||||
|
ExpectedDates: []string{
|
||||||
|
"2022-01-17 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-18 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-19 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-20 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-21 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-22 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-23 00:00:00 +0000 UTC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Date: "2022-01-19T00:00:00",
|
||||||
|
ExpectedDates: []string{
|
||||||
|
"2022-01-17 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-18 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-19 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-20 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-21 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-22 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-23 00:00:00 +0000 UTC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Date: "2022-01-20T00:00:00",
|
||||||
|
ExpectedDates: []string{
|
||||||
|
"2022-01-17 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-18 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-19 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-20 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-21 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-22 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-23 00:00:00 +0000 UTC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Date: "2022-01-21T00:00:00",
|
||||||
|
ExpectedDates: []string{
|
||||||
|
"2022-01-17 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-18 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-19 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-20 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-21 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-22 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-23 00:00:00 +0000 UTC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Date: "2022-01-22T00:00:00",
|
||||||
|
ExpectedDates: []string{
|
||||||
|
"2022-01-17 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-18 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-19 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-20 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-21 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-22 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-23 00:00:00 +0000 UTC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ // Sunday.
|
||||||
|
Date: "2022-01-23T00:00:00",
|
||||||
|
ExpectedDates: []string{
|
||||||
|
"2022-01-17 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-18 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-19 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-20 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-21 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-22 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-23 00:00:00 +0000 UTC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ // Test 1st Jenuary (Saturday) .
|
||||||
|
Date: "2022-01-01T00:00:00",
|
||||||
|
ExpectedDates: []string{
|
||||||
|
"2021-12-27 00:00:00 +0000 UTC", // monday.
|
||||||
|
"2021-12-28 00:00:00 +0000 UTC",
|
||||||
|
"2021-12-29 00:00:00 +0000 UTC",
|
||||||
|
"2021-12-30 00:00:00 +0000 UTC",
|
||||||
|
"2021-12-31 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-01 00:00:00 +0000 UTC",
|
||||||
|
"2022-01-02 00:00:00 +0000 UTC", // sunday.
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range tests {
|
||||||
|
tt := tests[i]
|
||||||
|
t.Run(fmt.Sprintf("%s[%d]", t.Name(), i+1), func(t *testing.T) {
|
||||||
|
now, err := time.Parse(ISO8601Layout, tt.Date)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dates := GetWeekdays(now, time.Monday, time.Sunday)
|
||||||
|
checkDates(t, "", tt.ExpectedDates, dates)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// t.Logf("[%s] Current day of the week: %s", now.String(), now.Weekday().String())
|
||||||
|
}
|
||||||
|
|
||||||
func TestBackwardsToMonday(t *testing.T) {
|
func TestBackwardsToMonday(t *testing.T) {
|
||||||
end, err := time.Parse(jsonx.ISO8601Layout, "2021-04-05T00:00:00")
|
end, err := time.Parse(ISO8601Layout, "2021-04-05T00:00:00")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -339,7 +353,7 @@ func TestBackwardsToMonday(t *testing.T) {
|
||||||
checkDates(t, "", expected, dates)
|
checkDates(t, "", expected, dates)
|
||||||
|
|
||||||
// Test when today is tuesday.
|
// Test when today is tuesday.
|
||||||
end, err = time.Parse(jsonx.ISO8601Layout, "2021-04-06T00:00:00")
|
end, err = time.Parse(ISO8601Layout, "2021-04-06T00:00:00")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -353,7 +367,7 @@ func TestBackwardsToMonday(t *testing.T) {
|
||||||
checkDates(t, "", expected, dates)
|
checkDates(t, "", expected, dates)
|
||||||
|
|
||||||
// Test when today is thursday.
|
// Test when today is thursday.
|
||||||
end, err = time.Parse(jsonx.ISO8601Layout, "2021-04-08T00:00:00")
|
end, err = time.Parse(ISO8601Layout, "2021-04-08T00:00:00")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -369,7 +383,7 @@ func TestBackwardsToMonday(t *testing.T) {
|
||||||
checkDates(t, "", expected, dates)
|
checkDates(t, "", expected, dates)
|
||||||
|
|
||||||
// Test when today is sunday.
|
// Test when today is sunday.
|
||||||
end, err = time.Parse(jsonx.ISO8601Layout, "2021-04-10T00:00:00")
|
end, err = time.Parse(ISO8601Layout, "2021-04-10T00:00:00")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -414,12 +428,12 @@ func checkDates(t *testing.T, typ DateRangeType, expected []string, dates []time
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBetweenAndBackwardsN(t *testing.T) {
|
func TestBetweenAndBackwardsN(t *testing.T) {
|
||||||
start, err := time.Parse(jsonx.ISO8601Layout, "2021-03-26T00:00:00")
|
start, err := time.Parse(ISO8601Layout, "2021-03-26T00:00:00")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
end, err := time.Parse(jsonx.ISO8601Layout, "2021-04-01T00:00:00")
|
end, err := time.Parse(ISO8601Layout, "2021-04-01T00:00:00")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user