Get min and max dates
cantorp at gmail.com
cantorp at gmail.com
Thu Dec 8 11:30:14 EST 2016
Am Donnerstag, 8. Dezember 2016 14:47:31 UTC+1 schrieb DFS:
> On 12/8/2016 12:16 AM, Steven D'Aprano wrote:
> > On Thursday 08 December 2016 03:15, DFS wrote:
> >
> >> dts= ['10-Mar-1998',
> >> '20-Aug-1997',
> >> '06-Sep-2009',
> >> '23-Jan-2010',
> >> '12-Feb-2010',
> >> '05-Nov-2010',
> >> '03-Sep-2009',
> >> '07-Nov-2014',
> >> '08-Mar-2013']
> >>
> >> Of course, the naive:
> >> min(dates) = '03-Sep-2009'
> >> max(dates) = '23-Jan-2010'
> >> is wrong.
> >>
> >> Not wanting to use any date parsing libraries, I came up with:
> > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >
> > That's where you have gone wrong. By not using a date parsing library which
> > works and has been tested, you have to write your own dodgy and possibly buggy
> > parsing routine yourself.
> >
> > Why reinvent the wheel?
>
>
> Because the "wheel" is a pain in the ass.
>
Why? And why do you use this wording?
> --------------------------------------------------------------
> import time
> dts=['10-Mar-1908','20-Aug-1937','06-Sep-1969','23-Jan-1952']
> def date_to_seconds(string):
> return time.mktime(time.strptime(string, '%d-%b-%Y'))
> print min(dts, key=date_to_seconds)
> --------------------------------------------------------------
>
> OverflowError: mktime argument out of range
>
>
(snip)
>
> I like that flexibility, but mktime is apparently useless for dates
> prior to 'the epoch'.
With a little more experience in Python programming you should have discovered that time.mktime is not even required to do your calculations.
Please remove time.mktime from the function date_to_seconds and rename the function to date_to_timestruct.
-- Paolo
More information about the Python-list
mailing list