Anyone have success w/STRPTIME???
Mauro Cicognini
mcicogni at siosistemi.it
Mon Oct 11 05:56:25 EDT 1999
Benjamin Schollnick wrote:
> On Fri, 08 Oct 1999 15:51:48 +0200, Mauro Cicognini wrote:
>
> >Ben, strptime() is not supported under Windows. It does say so in the
> >docs. :(
>
> I must of missed that...
>
> >If you're interested, I've written a str2time() which I'm pretty much
> >satisfacted with, that does some nice RE processing and a couple crude
> >hacks to get what I want in a Python format. It extracts a date from
> >many general-format strings; if you need to, changing the RE will allow
> >you to parse even more formats. I can send you the source if you wish.
> >
> >Also, a coworker of mine has found a strptime() written in Python (which
> >will work on Windows, too) that does all that the standard strptime()
> >does, and more.
>
> I'd certainly appreciate it if you could send me yours. If your coworker
> wouldn't mind, I certainly would like to see the strptime replacement,
> so that I can keep the program "general" python compliant.
>
> - Benjamin
My source is attached.
The stuff to parse month names is really crude, a better way would be to use
python's 'local' module but I have no time now. I have translated all the
comments, I hope I haven't broken something in the attempt, in case please
fix it, it shouldn't be too difficult.
I noticed on the newsgroup appeared the link to the pure Python strptime(),
so it's best to follow that.
Mauro
-------------- next part --------------
def str2time(d):
"""
This function attempts to convert a date expressed in various manners
in UNIX format (no. of seconds from 1 jan 1970 00:00:00 GMT).
The general format must contain at least day and month, as follows:
Day: 1 or 2 digits.
Month: 1 or 2 digits or at least 3 letters of the name.
Year: 2 to 4 digits. Optional: if missing the current year is assumed.
Elements in the date can be separated by '-', '/', '.' or
by an indefinite number of blanks.
A time figure may be present (even without an explicit year figure).
If present, it must be separated for the date by at least a blank.
Hours: 1 or 2 digits.
Minutes: 2 digits.
Seconds: 2 digits. Optional.
Elements inside time can be separated by '.' or ':'.
Special formats:
- "yesterday": obviously returns yesterday's date
- "today": obviously returns today's date
- "tomorrow": obviously returns tomorrow's date
- A number preceded by '+' or '-': returns the date of the day that comes
so many days before (if preceded by '-') or after (if preceded by '-')
today, e.g. '+5' means 5 days in the future; '-2' is the day before yesterday;
'+2' is the day after tomorrow; etc. For generality, '0' (the digit zero) = today.
The RE that does most of the work is hereafter commented.
( # start date section
(?P<dy>\d{1,2}) # dy=day, 1 or 2 digits
([-/.]|\s+) # separators: '-', '/', '.' or blanks
(?P<mo>\d{1,2}|[a-zA-Z]{3,}) # mo=month, 1 or 2 digits, or an alpha string
( # start year section, optional
([-/.]|\s+) # separators: '-', '/', '.' or blanks
(?P<yr>\d{2,4} # yr=year, 2 to 4 digits
(?=\s+|$)) # "lookahead", blanks or end
)? # end year section
) # end date section
( # start hour section, optional
\s+ # separators: one or more blanks
(?P<hr>\d{1,2}) # hr=hours, 1 or 2 digits
[.:] # separators: '.' or ':'
(?P<mn>\d{2}) # mn=minutes, 2 digits
( # start seconds section, optional
[.:] # separators: '.' o ':'
(?P<sc>\d{2}) # sc=seconds, 2 digits
)? # end seconds section
)? # end time section
"""
if d == '':
return (None)
if d == 'today' or d == '0':
return (time.time())
elif d == 'yesterday':
return (time.time() - 86400)
elif d == 'tomorrow':
return (time.time() + 86400)
elif d[0] == '+' or d[0] == '-':
try:
nd = int(d)
except ValueError:
return (None)
return (time.time() + 86400*nd)
else:
p = re.compile("((?P<dy>\d{1,2})([-/.]|\s+)(?P<mo>\d{1,2}|[a-zA-Z]{3,})" # giorno e mese
"(([-/.]|\s+)(?P<yr>\d{2,4}(?=\s+|$)))?)" # anno opzionale
"(\s+(?P<hr>\d{1,2})[.:](?P<mn>\d{2})([.:](?P<sc>\d{2}))?)?") # ore, minuti e secondi (opz.)
m = p.match(d)
if m:
giorno,meseboh,anno,ore,min,sec = m.group("dy","mo","yr","hr","mn","sc")
if not anno:
anno = time.localtime(time.time())[0]
if not ore: ore = 0
if not min: min = 0
if not sec: sec = 0
try:
mese = int(meseboh)
except ValueError:
try:
mese = {'jan':1,'feb':2,'mar':3,'apr':4,'may':5,'jun':6,'jul':7,'aug':8,'sep':9,'oct':10,'nov':11,'dec':12}[meseboh[:3]]
except KeyError:
return (None)
else:
return (None)
try:
return (time.mktime(int(anno),int(mese),int(giorno), int(ore),int(min),int(sec), 0,0,-1))
except ValueError:
return (None)
except OverflowError:
return (None)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mcicogni.vcf
Type: text/x-vcard
Size: 566 bytes
Desc: Card for Mauro Cicognini
URL: <http://mail.python.org/pipermail/python-list/attachments/19991011/f6ea4fcc/attachment.vcf>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 2184 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/19991011/f6ea4fcc/attachment.bin>
More information about the Python-list
mailing list