FTP Date Format Function

S

samuraisam

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

billiejoex

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
 
S

samuraisam

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

Ah, well if there isn't LST and if NLST doesn't return dates that's
OK, I wasn't sure at the time I authored the post. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top