Пример #1
0
func fmtDuration(duration time.Duration) string {
	result := []string{}
	started := false
	if duration > 7*24*time.Hour || started {
		started = true
		weeks := int(duration.Hours() / 24 / 7)
		duration %= 7 * 24 * time.Hour
		result = append(result, fmt.Sprintf("%2dw", weeks))
	}
	if duration > 24*time.Hour || started {
		started = true
		days := int(duration.Hours() / 24)
		duration %= 24 * time.Hour
		result = append(result, fmt.Sprintf("%2dd", days))
	}
	if duration > time.Hour || started {
		started = true
		hours := int(duration.Hours())
		duration %= time.Hour
		result = append(result, fmt.Sprintf("%2dh", hours))
	}
	if duration > time.Minute || started {
		started = true
		minutes := int(duration.Minutes())
		duration %= time.Minute
		result = append(result, fmt.Sprintf("%2dm", minutes))
	}
	seconds := int(duration.Seconds())
	result = append(result, fmt.Sprintf("%2ds", seconds))
	return strings.TrimSpace(strings.Join(result, ""))
}
Пример #2
0
func roundDuration(d time.Duration) time.Duration {
	rd := time.Duration(d.Minutes()) * time.Minute
	if rd < d {
		rd += time.Minute
	}
	return rd
}
Пример #3
0
// LogFetches processes all the Fetch items are writes the log to S3 for the parsers to start working.
func LogFetches(logChan <-chan *Fetch, errChan <-chan *FetchError, duration *time.Duration) {
	report := &Report{Novel: 0, Errors: 0, Total: 0}
	fetchDuration := &FetchDuration{Hours: duration.Hours(), Minutes: duration.Minutes(), Seconds: duration.Seconds()}
	fetches := &Fetches{}
	for fetch := range logChan {
		fetches.Fetch = append(fetches.Fetch, fetch)
		if fetch.Novel {
			report.Novel++
		}
		report.Total++
	}

	for err := range errChan {
		fetches.FetchError = append(fetches.FetchError, err)
		report.Errors++
		report.Total++
	}

	fetches.Meta = &Meta{FetchDuration: fetchDuration, Report: report}

	content, _ := xml.MarshalIndent(fetches, "", "\t")

	// Write to S3.
	logPath := logFilePath()
	s3content := []byte(xml.Header + string(content))
	S3BucketFromOS().Put(logPath, s3content, "application/xml", s3.Private)
}
Пример #4
0
// FormattedSecondsToMax8Chars ...
func FormattedSecondsToMax8Chars(t time.Duration) (string, error) {
	sec := t.Seconds()
	min := t.Minutes()
	hour := t.Hours()

	if sec < 1.0 {
		// 0.999999 sec -> 0.99 sec
		return fmt.Sprintf("%.2f sec", sec), nil // 8
	} else if sec < 10.0 {
		// 9.99999 sec -> 9.99 sec
		return fmt.Sprintf("%.2f sec", sec), nil // 8
	} else if sec < 600 {
		// 599,999 sec -> 599 sec
		return fmt.Sprintf("%.f sec", sec), nil // 7
	} else if min < 60 {
		// 59,999 min -> 59.9 min
		return fmt.Sprintf("%.1f min", min), nil // 8
	} else if hour < 10 {
		// 9.999 hour -> 9.9 hour
		return fmt.Sprintf("%.1f hour", hour), nil // 8
	} else if hour < 1000 {
		// 999,999 hour -> 999 hour
		return fmt.Sprintf("%.f hour", hour), nil // 8
	}

	return "", fmt.Errorf("time (%f hour) greater then max allowed (999 hour)", hour)
}
Пример #5
0
func relativeTime(duration time.Duration) string {
	hours := int64(math.Abs(duration.Hours()))
	minutes := int64(math.Abs(duration.Minutes()))
	when := ""
	switch {
	case hours >= (365 * 24):
		when = "Over an year ago"
	case hours > (30 * 24):
		when = fmt.Sprintf("%d months ago", int64(hours/(30*24)))
	case hours == (30 * 24):
		when = "a month ago"
	case hours > 24:
		when = fmt.Sprintf("%d days ago", int64(hours/24))
	case hours == 24:
		when = "yesterday"
	case hours >= 2:
		when = fmt.Sprintf("%d hours ago", hours)
	case hours > 1:
		when = "over an hour ago"
	case hours == 1:
		when = "an hour ago"
	case minutes >= 2:
		when = fmt.Sprintf("%d minutes ago", minutes)
	case minutes > 1:
		when = "a minute ago"
	default:
		when = "just now"
	}
	return when
}
Пример #6
0
// SetDuration sets the duration of an Usage
func (u *Usage) SetDuration(duration time.Duration) error {
	minutes := new(big.Rat).SetFloat64(duration.Minutes())
	factor := new(big.Rat).SetInt64((u.Object.UsageGranularity / time.Minute).Nanoseconds())
	quantity := new(big.Rat).Quo(minutes, factor)
	ceil := new(big.Rat).SetInt(ratCeil(quantity))
	return u.SetQuantity(ceil)
}
Пример #7
0
func PrettyTime(d time.Duration) string {
	if d.Minutes() > 1 {
		return "" + strconv.Itoa(int(d.Minutes()))
	} else {
		return "Less than 1 min"
	}
}
Пример #8
0
func (this *ProgressBarSimple) renderFixedSizeDuration(dur time.Duration) string {
	h := dur.Hours()
	m := dur.Minutes()
	if h > pbYear*10 {
		y := int(h / pbYear)
		h -= float64(y) * pbYear
		w := h / pbWeek
		return fmt.Sprintf("%02dy%02dw", y, int(w))
	} else if h > pbWeek*10 {
		return fmt.Sprintf("%05dw", int(h/pbWeek))
	} else if h > pbDay*2 {
		d := int(h / pbDay)
		h -= pbDay * float64(d)
		return fmt.Sprintf("%02dd%02dh", d, int(h))
	} else if h > 1 {
		o := int(h)
		i := m - float64(o)*60
		return fmt.Sprintf("%02dh%02dm", o, int(i))
	} else if dur.Seconds() < 0 {
		return "00m00s"
	} else {
		i := int(m)
		s := dur.Seconds() - float64(i)*60
		return fmt.Sprintf("%02dm%02ds", i, int(s))
	}
}
Пример #9
0
func RenderFixedSizeDuration(dur time.Duration) string {
	h := dur.Hours()
	m := dur.Minutes()
	s := dur.Seconds()
	if h > pbYear*10 {
		y := int(h / pbYear)
		h -= float64(y) * pbYear
		w := h / pbWeek
		return fmt.Sprintf("%02dy%02dw", y, int(w))
	} else if h > pbWeek*10 {
		return fmt.Sprintf("%05dw", int(h/pbWeek))
	} else if h > pbDay*2 {
		d := int(h / pbDay)
		h -= pbDay * float64(d)
		return fmt.Sprintf("%02dd%02dh", d, int(h))
	} else if h > 1 {
		o := int(h)
		i := m - float64(o)*60
		return fmt.Sprintf("%02dh%02dm", o, int(i))
	} else if s > 99 {
		i := int(m)
		o := s - float64(i)*60
		return fmt.Sprintf("%02dm%02ds", i, int(o))
	} else if s < 1 {
		return "00m00s"
	} else {
		ms := (s - float64(int(s))) * 1000
		return fmt.Sprintf("%02ds%03d", int(s), int(ms))
	}
}
Пример #10
0
//	list request all entries from the database, and the displays them.
func list() {
	// get all the entries
	var entries []entry.Entry
	jsonRequest("GET", "/entries", &entries)

	fmt.Println("\nAll Entries")
	fmt.Println("-------------------------------------")

	var total time.Duration

	for _, e := range entries {
		if e.Ended() {
			fmt.Printf("-- %d\t%s\t%s\n", e.Id, e.TimeString(), e.Msg)
		} else {
			fmt.Printf("-- \033[033m%d\t%s\t%s%s\n",
				e.Id,
				e.TimeString(),
				e.Msg,
				" <= active \033[0m")
		}

		total = total + e.Duration()
	}

	fmt.Println("-------------------------------------")
	fmt.Printf("Total - %dh %dm %ds\n\n",
		int(total.Hours()),
		int(total.Minutes())%60,
		int(total.Seconds())%60)
}
Пример #11
0
func dur2secs(dur time.Duration) (secs float32) {
	secs = float32(dur.Hours() * 3600)
	secs += float32(dur.Minutes() * 60)
	secs += float32(dur.Seconds())
	secs += float32(dur.Nanoseconds()) * float32(0.000000001)
	return secs
}
Пример #12
0
func getDurationString(duration time.Duration) string {
	return fmt.Sprintf(
		"%0.2d:%02d:%02d",
		int(duration.Hours()),
		int(duration.Minutes())%60,
		int(duration.Seconds())%60,
	)
}
Пример #13
0
// Formats the given duration as more readable string.
func FormatDuration(dur *time.Duration) string {
	var days int = int(dur.Hours() / 24)
	var hrs int = int(dur.Hours()) % 24
	var mins int = int(dur.Minutes()) % 60
	var secs int = int(dur.Seconds()) % 60

	return fmt.Sprintf("%d days, %d hours, %d minutes and %d seconds", days, hrs, mins, secs)
}
Пример #14
0
func FormatDuration(duration time.Duration) string {
	if duration.Minutes() >= 1 {
		sec := int(duration.Seconds()) % 60
		return fmt.Sprintf("%vm %vs", int(duration.Minutes()), sec)
	} else {
		return fmt.Sprintf("%0.1fs", duration.Seconds())
	}
}
Пример #15
0
func durationToISO8601(d time.Duration) (string, error) {
	if d > time.Hour {
		return "", fmt.Errorf("Duration is too long: %v", d)
	}
	minutes := int(math.Floor(d.Minutes()))
	seconds := int(math.Floor(d.Minutes()-float64(minutes)) * 60.0)
	return fmt.Sprintf("PT%dM%dS", minutes, seconds), nil
}
func getFrequencyString(minWaitTime, maxWaitTime time.Duration) string {
	lowerBoundMinutes := int(minWaitTime.Minutes())
	upperBoundMinutes := int(maxWaitTime.Minutes())

	if lowerBoundMinutes == upperBoundMinutes {
		return fmt.Sprintf("Every %v minutes", lowerBoundMinutes)
	}
	return fmt.Sprintf("Every %v-%v minutes", lowerBoundMinutes, upperBoundMinutes)
}
Пример #17
0
Файл: guess.go Проект: Feh/guess
func deltaNow(t time.Time) (time.Duration, string) {
	var suff string
	var d time.Duration

	now := time.Now()
	if now.Before(t) {
		suff = "ahead"
		d = t.Sub(now)
	} else {
		suff = "ago"
		d = now.Sub(t)
	}
	if d < 1*time.Second {
		return time.Duration(0), "right now"
	}

	interv := []struct {
		d    time.Duration
		desc string
	}{
		{time.Minute, "minute"},
		{time.Hour, "hour"},
		{24 * time.Hour, "day"},
		{7 * 24 * time.Hour, "week"},
	}

	var roughly string
	for _, i := range interv {
		if d < i.d {
			roughly = "within the " + i.desc + ", "
			break
		}
	}

	days, hours, minutes, seconds := 0, int(d.Hours()), int(d.Minutes()), int(d.Seconds())
	minutes -= 60 * hours
	seconds -= 3600*hours + 60*minutes
	for hours >= 24 {
		days += 1
		hours -= 24
	}
	exact := suff
	plural := map[bool]string{true: "s", false: ""}
	if seconds != 0 {
		exact = fmt.Sprintf("%d second%s %s", seconds, plural[seconds > 1], exact)
	}
	if minutes != 0 {
		exact = fmt.Sprintf("%d minute%s %s", minutes, plural[minutes > 1], exact)
	}
	if hours != 0 {
		exact = fmt.Sprintf("%d hour%s %s", hours, plural[hours > 1], exact)
	}
	if days != 0 {
		exact = fmt.Sprintf("%d day%s %s", days, plural[days > 1], exact)
	}
	return d, roughly + exact
}
Пример #18
0
// Writes a duration formatted as hours:minues:seconds,milliseconds
func writeTime(w io.Writer, dur time.Duration) (nbytes int, err error) {
	hoursToPrint := int(math.Floor(dur.Hours()))
	minutesToPrint := int(math.Floor(dur.Minutes() - (time.Duration(hoursToPrint) * time.Hour).Minutes()))
	secondsToPrint := int(math.Floor(dur.Seconds() - (time.Duration(hoursToPrint)*time.Hour + time.Duration(minutesToPrint)*time.Minute).Seconds()))
	millisecondsToPrint := int(math.Floor(float64(dur/time.Millisecond - (time.Duration(hoursToPrint)*time.Hour+time.Duration(minutesToPrint)*time.Minute+time.Duration(secondsToPrint)*time.Second)/time.Millisecond)))

	nbytes, err = fmt.Fprintf(w, "%02d:%02d:%02d,%03d", hoursToPrint, minutesToPrint, secondsToPrint, millisecondsToPrint)
	return
}
Пример #19
0
// durationStr returns duration d in a human readable simple format:
// "2.5 hours", "1 hour", "30 minutes", etc.
func durationStr(d time.Duration) string {
	if d < time.Hour {
		return fmt.Sprintf("%d minutes", int(d.Minutes()))
	}
	v, u := d.Hours(), "hour"
	if v > 1.5 {
		u += "s"
	}
	return fmt.Sprintf("%g %s", d.Hours(), u)
}
Пример #20
0
// DescDuration returns a human-readable string describing the duration.
func DescDuration(d time.Duration) string {
	if d < time.Minute {
		return fmt.Sprintf("%0.1f sec ago", d.Seconds())
	} else if d < time.Hour {
		return fmt.Sprintf("%0.1f min ago", d.Minutes())
	} else if d < time.Hour*24 {
		return fmt.Sprintf("%0.1f hrs ago", d.Hours())
	} else {
		return fmt.Sprintf("%0.1f days ago", d.Hours()/24.0)
	}
}
Пример #21
0
Файл: web.go Проект: kalafut/gmw
func durationToString(d time.Duration) string {
	hours := int(math.Trunc(d.Hours()))
	minutes := int(math.Trunc(d.Minutes())) - 60*hours
	seconds := int(d.Seconds()) % 60

	if hours > 0 {
		return fmt.Sprintf("%d:%d:%02d", hours, minutes, seconds)
	}

	return fmt.Sprintf("%d:%02d", minutes, seconds)
}
Пример #22
0
//////////////////
// Utility
//////////////////
func PrintDuration(t time.Duration) {
	if hours := math.Floor(t.Hours()); hours > 0 {
		fmt.Printf("%vh ", int(hours))
	}
	if minutes := math.Mod(math.Floor(t.Minutes()), 60); minutes > 0 {
		fmt.Printf("%1.0vm ", int(minutes))
	}
	if seconds := math.Mod(t.Seconds(), 60); seconds > 0 {
		fmt.Printf("%2.3vs", seconds)
	}
}
Пример #23
0
func shortHumanDuration(d time.Duration) string {
	if seconds := int(d.Seconds()); seconds < 60 {
		return fmt.Sprintf("%ds", seconds)
	} else if minutes := int(d.Minutes()); minutes < 60 {
		return fmt.Sprintf("%dm", minutes)
	} else if hours := int(d.Hours()); hours < 24 {
		return fmt.Sprintf("%dh", hours)
	} else if hours < 24*364 {
		return fmt.Sprintf("%dd", hours/24)
	}
	return fmt.Sprintf("%dy", int(d.Hours()/24/365))
}
Пример #24
0
func showThrottlerLog(w http.ResponseWriter, m *managerImpl, name string) {
	results, err := m.Log(name)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	logz.StartHTMLTable(w)

	if _, err := io.WriteString(w, logHeaderHTML); err != nil {
		panic(fmt.Sprintf("failed to execute logHeader template: %v", err))
	}
	for _, r := range results {
		// Color based on max(tested state, new state).
		state := r.TestedState
		if stateGreater(r.NewState, state) {
			state = r.NewState
		}
		var colorLevel string
		switch state {
		case stateIncreaseRate:
			colorLevel = "low"
		case stateDecreaseAndGuessRate:
			colorLevel = "medium"
		case stateEmergency:
			colorLevel = "high"
		}
		data := struct {
			result
			ColorLevel string
		}{r, colorLevel}

		if err := logEntryTemplate.Execute(w, data); err != nil {
			panic(fmt.Sprintf("failed to execute logEntry template: %v", err))
		}
	}

	logz.EndHTMLTable(w)

	// Print footer.
	count := len(results)
	var d time.Duration
	if count > 0 {
		d = results[0].Now.Sub(results[count-1].Now)
	}
	if err := logFooterTemplate.Execute(w, map[string]interface{}{
		"Count":    count,
		"TimeSpan": fmt.Sprintf("%.1f", d.Minutes()),
	}); err != nil {
		panic(fmt.Sprintf("failed to execute logFooter template: %v", err))
	}
}
Пример #25
0
Файл: util.go Проект: credli/u
func DurationToString(d time.Duration) string {
	minutes := int(d.Minutes()) % 60
	hours := int(d.Hours())
	days := hours / 24
	hours = hours % 24
	if days > 0 {
		return fmt.Sprintf("%dd %dhr", days, hours)
	}
	if hours > 0 {
		return fmt.Sprintf("%dhr %dm", hours, minutes)
	}
	return fmt.Sprintf("%dm", minutes)
}
Пример #26
0
// timeDurationToHumanizedDuration convert golang time.Duration to a custom more readable humanizedDuration.
func timeDurationToHumanizedDuration(duration time.Duration) humanizedDuration {
	r := humanizedDuration{}
	if duration.Seconds() < 60.0 {
		r.Seconds = int64(duration.Seconds())
		return r
	}
	if duration.Minutes() < 60.0 {
		remainingSeconds := math.Mod(duration.Seconds(), 60)
		r.Seconds = int64(remainingSeconds)
		r.Minutes = int64(duration.Minutes())
		return r
	}
	if duration.Hours() < 24.0 {
		remainingMinutes := math.Mod(duration.Minutes(), 60)
		remainingSeconds := math.Mod(duration.Seconds(), 60)
		r.Seconds = int64(remainingSeconds)
		r.Minutes = int64(remainingMinutes)
		r.Hours = int64(duration.Hours())
		return r
	}
	remainingHours := math.Mod(duration.Hours(), 24)
	remainingMinutes := math.Mod(duration.Minutes(), 60)
	remainingSeconds := math.Mod(duration.Seconds(), 60)
	r.Hours = int64(remainingHours)
	r.Minutes = int64(remainingMinutes)
	r.Seconds = int64(remainingSeconds)
	r.Days = int64(duration.Hours() / 24)
	return r
}
Пример #27
0
func reducePrecision(dur time.Duration, unit time.Duration) float64 {
	switch unit {
	case time.Hour:
		return dur.Hours()
	case time.Minute:
		return dur.Minutes()
	case time.Second:
		return dur.Seconds()
	case time.Nanosecond:
		return float64(dur.Nanoseconds())
	default:
		return float64(dur)
	}
}
Пример #28
0
func formatDuration(d time.Duration) string {
	h := int64(d.Hours())
	if h >= 24 {
		days := h / 24
		return pluralize(days, "day")
	} else if h >= 1 {
		return pluralize(h, "hour")
	}
	m := int64(d.Minutes())
	if m >= 1 {
		return pluralize(m, "minute")
	}
	s := int64(d.Seconds())
	return pluralize(s, "second")
}
Пример #29
0
// HumanDuration returns a human-readable approximation of a duration
// This function is taken from the Docker project, and slightly modified
// to cap units at days.
// (eg. "About a minute", "4 hours ago", etc.)
// (c) 2013 Docker, inc. and the Docker authors (http://docker.io)
func HumanDuration(d time.Duration) string {
	if seconds := int(d.Seconds()); seconds < 1 {
		return "Less than a second"
	} else if seconds < 60 {
		return fmt.Sprintf("%d seconds", seconds)
	} else if minutes := int(d.Minutes()); minutes == 1 {
		return "About a minute"
	} else if minutes < 60 {
		return fmt.Sprintf("%d minutes", minutes)
	} else if hours := int(d.Hours()); hours == 1 {
		return "About an hour"
	} else if hours < 48 {
		return fmt.Sprintf("%d hours", hours)
	}
	return fmt.Sprintf("%d days", int(d.Hours()/24))
}
Пример #30
0
func DurString(dur time.Duration) string {
	minutes := int(dur.Minutes())
	hours := int(dur.Hours())
	days := int(hours / 24)

	if days > 0 {
		return fmt.Sprintf("%d days ago", days)
	}
	if hours > 0 {
		return fmt.Sprintf("%d hours ago", hours)
	}
	if minutes > 0 {
		return fmt.Sprintf("%d minutes ago", minutes)
	}
	return "just now"
}