lsf: add support for unix and unixnano time formats

This commit is contained in:
Motte
2025-08-28 17:28:49 +02:00
committed by GitHub
parent c8912eb6a0
commit 89a8ea7a91
4 changed files with 29 additions and 8 deletions

View File

@@ -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`" + `

View File

@@ -2219,15 +2219,26 @@ 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)
})
}
}
// AddSize adds file's size to output
func (l *ListFormat) AddSize() {

View File

@@ -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()

View File

@@ -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