Difference between two times (working ugly code, needs polish)

Iain King iainking at gmail.com
Wed Sep 12 10:59:49 CEST 2007


On Sep 12, 1:31 am, "Shawn Milochik" <Sh... at Milochik.com> wrote:
> > I suppose really oneDay should be a global (i.e. outside the function
> > definition). Apart from that it would be hard to improve on: obvious,
> > easy to read, in short - pythonic.
>
> > Are you concerned about daylight savings? That could certainly introduce
> > a whole new level of complexity into the problem. Let's hope not ...
>
> I'm not concerned with DST; this is a script which checks my Ebay
> auctions (I have some things for sale), and sends me e-mail whenever
> someone bids. It's run by cron every half hour -- it keeps me from
> compulsively checking my auctions. ^_^
>
> In any case, DST isn't an issue because the same machine generates
> both timestamps, and all I use it for is to stop displaying auctions
> after they are 10 days old, so I don't get all my old crap filling up
> the alert e-mail or skewing the total dollar amount for all active
> auctions.
>
> Thanks.
> Shawn

Just to be picky - your function returns the number of days between
two dates, but it's called isOld, which looks like it should return a
boolean.  i.e.  it looks like it would be used as:

if not isOld(auctionDate, currentTime):
    checkForBid()

rather than how I assume it is used:

if isOld(auctionDate, currentTime) <= 10:
    checkForBid()

I'd call it daysDiff or something similar, or make it more specific so
that it works like the first block of code above:

ONEDAY = 60*60*24
OLDNESS_THRESHOLD = 10

def isOld(lastUpdate, runTimeStamp):
    lastUpdate = time.mktime(time.strptime(lastUpdate, "%Y-%m-%d_%H:
%M"))
    runTimeStamp = time.mktime(time.strptime(runTimeStamp, "%Y-%m-%d_
%H:%M"))
    return (runTimeStamp - lastUpdate) / ONEDAY  >=  OLDNESS_THRESHOLD

if not isOld(auctionDate, currentTime):
    checkForBid()

Iain




More information about the Python-list mailing list