Beispiel #1
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)
}
Beispiel #2
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)
}
Beispiel #3
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))
	}
}
Beispiel #4
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
}
Beispiel #5
0
func FriendlyDuration(d time.Duration) string {
	switch {
	case d.Hours() >= 24:
		days := int(d.Hours() / 24)
		hours := int(d.Hours()) - days*24
		return joinpair(plural(days, "day"), plural(hours, "hour"))
	case d.Hours() >= 1:
		hours := int(d.Hours())
		mins := int(int(d.Minutes()) - 60*hours)
		return joinpair(plural(hours, "hour"), plural(mins, "minute"))
	case d.Minutes() >= 1:
		mins := int(d.Minutes())
		secs := int(int(d.Seconds()) - 60*mins)
		return joinpair(plural(mins, "minute"), plural(secs, "second"))
	case d.Seconds() >= 1:
		secs := int(d.Seconds())
		return plural(secs, "second")
	case d.Nanoseconds() >= 1000:
		ms := int(d.Seconds() * 1000)
		return plural(ms, "millisecond")
	case d.Nanoseconds() > 0:
		ns := d.Nanoseconds()
		return plural(int(ns), "nanosecond")
	}
	return "0 seconds"
}
Beispiel #6
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))
	}
}
Beispiel #7
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)
}
Beispiel #8
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
}
Beispiel #9
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)
}
Beispiel #10
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,
	)
}
Beispiel #11
0
func fmtDuration(d time.Duration) string {
	if d.Hours() >= 1 {
		return fmt.Sprintf("%vh%vm", int(d.Hours()), int(d.Minutes())%60)
	} else if d.Minutes() >= 1 {
		return fmt.Sprintf("%vm%vs", int(d.Minutes()), int(d.Seconds())%60)
	} else {
		return fmt.Sprintf("%vs", int(d.Seconds()))
	}
}
Beispiel #12
0
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
}
Beispiel #13
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
}
Beispiel #14
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)
}
Beispiel #15
0
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)
}
Beispiel #16
0
// This is the calculation of the exponential smoothing factor. It tries to
// make sure that if we get lots of fast merges we don't race the 'daily'
// avg really high really fast. But more importantly it means that if merges
// start going slowly the 'daily' average will get pulled down a lot by one
// slow merge instead of requiring numerous merges to get pulled down
func getSmoothFactor(dur time.Duration) float64 {
	hours := dur.Hours()
	smooth := .155*math.Log(hours) + .422
	if smooth < .1 {
		return .1
	}
	if smooth > .999 {
		return .999
	}
	return smooth
}
Beispiel #17
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)
	}
}
Beispiel #18
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)
	}
}
func (d internalDist) print(device, name string, duration time.Duration) DurationStats {
	ds := DurationStats{
		Name:          name,
		NumRate:       float64(d.Num) / duration.Hours(),
		DurationRate:  d.TotalDuration.Seconds() / duration.Hours(),
		Num:           d.Num,
		TotalDuration: d.TotalDuration,
		MaxDuration:   d.MaxDuration,
	}
	return ds
}
Beispiel #20
0
func main() {
	fmt.Println("xxxxxxx", int64(int64(time.Now().Nanosecond())/time.Millisecond.Nanoseconds()*time.Millisecond.Nanoseconds()), int(time.Millisecond), int(time.Millisecond.Nanoseconds()))
	return
	tt := &Test{}
	fmt.Println(tt.offset)
	fmt.Println(tt.Add())
	fmt.Println(tt.offset)
	fmt.Println(unibase.Atoi("192.158.68.2"))
	d, e := time.ParseDuration("1s")
	fmt.Println(d, e)
	fmt.Println("xxxxxxx", int(time.Millisecond), int(time.Millisecond.Nanoseconds()))
	year, month, day := time.Now().Date()
	fmt.Println(year, month, day)
	ip := net.ParseIP("1.1.1.11").To4()
	fmt.Println(uint32(ip[0])<<24 + uint32(ip[1])<<16)
	loc, _ := time.LoadLocation("")
	fmt.Println(time.Now().Unix(), time.Now().Local().Unix(), time.Now().Second(), time.Now().Location().String(), loc.String())
	pt, err := ParseTime("2012-12-12 12:12:12")
	fmt.Println(pt.Unix(), err)
	now := time.Now()
	_, offset := now.Zone()
	fmt.Println(int(time.Duration(offset)))
	begin := time.Duration(offset) * time.Second
	zonenow := now.Add(-begin)
	fmt.Println(now.Unix(), zonenow.Unix())
	fmt.Println("GOTRACEBACK:", os.Getenv("RACEBACK"))
	//now := time.Now()
	//time.Sleep(1000000000)
	fmt.Println(time.Now().UnixNano(), time.Now().Unix())
	t := time.Date(2009, time.December, 30, 13, 13, 13, 13, time.UTC)
	fmt.Printf("%s,%d\n", t.Local(), time.Millisecond)
	seconds := 10
	var dur time.Duration
	fmt.Println(dur.Hours())
	fmt.Printf("%d\n", time.Duration(seconds)*time.Second)
	//zone, sec := now.Zone()
	//fmt.Println(now.Unix(), now.Sub(1111), zone, sec)
	go func() {
		for {
			select {
			case <-time.After(2 * time.Second):
				fmt.Println("2time.After:", time.Now().Unix())
			case <-time.After(1 * time.Second):
				fmt.Println("1time.After:", time.Now().Unix())
			}
		}
	}()
	timer := time.AfterFunc(time.Second*2, timefunc)
	defer timer.Stop()
	for i := 0; i < 30; i++ {
		time.Sleep(time.Second * 2)
		fmt.Println("sleep:", time.Now().Unix())
	}
}
// sumWakelockInfo sums the Count and Duration fields of the given WakelockInfos.
// CountPerHour and SecondsPerHour will be filled in if r is not 0.
func sumWakelockInfo(d []*checkinparse.WakelockInfo, r time.Duration) ActivityData {
	wd := ActivityData{}
	for _, w := range d {
		wd.Count += w.Count
		wd.Duration += w.Duration
	}
	if r != 0 {
		wd.CountPerHour = wd.Count / float32(r.Hours())
		wd.SecondsPerHr = float32(wd.Duration.Seconds()) / float32(r.Hours())
	}
	return wd
}
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))
}
Beispiel #23
0
func TestDuration(t *testing.T) {
	var t1 Quantity
	t1 = Q(1.5, "d")
	var t2 time.Duration
	t2, err := Duration(t1)
	if err != nil {
		t.Error(err)
	}
	if t2.Hours() != 36 {
		t.Error("expected:", 36, "actual:", t2.Hours())
	}
}
Beispiel #24
0
Datei: util.go Projekt: 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)
}
// 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
}
Beispiel #26
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)
	}
}
Beispiel #27
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")
}
Beispiel #28
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))
}
Beispiel #29
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"
}
Beispiel #30
0
// there is a better way to do this
func formatDuration(d time.Duration) (str string) {
	hours := int(d.Hours())
	d -= (time.Duration(hours) * time.Hour)

	minutes := int(d.Minutes())
	d -= (time.Duration(minutes) * time.Minute)

	seconds := int(d.Seconds())

	if hours == 0 {
		str += fmt.Sprintf("%d:%02d", minutes, seconds)
	} else {
		str += fmt.Sprintf("%d:%02d:%02d", hours, minutes, seconds)
	}
	return
}