You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1.2 KiB

tags search
v2
boost
2

Using the timestamp flag is simple. Please refer to time.Parse to get possible formats.

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/urfave/cli/v2"
)

func main() {
	app := &cli.App{
		Flags: []cli.Flag{
			&cli.TimestampFlag{Name: "meeting", Layout: "2006-01-02T15:04:05"},
		},
		Action: func(cCtx *cli.Context) error {
			fmt.Printf("%s", cCtx.Timestamp("meeting").String())
			return nil
		},
	}

	if err := app.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}

In this example the flag could be used like this:

$ myapp --meeting 2019-08-12T15:04:05

When the layout doesn't contain timezones, timestamp will render with UTC. To change behavior, a default timezone can be provided with flag definition:

app := &cli.App{
	Flags: []cli.Flag{
		&cli.TimestampFlag{Name: "meeting", Layout: "2006-01-02T15:04:05", Timezone: time.Local},
	},
}

(time.Local contains the system's local time zone.)

Side note: quotes may be necessary around the date depending on your layout (if you have spaces for instance)