1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00
This commit is contained in:
Gerasimos (Makis) Maropoulos
2023-02-19 21:44:37 +02:00
parent 84a1e97a01
commit 5095d862d9
41 changed files with 426 additions and 657 deletions

View File

@@ -135,6 +135,15 @@ func (t ISO8601) String() string {
return tt.Format(ISO8601Layout)
}
// ToSimpleDate converts the current ISO8601 "t" to SimpleDate in specific location.
func (t ISO8601) ToSimpleDate(in *time.Location) SimpleDate {
if in == nil {
in = time.UTC
}
return SimpleDateFromTime(t.ToTime().In(in))
}
// Value returns the database value of time.Time.
func (t ISO8601) Value() (driver.Value, error) {
return time.Time(t), nil

View File

@@ -13,6 +13,13 @@ const SimpleDateLayout = "2006-01-02"
// SimpleDate holds a json "year-month-day" time.
type SimpleDate time.Time
// SimpleDateFromTime accepts a "t" Time and returns
// a SimpleDate. If format fails, it returns the zero value of time.Time.
func SimpleDateFromTime(t time.Time) SimpleDate {
date, _ := ParseSimpleDate(t.Format(SimpleDateLayout))
return date
}
// ParseSimpleDate reads from "s" and returns the SimpleDate time.
func ParseSimpleDate(s string) (SimpleDate, error) {
if s == "" || s == "null" {
@@ -70,6 +77,19 @@ func (t SimpleDate) IsZero() bool {
return t.ToTime().IsZero()
}
// After reports whether the time instant t is after u.
func (t SimpleDate) After(d2 SimpleDate) bool {
t1, t2 := t.ToTime(), d2.ToTime()
return t1.Truncate(24 * time.Hour).After(t2.Truncate(24 * time.Hour))
}
// Before reports whether the time instant t is before u.
func (t SimpleDate) Before(d2 SimpleDate) bool {
t1, t2 := t.ToTime(), d2.ToTime()
return t1.Truncate(24 * time.Hour).Before(t2.Truncate(24 * time.Hour))
// OR: compare year and year's day.
}
// ToTime returns the standard time type.
func (t SimpleDate) ToTime() time.Time {
return time.Time(t)

View File

@@ -0,0 +1,59 @@
package jsonx
import (
"encoding/json"
"testing"
"time"
)
func TestJSONSimpleDate(t *testing.T) {
data := `{"start": "2021-08-20", "end": "2021-12-01", "nothing": null, "empty": ""}`
v := struct {
Start SimpleDate `json:"start"`
End SimpleDate `json:"end"`
Nothing SimpleDate `json:"nothing"`
Empty SimpleDate `json:"empty"`
}{}
err := json.Unmarshal([]byte(data), &v)
if err != nil {
t.Fatal(err)
}
if !v.Nothing.IsZero() {
t.Fatalf("expected 'nothing' to be zero but got: %v", v.Nothing)
}
if !v.Empty.IsZero() {
t.Fatalf("expected 'empty' to be zero but got: %v", v.Empty)
}
loc := time.UTC
if expected, got := time.Date(2021, time.August, 20, 0, 0, 0, 0, loc), v.Start.ToTime(); expected != got {
t.Fatalf("expected 'start' to be: %v but got: %v", expected, got)
}
if expected, got := time.Date(2021, time.December, 1, 0, 0, 0, 0, loc), v.End.ToTime(); expected != got {
t.Fatalf("expected 'start' to be: %v but got: %v", expected, got)
}
}
func TestSimpleDateAterBefore(t *testing.T) {
d1, d2 := SimpleDateFromTime(time.Now()), SimpleDateFromTime(time.Now().AddDate(0, 0, 1))
if d1.After(d2) {
t.Fatalf("[after] expected d1 to be before d2")
}
if !d1.Before(d2) {
t.Fatalf("[before] expected d1 to be before d2")
}
if d2.Before(d1) {
t.Fatalf("[after] expected d2 to be after d1")
}
if !d2.After(d1) {
t.Fatalf("[after] expected d2 to be after d1")
}
}