8000 Implement time.Time pretty printer by k0kubun · Pull Request #11 · k0kubun/pp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implement time.Time pretty printer #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"text/tabwriter"
"time"
)

const (
Expand Down Expand Up @@ -160,6 +161,11 @@ func (p *printer) printMap() {
}

func (p *printer) printStruct() {
if p.value.Type().String() == "time.Time" {
p.printTime()
return
}

p.println(p.typeString() + "{")
p.indented(func() {
for i := 0; i < p.value.NumField(); i++ {
Expand All @@ -171,6 +177,20 @@ func (p *printer) printStruct() {
p.indentPrint("}")
}

func (p *printer) printTime() {
tm := p.value.Interface().(time.Time)
p.printf(
"%s-%s-%s %s:%s:%s %s",
boldBlue(strconv.Itoa(tm.Year())),
boldBlue(fmt.Sprintf("%02d", tm.Month())),
boldBlue(fmt.Sprintf("%02d", tm.Day())),
boldBlue(fmt.Sprintf("%02d", tm.Hour())),
boldBlue(fmt.Sprintf("%02d", tm.Minute())),
boldBlue(fmt.Sprintf("%02d", tm.Second())),
boldBlue(tm.Location().String()),
)
}

func (p *printer) printSlice() {
if p.value.Len() == 0 {
p.printf("%s{}", p.typeString())
Expand Down
14 changes: 14 additions & 0 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"strings"
"testing"
"time"
"unsafe"

// Use fork until following PR is merged
Expand Down Expand Up @@ -42,6 +43,12 @@ type HogeHoge struct {
A interface{}
}

type User struct {
Name string
CreatedAt time.Time
UpdatedAt time.Time
}

type Circular struct {
C *Circular
}
Expand Down Expand Up @@ -90,9 +97,14 @@ var (
`,
},
{"日本\t語\x00", `[red][bold]"[reset][red]日本[reset][magenta][bold]\t[reset][red]語[reset][magenta][bold]\x00[reset][red][bold]"`},
{
time.Date(2015, time.February, 14, 22, 15, 0, 0, time.UTC),
"[blue][bold]2015[reset]-[blue][bold]02[reset]-[blue][bold]14[reset] [blue][bold]22[reset]:[blue][bold]15[reset]:[blue][bold]00[reset] [blue][bold]UTC[reset]",
},
}

arr [3]int
tm = time.Date(2015, time.January, 2, 0, 0, 0, 0, time.UTC)

checkCases = []interface{}{
map[string]int{"hell": 23, "world": 34},
Expand All @@ -111,6 +123,8 @@ var (
new(regexp.Regexp),
unsafe.Pointer(new(regexp.Regexp)),
"日本\t語\n\000\U00101234a",
&tm,
&User{Name: "k0kubun", CreatedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC()},
}
)

Expand Down
0