Give relative time

This commit is contained in:
Alex Dadgar 2016-06-15 15:26:58 -07:00
parent bf14fd355f
commit 122d035457
4 changed files with 19 additions and 4 deletions

View File

@ -49,6 +49,13 @@ func formatTime(t time.Time) string {
return t.Format("01/02/06 15:04:05 MST")
}
// formatTimeDifference takes two times and determines their duration difference
// truncating to a passed unit.
// E.g. formatTimeDifference(first=1m22s33ms, second=1m28s55ms, time.Second) -> 6s
func formatTimeDifference(first, second time.Time, d time.Duration) string {
return second.Truncate(d).Sub(first.Truncate(d)).String()
}
// getLocalNodeID returns the node ID of the local Nomad Client and an error if
// it couldn't be determined or the Agent is not running in Client mode.
func getLocalNodeID(client *api.Client) (string, error) {

View File

@ -4,6 +4,7 @@ import (
"fmt"
"sort"
"strings"
"time"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/jobspec"
@ -189,8 +190,9 @@ func formatDryRun(resp *api.JobPlanResponse) string {
out += fmt.Sprintf("[green]- Rolling update, next evaluation will be in %s.\n", rolling.Wait)
}
if !resp.NextPeriodicLaunch.IsZero() {
out += fmt.Sprintf("[green]- If submitted now, next periodic launch would be at %s.\n", formatTime(resp.NextPeriodicLaunch))
if next := resp.NextPeriodicLaunch; !next.IsZero() {
out += fmt.Sprintf("[green]- If submitted now, next periodic launch would be at %s (%s from now).\n",
formatTime(next), formatTimeDifference(time.Now().UTC(), next, time.Second))
}
out = strings.TrimSuffix(out, "\n")

View File

@ -193,7 +193,10 @@ func (c *RunCommand) Run(args []string) int {
if detach || periodic {
c.Ui.Output("Job registration successful")
if periodic {
c.Ui.Output(fmt.Sprintf("Approximate next launch time: %v", formatTime(job.Periodic.Next(time.Now().UTC()))))
now := time.Now().UTC()
next := job.Periodic.Next(now)
c.Ui.Output(fmt.Sprintf("Approximate next launch time: %s (%s from now)",
formatTime(next), formatTimeDifference(now, next, time.Second)))
} else {
c.Ui.Output("Evaluation ID: " + evalID)
}

View File

@ -164,8 +164,11 @@ func (c *StatusCommand) Run(args []string) int {
}
if periodic {
now := time.Now().UTC()
next := sJob.Periodic.Next(now)
basic = append(basic, fmt.Sprintf("Next Periodic Launch|%s",
formatTime(sJob.Periodic.Next(time.Now().UTC()))))
fmt.Sprintf("%s (%s from now)",
formatTime(next), formatTimeDifference(now, next, time.Second))))
}
c.Ui.Output(formatKV(basic))