touch: add --localtime flag to make --timestamp localtime not UTC
Fixes #4067
This commit is contained in:
parent
6c351c15f8
commit
243a868a5b
1 changed files with 26 additions and 3 deletions
|
@ -16,6 +16,7 @@ import (
|
||||||
var (
|
var (
|
||||||
notCreateNewFile bool
|
notCreateNewFile bool
|
||||||
timeAsArgument string
|
timeAsArgument string
|
||||||
|
localTime bool
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultLayout string = "060102"
|
const defaultLayout string = "060102"
|
||||||
|
@ -25,12 +26,29 @@ func init() {
|
||||||
cmd.Root.AddCommand(commandDefinition)
|
cmd.Root.AddCommand(commandDefinition)
|
||||||
cmdFlags := commandDefinition.Flags()
|
cmdFlags := commandDefinition.Flags()
|
||||||
flags.BoolVarP(cmdFlags, ¬CreateNewFile, "no-create", "C", false, "Do not create the file if it does not exist.")
|
flags.BoolVarP(cmdFlags, ¬CreateNewFile, "no-create", "C", false, "Do not create the file if it does not exist.")
|
||||||
flags.StringVarP(cmdFlags, &timeAsArgument, "timestamp", "t", "", "Change the modification times to the specified time instead of the current time of day. The argument is of the form 'YYMMDD' (ex. 17.10.30) or 'YYYY-MM-DDTHH:MM:SS' (ex. 2006-01-02T15:04:05)")
|
flags.StringVarP(cmdFlags, &timeAsArgument, "timestamp", "t", "", "Use specified time instead of the current time of day.")
|
||||||
|
flags.BoolVarP(cmdFlags, &localTime, "localtime", "", false, "Use localtime for timestamp, not UTC.")
|
||||||
}
|
}
|
||||||
|
|
||||||
var commandDefinition = &cobra.Command{
|
var commandDefinition = &cobra.Command{
|
||||||
Use: "touch remote:path",
|
Use: "touch remote:path",
|
||||||
Short: `Create new file or change file modification time.`,
|
Short: `Create new file or change file modification time.`,
|
||||||
|
Long: `
|
||||||
|
Set the modification time on object(s) as specified by remote:path to
|
||||||
|
have the current time.
|
||||||
|
|
||||||
|
If remote:path does not exist then a zero sized object will be created
|
||||||
|
unless the --no-create flag is provided.
|
||||||
|
|
||||||
|
If --timestamp is used then it will set the modification time to that
|
||||||
|
time instead of the current time. Times may be specified as one of:
|
||||||
|
|
||||||
|
- 'YYMMDD' - eg. 17.10.30
|
||||||
|
- 'YYYY-MM-DDTHH:MM:SS' - eg. 2006-01-02T15:04:05
|
||||||
|
|
||||||
|
Note that --timestamp is in UTC if you want local time then add the
|
||||||
|
--localtime flag.
|
||||||
|
`,
|
||||||
Run: func(command *cobra.Command, args []string) {
|
Run: func(command *cobra.Command, args []string) {
|
||||||
cmd.CheckArgs(1, 1, command, args)
|
cmd.CheckArgs(1, 1, command, args)
|
||||||
fsrc, srcFileName := cmd.NewFsDstFile(args)
|
fsrc, srcFileName := cmd.NewFsDstFile(args)
|
||||||
|
@ -41,14 +59,19 @@ var commandDefinition = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
//Touch create new file or change file modification time.
|
//Touch create new file or change file modification time.
|
||||||
func Touch(ctx context.Context, fsrc fs.Fs, srcFileName string) error {
|
func Touch(ctx context.Context, fsrc fs.Fs, srcFileName string) (err error) {
|
||||||
timeAtr := time.Now()
|
timeAtr := time.Now()
|
||||||
if timeAsArgument != "" {
|
if timeAsArgument != "" {
|
||||||
layout := defaultLayout
|
layout := defaultLayout
|
||||||
if len(timeAsArgument) == len(layoutDateWithTime) {
|
if len(timeAsArgument) == len(layoutDateWithTime) {
|
||||||
layout = layoutDateWithTime
|
layout = layoutDateWithTime
|
||||||
}
|
}
|
||||||
timeAtrFromFlags, err := time.Parse(layout, timeAsArgument)
|
var timeAtrFromFlags time.Time
|
||||||
|
if localTime {
|
||||||
|
timeAtrFromFlags, err = time.ParseInLocation(layout, timeAsArgument, time.Local)
|
||||||
|
} else {
|
||||||
|
timeAtrFromFlags, err = time.Parse(layout, timeAsArgument)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to parse date/time argument")
|
return errors.Wrap(err, "failed to parse date/time argument")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue