From: Jim on
How can I calculate how much time is between now and the next 2:30
am? Naturally I want the system to worry about leap years, etc.

Thanks,
Jim
From: Neil Cerutti on
On 2010-07-23, Jim <jim.hefferon(a)gmail.com> wrote:
> How can I calculate how much time is between now and the next
> 2:30 am? Naturally I want the system to worry about leap
> years, etc.

You need the datetime module. Specifically, a datetime and
timedelta object.

--
Neil Cerutti
From: Jim on
Thanks; I'll have a look, and a think.

Jim
From: David Bolen on
Neil Cerutti <neilc(a)norwich.edu> writes:

> On 2010-07-23, Jim <jim.hefferon(a)gmail.com> wrote:
>> How can I calculate how much time is between now and the next
>> 2:30 am? Naturally I want the system to worry about leap
>> years, etc.
>
> You need the datetime module. Specifically, a datetime and
> timedelta object.

Although it sounds like the question is to derive the timedelta value,
so it's not known up front. That's a little trickier since you'd need
to construct the datetime object for "next 2:30 am" to subtract "now"
from to get the delta. But that requires knowing when the next day
is, thus dealing with month endings. Could probably use the built-in
calendar module to help with that though.

For the OP, you might also take a peek at the dateutil third party
module, and its relativedelta support, which can simplify the creation
of the "next 2:30 am" datetime object.

Your case could be handled by something like:

from datetime import datetime
from dateutil.relativedelta import relativedelta

target = datetime.now() + relativedelta(days=+1, hour=2, minute=30,
second=0, microsecond=0)
remaining = target - datetime.now()

This ends up with target being a datetime instance for the next day at
2:30am, and remaining being a timedelta object representing the time
remaining, at least as of the moment of its calculation.

Note that relativedelta leaves fields alone that aren't specified, so
since datetime.now() includes down to microseconds, I clear those
explicitly). Since you really only need the date, you could also use
datetime.date.today() instead as the basis of the calculation and then
not need second/microsecond parameters to relativedelta.

-- David
From: Christian Heimes on
> Your case could be handled by something like:
>
> from datetime import datetime
> from dateutil.relativedelta import relativedelta
>
> target = datetime.now() + relativedelta(days=+1, hour=2, minute=30,
> second=0, microsecond=0)
> remaining = target - datetime.now()

You don't need the dateutil package for the trick:

>>> dt = datetime(2010, 1, 1, 1, 0)
>>> str(dt)
'2010-01-01 01:00:00'
>>> next = dt.replace(hour=2, minute=30)
>>> next - dt
datetime.timedelta(0, 5400)
>>> (next - dt).seconds
5400

>>> dt = datetime(2010, 1, 1, 3, 0)
>>> next = dt.replace(hour=2, minute=30)
>>> next - dt
datetime.timedelta(-1, 84600)
>>> (next - dt).seconds
84600

 |  Next  |  Last
Pages: 1 2
Prev: Unicode error
Next: Function closure inconsistency