FTP Date Format Function

samuraisam samuraiblog at gmail.com
Thu Jun 14 19:25:17 CEST 2007

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"
        ).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. :)

More information about the Python-list mailing list