Parcourir la source

优化框架代码

lxg il y a 3 ans
Parent
commit
0996b18c2e
2 fichiers modifiés avec 24 ajouts et 1 suppressions
  1. 13 1
      helper/console/console.go
  2. 11 0
      helper/timeutil/time.go

+ 13 - 1
helper/console/console.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"fmt"
 	"strings"
+	"time"
 	"unicode/utf8"
 )
 
@@ -47,6 +48,17 @@ func calcCharsetWidth(s string) int {
 	}
 }
 
+func (table *Table) toString(v interface{}) string {
+	switch t := v.(type) {
+	case float32, float64:
+		return fmt.Sprintf("%.2f", t)
+	case time.Time:
+		return t.Format("2006-01-02 15:04:05")
+	default:
+		return fmt.Sprint(v)
+	}
+}
+
 func (table *Table) Marshal() ([]byte, error) {
 	columns := make([][]string, 0)
 	var widths []int
@@ -64,7 +76,7 @@ func (table *Table) Marshal() ([]byte, error) {
 		vl := len(vs)
 		column := make([]string, vl)
 		for i, val := range vs {
-			str := fmt.Sprint(val)
+			str := table.toString(val)
 			if vl > 1 {
 				width = calcWidth(str)
 				if width > widths[i] {

+ 11 - 0
helper/timeutil/time.go

@@ -0,0 +1,11 @@
+package timeutil
+
+import "time"
+
+func BeginOfDay(t time.Time) time.Time {
+	return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
+}
+
+func EndOfDay(t time.Time) time.Time {
+	return time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location())
+}