1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-24 20:35:59 +00:00

add x/timex.GetMonthDays, GetMonthEnd and x/jsonx.SimpleDates type and x/jsonx.GetSimpleDateRange function

This commit is contained in:
Gerasimos (Makis) Maropoulos
2023-12-30 13:23:59 +02:00
parent c80c5903ab
commit b727f4b143
6 changed files with 340 additions and 183 deletions

View File

@@ -2,9 +2,12 @@ package jsonx
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/kataras/iris/v12/x/timex"
)
// SimpleDateLayout represents the "year-month-day" Go time format.
@@ -77,6 +80,21 @@ func (t SimpleDate) IsZero() bool {
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.
func (t SimpleDate) After(d2 SimpleDate) bool {
t1, t2 := t.ToTime(), d2.ToTime()
@@ -134,3 +152,114 @@ func (t *SimpleDate) Scan(src interface{}) error {
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
}