2020-10-21 16:13:21 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultFormat = "2006-01-02 15:04:05"
|
|
|
|
filenameFormat = "20060102-150405"
|
|
|
|
)
|
|
|
|
|
|
|
|
type flags struct {
|
|
|
|
utc bool
|
|
|
|
filename bool
|
2020-10-21 20:02:14 +02:00
|
|
|
nano bool
|
2020-10-21 16:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func stamp(format string, f *flags) string {
|
|
|
|
t := time.Now()
|
|
|
|
if f.utc {
|
|
|
|
t = t.UTC()
|
|
|
|
}
|
|
|
|
return t.Format(format)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFormat(f *flags) string {
|
|
|
|
var format string
|
|
|
|
if f.filename {
|
|
|
|
format = filenameFormat
|
|
|
|
} else {
|
|
|
|
format = defaultFormat
|
|
|
|
}
|
2020-10-21 20:02:14 +02:00
|
|
|
if f.nano {
|
2020-10-21 16:13:21 +02:00
|
|
|
format += ".000000000"
|
|
|
|
}
|
|
|
|
return format
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
f := flags{}
|
|
|
|
flag.BoolVar(&f.utc, "utc", false, "use UTC instead of local time")
|
|
|
|
flag.BoolVar(&f.filename, "filename", false, "use filename friendly format")
|
2020-10-21 20:02:14 +02:00
|
|
|
flag.BoolVar(&f.nano, "nano", false, "include nanoseconds")
|
2020-10-21 16:13:21 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
format := getFormat(&f)
|
|
|
|
|
|
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
fmt.Println(stamp(format, &f), scanner.Text())
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println(stamp(format, &f))
|
|
|
|
}
|
|
|
|
}
|