Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


Shawn at Milochik

Sep 11, 2007, 1:46 PM

Post #1 of 8 (80 views)
Permalink
Difference between two times (working ugly code, needs polish)

I have done what I wanted, but I think there must be a much better way.

Given two timestamps in the following format, I just want to figure
out how far apart they are (in days, seconds, whatever).

Format:

YYYY-MM-DD_MM:SS

Example:
2007-09-11_16:41


It seems to me that to do what I want, I need to convert a string into
a number (time.mktime()), such as this: 1189543487.0

Once I have that, I can just subtract one from the other and do
whatever I want. The ugly part is converting something like
2007-09-11_16:41 to the numeric equivalent.

Below is my code. Any suggestions?

Thanks in advance.


Here is what I have (works):

#runTimeStamp is the current time, set when the script begins execution
def recAge(lastUpdate, runTimeStamp):

import re

oneDay = 60 * 60 * 24

lastUpdate = re.sub(r'\D+', ',', lastUpdate)
lastUpdate = lastUpdate + ",0,0,0,0"
lastUpdate = [int(x) for x in lastUpdate.split(',')]
lastUpdate = time.mktime(tuple(lastUpdate))

runTimeStamp = re.sub(r'\D+', ',', runTimeStamp)
runTimeStamp = runTimeStamp + ",0,0,0,0"
runTimeStamp = [int(x) for x in runTimeStamp.split(',')]
runTimeStamp = time.mktime(tuple(runTimeStamp))

return (runTimeStamp - lastUpdate) / oneDay
--
http://mail.python.org/mailman/listinfo/python-list


bdesth.quelquechose at free

Sep 9, 2007, 8:15 AM

Post #2 of 8 (76 views)
Permalink
Re: Difference between two times (working ugly code, needs polish) [In reply to]

Shawn Milochik a écrit :
> I have done what I wanted, but I think there must be a much better way.
>
> Given two timestamps in the following format, I just want to figure
> out how far apart they are (in days, seconds, whatever).
>
> Format:
>
> YYYY-MM-DD_MM:SS
>
> Example:
> 2007-09-11_16:41
>
>
> It seems to me that to do what I want, I need to convert a string into
> a number (time.mktime()), such as this: 1189543487.0
>
> Once I have that, I can just subtract one from the other and do
> whatever I want. The ugly part is converting something like
> 2007-09-11_16:41 to the numeric equivalent.
>
> Below is my code. Any suggestions?

import time
print time.mktime(time.strptime("2007-09-11_16:41", '%Y-%m-%d_%H:%M'))
=> 1189521660.0

FWIW, you may also want to have a look at the datetime module, with
special attention to the timedelta class.

HTH
--
http://mail.python.org/mailman/listinfo/python-list


grante at visi

Sep 11, 2007, 1:53 PM

Post #3 of 8 (76 views)
Permalink
Re: Difference between two times (working ugly code, needs polish) [In reply to]

On 2007-09-11, Shawn Milochik <Shawn[at]Milochik.com> wrote:

> I have done what I wanted, but I think there must be a much better way.

See the strptime() function in either the time or the datetime
modules:

http://docs.python.org/lib/module-time.html
http://docs.python.org/lib/module-datetime.html

--
Grant Edwards grante Yow! Here we are in America
at ... when do we collect
visi.com unemployment?
--
http://mail.python.org/mailman/listinfo/python-list


Shawn at Milochik

Sep 11, 2007, 2:20 PM

Post #4 of 8 (76 views)
Permalink
Re: Difference between two times (working ugly code, needs polish) [In reply to]

On 9/11/07, Grant Edwards <grante[at]visi.com> wrote:
> On 2007-09-11, Shawn Milochik <Shawn[at]Milochik.com> wrote:
>
> > I have done what I wanted, but I think there must be a much better way.
>
> See the strptime() function in either the time or the datetime
> modules:
>
> http://docs.python.org/lib/module-time.html
> http://docs.python.org/lib/module-datetime.html
>


Grant:

Thanks, this works, and is much shorter. Any further improvements, anyone?

def isOld(lastUpdate, runTimeStamp):

oneDay = 60 * 60 * 24

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
--
http://mail.python.org/mailman/listinfo/python-list


steve at holdenweb

Sep 11, 2007, 3:09 PM

Post #5 of 8 (76 views)
Permalink
Re: Difference between two times (working ugly code, needs polish) [In reply to]

Shawn Milochik wrote:
> On 9/11/07, Grant Edwards <grante[at]visi.com> wrote:
>> On 2007-09-11, Shawn Milochik <Shawn[at]Milochik.com> wrote:
>>
>>> I have done what I wanted, but I think there must be a much better way.
>> See the strptime() function in either the time or the datetime
>> modules:
>>
>> http://docs.python.org/lib/module-time.html
>> http://docs.python.org/lib/module-datetime.html
>>
>
>
> Grant:
>
> Thanks, this works, and is much shorter. Any further improvements, anyone?
>
> def isOld(lastUpdate, runTimeStamp):
>
> oneDay = 60 * 60 * 24
>
> 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

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 ...


--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

--
http://mail.python.org/mailman/listinfo/python-list


Shawn at Milochik

Sep 11, 2007, 5:31 PM

Post #6 of 8 (74 views)
Permalink
Re: Difference between two times (working ugly code, needs polish) [In reply to]

>
> 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
--
http://mail.python.org/mailman/listinfo/python-list


iainking at gmail

Sep 12, 2007, 1:59 AM

Post #7 of 8 (65 views)
Permalink
Re: Difference between two times (working ugly code, needs polish) [In reply to]

On Sep 12, 1:31 am, "Shawn Milochik" <Sh...@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

--
http://mail.python.org/mailman/listinfo/python-list


Shawn at Milochik

Sep 12, 2007, 3:42 AM

Post #8 of 8 (65 views)
Permalink
Re: Difference between two times (working ugly code, needs polish) [In reply to]

> 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:

You're absolutely right; I started writing it with one purpose in mind
and changed it midstream. I actually renamed it yesterday to dayDiff.
;o)

Shawn
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.