FTP Date Format Function

billiejoex gnewsg at gmail.com
Fri Jun 15 07:53:47 CEST 2007


On 14 Giu, 19:25, samuraisam <samuraib... at gmail.com> wrote:
> FTP LST/LIST/NLST date field formatting function for all those seekers
> out there...
>
> import time
> import datetime
>
> def ftpdateformat(value):
>     """Formats dates from most FTP servers"""
>     if ":" in value: # within 6 months
>         return datetime.datetime(
>             *time.strptime( # have to guess this calculation
>                 "%s %s" % (value, datetime.datetime.now().year),
>                 "%b %d %H:%M %Y"
>             )[0:5]
>         ).strftime("%B %d, %Y %H:%M")
>     else: # before six months
>         return datetime.datetime(
>             *time.strptime(value, "%b %d %Y")[0:5]
>         ).strftime("%B %d, %Y")
>
> I'm not sure if there is a proper algorithm for deciding on a proper
> year within the last 6 months as it isn't given by most FTP servers.
> I'd love to amend the function with the correct solution. :)

I didn't well understand your question, anyway...

- FTP got no LST command. What's that?
- NLST should return filenames only: returned output doesn't contain
file sizes, last modification time values or whatever.
- RFC959 gives no specifications about *how* LIST command output
should be formatted. Depending on the type of server you're talking to
you could find unix/"ls -l"-like format outputs, DOS-like ones or
something completely different and your code does not cover all of
them. Take a look at: http://effbot.org/downloads/#ftpparse




More information about the Python-list mailing list