From 89a8ea7a91b8d631ce3ac42bbf701e0c5b344ff8 Mon Sep 17 00:00:00 2001 From: Motte <37443982+dmotte@users.noreply.github.com> Date: Thu, 28 Aug 2025 17:28:49 +0200 Subject: [PATCH] lsf: add support for unix and unixnano time formats --- cmd/lsf/lsf.go | 4 +++- fs/operations/operations.go | 23 ++++++++++++++----- fs/operations/operations_test.go | 8 +++++++ .../operationsflags/operationsflags.go | 2 +- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/cmd/lsf/lsf.go b/cmd/lsf/lsf.go index 3c704d27e..38e79d62a 100644 --- a/cmd/lsf/lsf.go +++ b/cmd/lsf/lsf.go @@ -33,7 +33,7 @@ func init() { cmd.Root.AddCommand(commandDefinition) cmdFlags := commandDefinition.Flags() flags.StringVarP(cmdFlags, &format, "format", "F", "p", "Output format - see help for details", "") - flags.StringVarP(cmdFlags, &timeFormat, "time-format", "t", "", "Specify a custom time format, or 'max' for max precision supported by remote (default: 2006-01-02 15:04:05)", "") + flags.StringVarP(cmdFlags, &timeFormat, "time-format", "t", "", "Specify a custom time format - see docs for details (default: 2006-01-02 15:04:05)", "") flags.StringVarP(cmdFlags, &separator, "separator", "s", ";", "Separator for the items in the format", "") flags.BoolVarP(cmdFlags, &dirSlash, "dir-slash", "d", true, "Append a slash to directory names", "") flags.FVarP(cmdFlags, &hashType, "hash", "", "Use this hash when `h` is used in the format MD5|SHA-1|DropboxHash", "") @@ -169,6 +169,8 @@ rclone lsf remote:path --format pt --time-format '2006-01-02T15:04:05.999999999Z rclone lsf remote:path --format pt --time-format RFC3339 rclone lsf remote:path --format pt --time-format DateOnly rclone lsf remote:path --format pt --time-format max +rclone lsf remote:path --format pt --time-format unix +rclone lsf remote:path --format pt --time-format unixnano ` + "```" + ` ` + "`--time-format max`" + ` will automatically truncate ` + "`2006-01-02 15:04:05.000000000`" + ` diff --git a/fs/operations/operations.go b/fs/operations/operations.go index e07b79be3..a10ad41b2 100644 --- a/fs/operations/operations.go +++ b/fs/operations/operations.go @@ -2219,14 +2219,25 @@ func (l *ListFormat) SetOutput(output []func(entry *ListJSONItem) string) { // AddModTime adds file's Mod Time to output func (l *ListFormat) AddModTime(timeFormat string) { - if timeFormat == "" { - timeFormat = "2006-01-02 15:04:05" - } else { + switch timeFormat { + case "": + l.AppendOutput(func(entry *ListJSONItem) string { + return entry.ModTime.When.Local().Format("2006-01-02 15:04:05") + }) + case "unix": + l.AppendOutput(func(entry *ListJSONItem) string { + return fmt.Sprint(entry.ModTime.When.Unix()) + }) + case "unixnano": + l.AppendOutput(func(entry *ListJSONItem) string { + return fmt.Sprint(entry.ModTime.When.UnixNano()) + }) + default: timeFormat = transform.TimeFormat(timeFormat) + l.AppendOutput(func(entry *ListJSONItem) string { + return entry.ModTime.When.Local().Format(timeFormat) + }) } - l.AppendOutput(func(entry *ListJSONItem) string { - return entry.ModTime.When.Local().Format(timeFormat) - }) } // AddSize adds file's size to output diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.go index 8e6331156..cb68b05ca 100644 --- a/fs/operations/operations_test.go +++ b/fs/operations/operations_test.go @@ -1278,6 +1278,14 @@ func TestListFormat(t *testing.T) { list.AddModTime("") assert.Equal(t, t1.Local().Format("2006-01-02 15:04:05"), list.Format(item0)) + list.SetOutput(nil) + list.AddModTime("unix") + assert.Equal(t, fmt.Sprint(t1.Local().Unix()), list.Format(item0)) + + list.SetOutput(nil) + list.AddModTime("unixnano") + assert.Equal(t, fmt.Sprint(t1.Local().UnixNano()), list.Format(item0)) + list.SetOutput(nil) list.SetSeparator("|") list.AddID() diff --git a/fs/operations/operationsflags/operationsflags.go b/fs/operations/operationsflags/operationsflags.go index 8b54fd442..054ed96bc 100644 --- a/fs/operations/operationsflags/operationsflags.go +++ b/fs/operations/operationsflags/operationsflags.go @@ -62,7 +62,7 @@ func AddLoggerFlags(cmdFlags *pflag.FlagSet, opt *operations.LoggerOpt, flagsOpt // lsf flags for destAfter flags.StringVarP(cmdFlags, &opt.Format, "format", "F", "p", "Output format - see lsf help for details", "Sync") - flags.StringVarP(cmdFlags, &opt.TimeFormat, "timeformat", "t", "", "Specify a custom time format, or 'max' for max precision supported by remote (default: 2006-01-02 15:04:05)", "") + flags.StringVarP(cmdFlags, &opt.TimeFormat, "timeformat", "t", "", "Specify a custom time format - see docs for details (default: 2006-01-02 15:04:05)", "") flags.StringVarP(cmdFlags, &opt.Separator, "separator", "s", ";", "Separator for the items in the format", "Sync") flags.BoolVarP(cmdFlags, &opt.DirSlash, "dir-slash", "d", true, "Append a slash to directory names", "Sync") opt.HashType = hash.MD5