touch: add --localtime flag to make --timestamp localtime not UTC

Fixes #4067
This commit is contained in:
Nick Craig-Wood 2020-03-17 12:28:23 +00:00
parent 6c351c15f8
commit 243a868a5b

View File

@ -16,6 +16,7 @@ import (
var (
notCreateNewFile bool
timeAsArgument string
localTime bool
)
const defaultLayout string = "060102"
@ -25,12 +26,29 @@ func init() {
cmd.Root.AddCommand(commandDefinition)
cmdFlags := commandDefinition.Flags()
flags.BoolVarP(cmdFlags, &notCreateNewFile, "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{
Use: "touch remote:path",
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) {
cmd.CheckArgs(1, 1, command, args)
fsrc, srcFileName := cmd.NewFsDstFile(args)
@ -41,14 +59,19 @@ var commandDefinition = &cobra.Command{
}
//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()
if timeAsArgument != "" {
layout := defaultLayout
if len(timeAsArgument) == len(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 {
return errors.Wrap(err, "failed to parse date/time argument")
}