From 03ec6d0eeeac898659fa072fc2e01341e44a16f4 Mon Sep 17 00:00:00 2001 From: Norwin Date: Mon, 5 Oct 2020 12:23:32 +0000 Subject: [PATCH] print times in local timezone (#217) dont resolve location ahead of time fixup! use local timezone for all printed times fixup! use local timezone for all printed times use local timezone for all printed times Co-authored-by: Norwin Roosen Reviewed-on: https://gitea.com/gitea/tea/pulls/217 Reviewed-by: techknowlogick Reviewed-by: 6543 <6543@noreply.gitea.io> --- cmd/issues/list.go | 2 +- cmd/milestones/issues.go | 2 +- cmd/milestones/list.go | 2 +- cmd/pulls/list.go | 2 +- cmd/releases/list.go | 2 +- modules/print/issue.go | 2 +- modules/print/milestone.go | 2 +- modules/print/print.go | 10 ++++++++++ modules/print/pull.go | 2 +- modules/print/times.go | 8 +------- 10 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/issues/list.go b/cmd/issues/list.go index 6a8b821..ed79bb9 100644 --- a/cmd/issues/list.go +++ b/cmd/issues/list.go @@ -83,7 +83,7 @@ func RunIssuesList(ctx *cli.Context) error { string(issue.State), author, mile, - issue.Updated.Format("2006-01-02 15:04:05"), + print.FormatTime(issue.Updated), }, ) } diff --git a/cmd/milestones/issues.go b/cmd/milestones/issues.go index 2a01372..16debc0 100644 --- a/cmd/milestones/issues.go +++ b/cmd/milestones/issues.go @@ -137,7 +137,7 @@ func runMilestoneIssueList(ctx *cli.Context) error { string(issue.State), kind, name, - issue.Updated.Format("2006-01-02 15:04:05"), + print.FormatTime(issue.Updated), issue.Title, }, ) diff --git a/cmd/milestones/list.go b/cmd/milestones/list.go index 52b073d..ba2db1b 100644 --- a/cmd/milestones/list.go +++ b/cmd/milestones/list.go @@ -72,7 +72,7 @@ func RunMilestonesList(ctx *cli.Context) error { var deadline = "" if m.Deadline != nil && !m.Deadline.IsZero() { - deadline = m.Deadline.Format("2006-01-02 15:04:05") + deadline = print.FormatTime(*m.Deadline) } item := []string{ diff --git a/cmd/pulls/list.go b/cmd/pulls/list.go index 3dc0eb7..73ee2fe 100644 --- a/cmd/pulls/list.go +++ b/cmd/pulls/list.go @@ -84,7 +84,7 @@ func RunPullsList(ctx *cli.Context) error { string(pr.State), author, mile, - pr.Updated.Format("2006-01-02 15:04:05"), + print.FormatTime(*pr.Updated), }, ) } diff --git a/cmd/releases/list.go b/cmd/releases/list.go index 8703a4e..349efe0 100644 --- a/cmd/releases/list.go +++ b/cmd/releases/list.go @@ -65,7 +65,7 @@ func RunReleasesList(ctx *cli.Context) error { []string{ release.TagName, release.Title, - release.PublishedAt.Format("2006-01-02 15:04:05"), + print.FormatTime(release.PublishedAt), status, release.TarURL, }, diff --git a/modules/print/issue.go b/modules/print/issue.go index 35a41dd..ddafa28 100644 --- a/modules/print/issue.go +++ b/modules/print/issue.go @@ -18,7 +18,7 @@ func IssueDetails(issue *gitea.Issue) { issue.Title, issue.State, issue.Poster.UserName, - issue.Created.Format("2006-01-02 15:04:05"), + FormatTime(issue.Created), issue.Body, )) } diff --git a/modules/print/milestone.go b/modules/print/milestone.go index cfed6d4..bb2f470 100644 --- a/modules/print/milestone.go +++ b/modules/print/milestone.go @@ -19,6 +19,6 @@ func MilestoneDetails(milestone *gitea.Milestone) { fmt.Printf("\n%s\n", milestone.Description) } if milestone.Deadline != nil && !milestone.Deadline.IsZero() { - fmt.Printf("\nDeadline: %s\n", milestone.Deadline.Format("2006-01-02 15:04:05")) + fmt.Printf("\nDeadline: %s\n", FormatTime(*milestone.Deadline)) } } diff --git a/modules/print/print.go b/modules/print/print.go index 49f6046..1473d82 100644 --- a/modules/print/print.go +++ b/modules/print/print.go @@ -6,6 +6,7 @@ package print import ( "fmt" + "time" ) // formatSize get kb in int and return string @@ -23,3 +24,12 @@ func formatSize(kb int64) string { } return fmt.Sprintf("%d Tb", gb/1024) } + +// FormatTime give a date-time in local timezone if available +func FormatTime(t time.Time) string { + location, err := time.LoadLocation("Local") + if err != nil { + return t.Format("2006-01-02 15:04 UTC") + } + return t.In(location).Format("2006-01-02 15:04") +} diff --git a/modules/print/pull.go b/modules/print/pull.go index f92cc11..050c483 100644 --- a/modules/print/pull.go +++ b/modules/print/pull.go @@ -18,7 +18,7 @@ func PullDetails(pr *gitea.PullRequest) { pr.Title, pr.State, pr.Poster.UserName, - pr.Created.Format("2006-01-02 15:04:05"), + FormatTime(*pr.Created), pr.Body, )) } diff --git a/modules/print/times.go b/modules/print/times.go index ebd3674..b1d0bd3 100644 --- a/modules/print/times.go +++ b/modules/print/times.go @@ -6,7 +6,6 @@ package print import ( "fmt" - "log" "strconv" "time" @@ -27,11 +26,6 @@ func TrackedTimesList(times []*gitea.TrackedTime, outputType string, from, until var outputValues [][]string var totalDuration int64 - localLoc, err := time.LoadLocation("Local") // local timezone for time formatting - if err != nil { - log.Fatal(err) - } - for _, t := range times { if !from.IsZero() && from.After(t.Created) { continue @@ -45,7 +39,7 @@ func TrackedTimesList(times []*gitea.TrackedTime, outputType string, from, until outputValues = append( outputValues, []string{ - t.Created.In(localLoc).Format("2006-01-02 15:04:05"), + FormatTime(t.Created), "#" + strconv.FormatInt(t.Issue.Index, 10), t.UserName, formatDuration(t.Time, outputType),