mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
publish v12.2.0-alpha6
This commit is contained in:
@@ -3,36 +3,57 @@ package jsonx
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// KitckenTimeLayout represents the "3:04 PM" Go time format, similar to time.Kitcken.
|
||||
const KitckenTimeLayout = "3:04 PM"
|
||||
// KitchenTimeLayout represents the "3:04 PM" Go time format, similar to time.Kitchen.
|
||||
const KitchenTimeLayout = "3:04 PM"
|
||||
|
||||
// KitckenTime holds a json "3:04 PM" time.
|
||||
type KitckenTime time.Time
|
||||
// KitchenTime holds a json "3:04 PM" time.
|
||||
type KitchenTime time.Time
|
||||
|
||||
// ParseKitchenTime reads from "s" and returns the KitckenTime time.
|
||||
func ParseKitchenTime(s string) (KitckenTime, error) {
|
||||
if s == "" || s == "null" {
|
||||
return KitckenTime{}, nil
|
||||
var ErrParseKitchenTimeColon = fmt.Errorf("parse kitchen time: missing ':' character")
|
||||
|
||||
func parseKitchenTime(s string) (KitchenTime, error) {
|
||||
// Remove any second,millisecond variable (probably given by postgres 00:00:00.000000).
|
||||
// required(00:00)remove(:00.000000)
|
||||
|
||||
firstIndex := strings.IndexByte(s, ':')
|
||||
if firstIndex == -1 {
|
||||
return KitchenTime{}, ErrParseKitchenTimeColon
|
||||
} else {
|
||||
nextIndex := strings.LastIndexByte(s, ':')
|
||||
spaceIdx := strings.LastIndexByte(s, ' ')
|
||||
if nextIndex > firstIndex && spaceIdx > 0 {
|
||||
tmp := s[0:nextIndex]
|
||||
s = tmp + s[spaceIdx:]
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
tt time.Time
|
||||
err error
|
||||
)
|
||||
|
||||
tt, err = time.Parse(KitckenTimeLayout, s)
|
||||
tt, err := time.Parse(KitchenTimeLayout, s)
|
||||
if err != nil {
|
||||
return KitckenTime{}, err
|
||||
return KitchenTime{}, err
|
||||
}
|
||||
|
||||
return KitckenTime(tt.UTC()), nil
|
||||
return KitchenTime(tt), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON binds the json "data" to "t" with the `KitckenTimeLayout`.
|
||||
func (t *KitckenTime) UnmarshalJSON(data []byte) error {
|
||||
// ParseKitchenTime reads from "s" and returns the KitchenTime time.
|
||||
func ParseKitchenTime(s string) (KitchenTime, error) {
|
||||
if s == "" || s == "null" {
|
||||
return KitchenTime{}, nil
|
||||
}
|
||||
|
||||
return parseKitchenTime(s)
|
||||
}
|
||||
|
||||
// UnmarshalJSON binds the json "data" to "t" with the `KitchenTimeLayout`.
|
||||
func (t *KitchenTime) UnmarshalJSON(data []byte) error {
|
||||
if t == nil {
|
||||
return fmt.Errorf("kitchen time: dest is nil")
|
||||
}
|
||||
|
||||
if isNull(data) {
|
||||
return nil
|
||||
}
|
||||
@@ -43,17 +64,17 @@ func (t *KitckenTime) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
tt, err := time.Parse(KitckenTimeLayout, string(data))
|
||||
tt, err := parseKitchenTime(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*t = KitckenTime(tt)
|
||||
*t = KitchenTime(tt)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON returns the json representation of the "t".
|
||||
func (t KitckenTime) MarshalJSON() ([]byte, error) {
|
||||
func (t KitchenTime) MarshalJSON() ([]byte, error) {
|
||||
if s := t.String(); s != "" {
|
||||
s = strconv.Quote(s)
|
||||
return []byte(s), nil
|
||||
@@ -64,47 +85,90 @@ func (t KitckenTime) MarshalJSON() ([]byte, error) {
|
||||
|
||||
// IsZero reports whether "t" is zero time.
|
||||
// It completes the pg.Zeroer interface.
|
||||
func (t KitckenTime) IsZero() bool {
|
||||
func (t KitchenTime) IsZero() bool {
|
||||
return t.Value().IsZero()
|
||||
}
|
||||
|
||||
// Value returns the standard time type.
|
||||
func (t KitckenTime) Value() time.Time {
|
||||
func (t KitchenTime) Value() time.Time {
|
||||
return time.Time(t)
|
||||
}
|
||||
|
||||
// String returns the text representation of the date
|
||||
// formatted based on the `KitckenTimeLayout`.
|
||||
// formatted based on the `KitchenTimeLayout`.
|
||||
// If date is zero it returns an empty string.
|
||||
func (t KitckenTime) String() string {
|
||||
func (t KitchenTime) String() string {
|
||||
tt := t.Value()
|
||||
if tt.IsZero() {
|
||||
return ""
|
||||
}
|
||||
|
||||
return tt.Format(KitckenTimeLayout)
|
||||
return tt.Format(KitchenTimeLayout)
|
||||
}
|
||||
|
||||
// Scan completes the pg and native sql driver.Scanner interface
|
||||
// reading functionality of a custom type.
|
||||
func (t *KitckenTime) Scan(src interface{}) error {
|
||||
func (t *KitchenTime) Scan(src interface{}) error {
|
||||
switch v := src.(type) {
|
||||
case time.Time: // type was set to timestamp
|
||||
case time.Time: // type was set to timestamp.
|
||||
if v.IsZero() {
|
||||
return nil // don't set zero, ignore it.
|
||||
}
|
||||
*t = KitckenTime(v)
|
||||
case string:
|
||||
tt, err := ParseKitchenTime(v)
|
||||
*t = KitchenTime(v)
|
||||
case string: // type was set to time, input example: 10:00:00.000000
|
||||
d, err := ParseTimeNotationDuration(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("kitchen time: convert to time notation first: %w", err)
|
||||
}
|
||||
|
||||
s := kitchenTimeStringFromDuration(d.ToDuration())
|
||||
*t, err = ParseKitchenTime(s)
|
||||
return err
|
||||
case int64: // timestamp with integer.
|
||||
u := time.Unix(v/1000, v%1000)
|
||||
s := kitchenTimeStringFromHourAndMinute(u.Hour(), u.Minute())
|
||||
|
||||
tt, err := ParseKitchenTime(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = tt
|
||||
case nil:
|
||||
*t = KitckenTime(time.Time{})
|
||||
*t = KitchenTime(time.Time{})
|
||||
default:
|
||||
return fmt.Errorf("KitckenTime: unknown type of: %T", v)
|
||||
return fmt.Errorf("KitchenTime: unknown type of: %T", v)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func kitchenTimeStringFromDuration(dt time.Duration) string {
|
||||
hour := int(dt.Hours())
|
||||
minute := 0
|
||||
if totalMins := dt.Minutes(); totalMins > 0 {
|
||||
minute := int(totalMins / 60)
|
||||
if minute < 0 {
|
||||
minute = 0
|
||||
}
|
||||
}
|
||||
|
||||
return kitchenTimeStringFromHourAndMinute(hour, minute)
|
||||
}
|
||||
|
||||
func kitchenTimeStringFromHourAndMinute(hour, minute int) string {
|
||||
ampm := "AM"
|
||||
if hour/12 == 1 {
|
||||
ampm = "PM"
|
||||
}
|
||||
th := hour % 12
|
||||
hh := strconv.Itoa(th)
|
||||
if th < 10 {
|
||||
hh = "0" + hh
|
||||
}
|
||||
tm := minute
|
||||
mm := strconv.Itoa(tm)
|
||||
if tm < 10 {
|
||||
mm = "0" + mm
|
||||
}
|
||||
return hh + ":" + mm + " " + ampm
|
||||
}
|
||||
|
||||
@@ -6,34 +6,50 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestJSONKitckenTime(t *testing.T) {
|
||||
data := `{"start": "8:33 AM", "end": "3:04 PM", "nothing": null, "empty": ""}`
|
||||
v := struct {
|
||||
Start KitckenTime `json:"start"`
|
||||
End KitckenTime `json:"end"`
|
||||
Nothing KitckenTime `json:"nothing"`
|
||||
Empty KitckenTime `json:"empty"`
|
||||
}{}
|
||||
err := json.Unmarshal([]byte(data), &v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
func TestJSONKitchenTime(t *testing.T) {
|
||||
tests := []struct {
|
||||
rawData string
|
||||
}{
|
||||
{
|
||||
rawData: `{"start": "8:33 AM", "end": "3:04 PM", "nothing": null, "empty": ""}`,
|
||||
},
|
||||
{
|
||||
rawData: `{"start": "08:33 AM", "end": "03:04 PM", "nothing": null, "empty": ""}`,
|
||||
},
|
||||
{
|
||||
rawData: `{"start": "08:33:00.000000 AM", "end": "03:04 PM", "nothing": null, "empty": ""}`,
|
||||
},
|
||||
}
|
||||
|
||||
if !v.Nothing.IsZero() {
|
||||
t.Fatalf("expected 'nothing' to be zero but got: %v", v.Nothing)
|
||||
}
|
||||
for _, tt := range tests {
|
||||
v := struct {
|
||||
Start KitchenTime `json:"start"`
|
||||
End KitchenTime `json:"end"`
|
||||
Nothing KitchenTime `json:"nothing"`
|
||||
Empty KitchenTime `json:"empty"`
|
||||
}{}
|
||||
|
||||
if !v.Empty.IsZero() {
|
||||
t.Fatalf("expected 'empty' to be zero but got: %v", v.Empty)
|
||||
}
|
||||
err := json.Unmarshal([]byte(tt.rawData), &v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
loc := time.UTC
|
||||
if !v.Nothing.IsZero() {
|
||||
t.Fatalf("expected 'nothing' to be zero but got: %v", v.Nothing)
|
||||
}
|
||||
|
||||
if expected, got := time.Date(0, time.January, 1, 8, 33, 0, 0, loc), v.Start.Value(); expected != got {
|
||||
t.Fatalf("expected 'start' to be: %v but got: %v", expected, got)
|
||||
}
|
||||
if !v.Empty.IsZero() {
|
||||
t.Fatalf("expected 'empty' to be zero but got: %v", v.Empty)
|
||||
}
|
||||
|
||||
if expected, got := time.Date(0, time.January, 1, 15, 4, 0, 0, loc), v.End.Value(); expected != got {
|
||||
t.Fatalf("expected 'start' to be: %v but got: %v", expected, got)
|
||||
loc := time.UTC
|
||||
|
||||
if expected, got := time.Date(0, time.January, 1, 8, 33, 0, 0, loc), v.Start.Value(); expected != got {
|
||||
t.Fatalf("expected 'start' to be: %v but got: %v", expected, got)
|
||||
}
|
||||
|
||||
if expected, got := time.Date(0, time.January, 1, 15, 4, 0, 0, loc), v.End.Value(); expected != got {
|
||||
t.Fatalf("expected 'start' to be: %v but got: %v", expected, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,40 @@ func fmtDuration(d time.Duration) string {
|
||||
h := d / time.Hour
|
||||
d -= h * time.Hour
|
||||
m := d / time.Minute
|
||||
return fmt.Sprintf("%02d:%02d", h, m)
|
||||
return fmt.Sprintf("%02d:%02d:00", h, m) // Manos doesn't care about the seconds.
|
||||
}
|
||||
|
||||
// ParseTimeNotationDuration parses a string to a time notation duration (00:00:00) hours:minutes:seconds.
|
||||
func ParseTimeNotationDuration(s string) (TimeNotationDuration, error) {
|
||||
entries := strings.SplitN(s, ":", 3)
|
||||
if len(entries) < 3 {
|
||||
return TimeNotationDuration(0), fmt.Errorf("invalid duration format: expected hours:minutes:seconds (e.g. 01:05:00) but got: %s", s)
|
||||
}
|
||||
|
||||
hours, err := strconv.Atoi(entries[0])
|
||||
if err != nil {
|
||||
return TimeNotationDuration(0), err
|
||||
}
|
||||
minutes, err := strconv.Atoi(entries[1])
|
||||
if err != nil {
|
||||
return TimeNotationDuration(0), err
|
||||
}
|
||||
|
||||
// remove any .0000
|
||||
secondsStr := strings.TrimSuffix(entries[2], ".000000")
|
||||
seconds, err := strconv.Atoi(secondsStr)
|
||||
if err != nil {
|
||||
return TimeNotationDuration(0), err
|
||||
}
|
||||
|
||||
format := fmt.Sprintf("%02dh%02dm%02ds", hours, minutes, seconds)
|
||||
v, err := time.ParseDuration(format)
|
||||
if err != nil {
|
||||
return TimeNotationDuration(0), err
|
||||
}
|
||||
|
||||
return TimeNotationDuration(v), nil
|
||||
|
||||
}
|
||||
|
||||
func (d TimeNotationDuration) MarshalJSON() ([]byte, error) {
|
||||
@@ -44,26 +77,11 @@ func (d *TimeNotationDuration) UnmarshalJSON(b []byte) error {
|
||||
*d = TimeNotationDuration(value)
|
||||
return nil
|
||||
case string:
|
||||
entries := strings.SplitN(value, ":", 2)
|
||||
if len(entries) < 2 {
|
||||
return fmt.Errorf("invalid duration format: expected hours:minutes:seconds (e.g. 01:05) but got: %s", value)
|
||||
}
|
||||
|
||||
hours, err := strconv.Atoi(entries[0])
|
||||
dv, err := ParseTimeNotationDuration(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
minutes, err := strconv.Atoi(entries[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
format := fmt.Sprintf("%02dh%02dm", hours, minutes)
|
||||
v, err := time.ParseDuration(format)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = TimeNotationDuration(v)
|
||||
*d = dv
|
||||
return nil
|
||||
default:
|
||||
return errors.New("invalid duration")
|
||||
@@ -75,7 +93,7 @@ func (d TimeNotationDuration) ToDuration() time.Duration {
|
||||
}
|
||||
|
||||
func (d TimeNotationDuration) Value() (driver.Value, error) {
|
||||
return int64(d), nil
|
||||
return d.ToDuration(), nil
|
||||
}
|
||||
|
||||
// Set sets the value of duration in nanoseconds.
|
||||
|
||||
Reference in New Issue
Block a user