| 1 | --- |
| 2 | tags: |
| 3 | - v2 |
| 4 | search: |
| 5 | boost: 2 |
| 6 | --- |
| 7 | |
| 8 | Using the timestamp flag is simple. Please refer to |
| 9 | [`time.Parse`](https://golang.org/pkg/time/#example_Parse) to get possible |
| 10 | formats. |
| 11 | |
| 12 | <!-- { |
| 13 | "args": ["--meeting", "2019-08-12T15:04:05"], |
| 14 | "output": "2019\\-08\\-12 15\\:04\\:05 \\+0000 UTC" |
| 15 | } --> |
| 16 | ```go |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "log" |
| 22 | "os" |
| 23 | |
| 24 | "github.com/urfave/cli/v2" |
| 25 | ) |
| 26 | |
| 27 | func main() { |
| 28 | app := &cli.App{ |
| 29 | Flags: []cli.Flag{ |
| 30 | &cli.TimestampFlag{Name: "meeting", Layout: "2006-01-02T15:04:05"}, |
| 31 | }, |
| 32 | Action: func(cCtx *cli.Context) error { |
| 33 | fmt.Printf("%s", cCtx.Timestamp("meeting").String()) |
| 34 | return nil |
| 35 | }, |
| 36 | } |
| 37 | |
| 38 | if err := app.Run(os.Args); err != nil { |
| 39 | log.Fatal(err) |
| 40 | } |
| 41 | } |
| 42 | ``` |
| 43 | |
| 44 | In this example the flag could be used like this: |
| 45 | |
| 46 | ```sh-session |
| 47 | $ myapp --meeting 2019-08-12T15:04:05 |
| 48 | ``` |
| 49 | |
| 50 | When the layout doesn't contain timezones, timestamp will render with UTC. To |
| 51 | change behavior, a default timezone can be provided with flag definition: |
| 52 | |
| 53 | ```go |
| 54 | app := &cli.App{ |
| 55 | Flags: []cli.Flag{ |
| 56 | &cli.TimestampFlag{Name: "meeting", Layout: "2006-01-02T15:04:05", Timezone: time.Local}, |
| 57 | }, |
| 58 | } |
| 59 | ``` |
| 60 | |
| 61 | (time.Local contains the system's local time zone.) |
| 62 | |
| 63 | Side note: quotes may be necessary around the date depending on your layout (if |
| 64 | you have spaces for instance) |