iyear / tdl

📥 A Telegram toolkit written in Golang
https://docs.iyear.me/tdl
GNU Affero General Public License v3.0
4.55k stars 448 forks source link

[Bug] formatDate does not work with MessageDate in template #517

Open puc9 opened 9 months ago

puc9 commented 9 months ago

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/td@v0.97.0/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

xiaoaxe commented 8 months ago

i encoutered the same error, pls help

sky32 commented 7 months ago

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")
            }
        }
    }
}
0akarma commented 4 months ago

I met with this error.

Error: callback:
    github.com/gotd/td/telegram.(*Client).Run.func3
        github.com/gotd/td@v0.105.0/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"