Ejemplo n.º 1
0
func main() {
	tz := "US/Pacific"
	if cattz, ok := os.LookupEnv("CATZ"); ok {
		tz = cattz
	} else {
		stdtz, ok := os.LookupEnv("TZ")
		if ok {
			tz = stdtz
		}
	}
	srcTZ := flag.String("srctz", "UTC", "input time zone")
	destTZ := flag.String("outtz", tz, "output time zone (defaults to $CATZ or $TZ env if available)")
	debug := flag.Bool("d", false, "enable debug logging")
	timeFormat := flag.String("t", "%Y-%m-%d %H", "strftime format")
	first := flag.Bool("first", false, "only replace first timestamp match per line")
	flag.Parse()

	sourceLocation, err := time.LoadLocation(*srcTZ)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR input TZ %s not known\n", *srcTZ)
		os.Exit(1)
	}

	destLocation, err := time.LoadLocation(*destTZ)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR output TZ %s not known\n", *destTZ)
		os.Exit(1)
	}
	c := &controller{
		srcLoc:  sourceLocation,
		destLoc: destLocation,
		buf:     &bytes.Buffer{},
		debug:   *debug,
	}
	// verify we know how to handle the time format
	c.format, err = strftime.New(*timeFormat)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR time format [%s] had problem converting to go format%s\n", *timeFormat, err)
		os.Exit(1)
	}
	err = c.strftimeToRE(*timeFormat)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR time format [%s] had problem to regex %s\n", *timeFormat, err)
		os.Exit(1)
	}
	// only replace first timestamp occurance, or replace all
	if *first {
		c.matchLimit = 1
	} else {
		c.matchLimit = -1
	}

	os.Exit(c.execute())
}
Ejemplo n.º 2
0
// testReplace is the actual test framework
// TODO: validate the replacement values are correct versus just not returning an error
func testReplace(t *testing.T, name string, strftimeS string, origtime string) {
	var err error
	testController.format, err = strftime.New(strftimeS)
	if err != nil {
		t.Fatalf("%s failure converting %s to go time.Parse format err=%s\n", name, strftimeS, err)
		return
	}
	err = testController.strfimeToRE(strftimeS)
	if err != nil {
		t.Fatalf("%s failure converting %s to regular expression err=%s\n", name, strftimeS, err)
		return
	}
	replaced, err := testController.replaceTime(origtime)
	if err != nil {
		t.Fatalf("%s replaced returned err %s\n", name, err)
		return
	}
	t.Logf("name = %s input = %s replaced=%s\n", name, origtime, replaced)
}