func TimestampFormatter()

in pkg/generator/gotext/gotext.go [44:96]


func TimestampFormatter(format, whence string) string {
	now := time.Now()

	dur,err := time.ParseDuration(whence)
	if err != nil {
		fmt.Println("failed to parse [", whence, "] with error::", err)
	} else {
		// now choose a random duration within this value
		trunc := int(dur.Round(time.Second).Seconds())
		seconds := 0
		if trunc > 0 {
			seconds = rand.Intn(trunc)
		}
		newdur, err := time.ParseDuration(fmt.Sprintf("-%ds", seconds))
		if err != nil {
			// ignore
		} else {
			dur = newdur
		}
		now = now.Add(dur)
	}

	// this format is seconds.[1-9]
	if strings.HasPrefix(format, "seconds") {
		secs := now.Unix()

		returnVal := fmt.Sprintf("%d", secs)
		result := strings.Split(format, ".")
		if len(result) > 1 {
			// generate a random number of nanos
			nanos := rand.Intn(1_000_000_000)
			partialsString := fmt.Sprintf("%09d", nanos)

			// figure out what precision to output
			precision := 6
			if prec, err := strconv.Atoi(result[1]); err == nil {
				switch prec {
				case 0:
					return fmt.Sprintf("%d", secs)
				case 1, 2, 3, 4, 5, 6, 7, 8, 9:
					precision = prec
				default:
					precision = 9
				}
			}
			returnVal = fmt.Sprintf("%d.%s", secs, partialsString[0:precision])
		}
		// return early
		return returnVal
	}

	return now.Format(format)
}