tdl icon indicating copy to clipboard operation
tdl copied to clipboard

[Bug] formatDate does not work with MessageDate in template

Open puc9 opened this issue 1 year ago • 3 comments

Describe the bug

Using this as part of the template {{ formatDate .MessageDate "2006-01-02_15-04-05" }} fails with:

Error: callback:
    github.com/gotd/td/telegram.(*Client).Run.func3
        github.com/gotd/[email protected]/telegram/connect.go:151
  - iter:
    github.com/iyear/tdl/pkg/downloader.(*Downloader).Download
        github.com/iyear/tdl/pkg/downloader/downloader.go:59
  - execute template:
    github.com/iyear/tdl/app/dl.(*iter).process
        github.com/iyear/tdl/app/dl/iter.go:175
  - template: dl:1:3: executing "dl" at <formatDate .MessageDate "2006-01-02_15-04-05">:
                 error calling formatDate: interface conversion: interface {} is int64, not int

To Reproduce

Run:

tdl.exe .... --template "{{ formatDate .MessageDate "2006-01-02_15-04-05" }}_{{ .MessageID }}_{{ replace .FileName `/` `-` `\` `-` `:` `-` `*` `-` `?` `-` `<` `-` `>` `-` `|` `-` ` ` `-` }}"

Expected behavior

Should successfully format the MessageDate

Version

Version: 0.16.0 Commit: 0e1ce1d Date: 2024-02-15T02:51:19Z

go1.21.7 windows/amd64

Which OS are you running tdl on?

Windows

Additional context

No response

puc9 avatar Feb 25 '24 10:02 puc9

i encoutered the same error, pls help

xiaoaxe avatar Mar 11 '24 15:03 xiaoaxe

pkg/tplfunc/date.go

func FormatDate() Func {
	return func(funcMap template.FuncMap) {
		funcMap["formatDate"] = func(args ...any) string {
			switch len(args) {
			case 0:
				panic("formatDate() requires at least 1 argument")
			case 1:
				switch v := args[0].(type) {
				case int, int64:
					return time.Unix(v.(int64), 0).Format("20060102150405")
				default:
					panic("formatDate() requires an int or int64 as its first argument")
				}
			case 2:
				switch v := args[0].(type) {
				case int, int64:
					return time.Unix(v.(int64), 0).Format(args[1].(string))
				default:
					panic("formatDate() requires an int or int64 as its first argument")
				}
			default:
				panic("formatDate() requires at most 2 arguments")
			}
		}
	}
}

sky32 avatar Apr 16 '24 06:04 sky32

I met with this error.

Error: callback:
    github.com/gotd/td/telegram.(*Client).Run.func3
        github.com/gotd/[email protected]/telegram/connect.go:151
  - parse template:
    github.com/iyear/tdl/app/dl.newIter
        github.com/iyear/tdl/app/dl/iter.go:67
  - template: dl:1: bad number syntax: "2006-01"

0akarma avatar Jul 23 '24 12:07 0akarma