Velocity Reviews > time between now and the next 2:30 am?

# time between now and the next 2:30 am?

Jim
Guest
Posts: n/a

 07-23-2010
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

Neil Cerutti
Guest
Posts: n/a

 07-23-2010
On 2010-07-23, Jim <> 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

Jim
Guest
Posts: n/a

 07-23-2010
Thanks; I'll have a look, and a think.

Jim

David Bolen
Guest
Posts: n/a

 07-23-2010
Neil Cerutti <> writes:

> On 2010-07-23, Jim <> 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

Jim
Guest
Posts: n/a

 07-23-2010
Thank you again to everyone; I greatly appreciate the help. I ended
with essentially what Christian advised and it seems to work fine.
FWIW, below is the rouine (I couldn't figure out how to do it without
the kludgy x variable). The background is that this is a Django
context processor that makes available the time until the next 2:30
because that is when the web site is rebuilt.

I hope the code isn't too much mangled by the online editor,
Jim

.................................................. ............................

import time, datetime
from django.conf import settings

WARNING_MINS=10 # How many mins before update time should start
warning?
WARNING_SECS=60*WARNING_MINS

def current_time_processor(req):
"""Add current time information to the Context
"""
# Calculate time until the next 2:30 am
x=datetime.datetime(2010,7,23) # actual date doesn't matter?
dt_localtime=x.fromtimestamp(time.time())

dt_twothirty=dt_localtime.replace(hour=settings.UP DATE_TIME_HOURS,minute=settings.UPDATE_TIME_MINS,s econd=0,microsecond=0)
dd_diff=dt_twothirty-dt_localtime
secs_until_twothirty=dd_diff.seconds
if secs_until_twothirty<WARNING_SECS:
w='This site shuts down every night at %02d:%02d local time,
to refresh the database. There are fewer than %d minutes until that
shutdown. If you have not completed your work when the site shuts
down then you will lose your work.' %
(settings.UPDATE_TIME_HOURS,settings.UPDATE_TIME_M INS,WARNING_MINS,)
else:
w=None
return {'CURRENT_LOCAL_TIME': time.asctime(time.localtime()),
'TIME_WARNING':w}

MRAB
Guest
Posts: n/a

 07-24-2010
Jim wrote:
> Thank you again to everyone; I greatly appreciate the help. I ended
> with essentially what Christian advised and it seems to work fine.
> FWIW, below is the rouine (I couldn't figure out how to do it without
> the kludgy x variable). The background is that this is a Django
> context processor that makes available the time until the next 2:30
> because that is when the web site is rebuilt.
>
> I hope the code isn't too much mangled by the online editor,
> Jim
>
> .................................................. ...........................
>
> import time, datetime
> from django.conf import settings
>
> WARNING_MINS=10 # How many mins before update time should start
> warning?
> WARNING_SECS=60*WARNING_MINS
>
> def current_time_processor(req):
> """Add current time information to the Context
> """
> # Calculate time until the next 2:30 am
> x=datetime.datetime(2010,7,23) # actual date doesn't matter?
> dt_localtime=x.fromtimestamp(time.time())
>

Why not:

dt_localtime = datetime.datetime.now()

> dt_twothirty=dt_localtime.replace(hour=settings.UP DATE_TIME_HOURS,minute=settings.UPDATE_TIME_MINS,s econd=0,microsecond=0)

You're changing the time of day, but not the date. You might want to add
a day to the shutdown time if it's earlier than the current time.

> dd_diff=dt_twothirty-dt_localtime
> secs_until_twothirty=dd_diff.seconds
> if secs_until_twothirty<WARNING_SECS:
> w='This site shuts down every night at %02d:%02d local time,
> to refresh the database. There are fewer than %d minutes until that
> shutdown. If you have not completed your work when the site shuts
> down then you will lose your work.' %
> (settings.UPDATE_TIME_HOURS,settings.UPDATE_TIME_M INS,WARNING_MINS,)
> else:
> w=None
> return {'CURRENT_LOCAL_TIME': time.asctime(time.localtime()),
> 'TIME_WARNING':w}

John Nagle
Guest
Posts: n/a

 07-24-2010
On 7/23/2010 10:01 AM, Jim 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.
>
> Thanks,
> Jim

DAYSECS = 24*60*60
GOALSECS = (2*60 + 30)*60
now = (GOALSECS + DAYSECS - (int(time.time()) % DAYSECS)) % DAYSECS

This is for UT; the adjustment for timezone should be obvious.

John Nagle

Jim
Guest
Posts: n/a

 07-24-2010
On Jul 23, 8:52*pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
> > dt_twothirty=dt_localtime.replace(hour=settings.UP DATE_TIME_HOURS,minute=se ttings.UPDATE_TIME_MINS,second=0,microsecond=0)

>
> You're changing the time of day, but not the date. You might want to add
> a day to the shutdown time if it's earlier than the current time.

I started out by finding the right date (I used a less-than test and
toordinal and fromordinal() ). However, after some trials, I came to
believe that I don't need to find the right date. The part of that
calculation I need, and later refer to, is the .seconds attribute. I
perceive that the way TimeDelta objects are laid out, the seconds
attribute will be the same, regardless of whether I calculate it using
2:30 today or first finding which is the right date and using its
2:30.

That is, as I understood it, if it is now 2:29 then the .seconds
attribute will be 60. If it is now 2:31 then the .seconds attribute
will be 24*60*60-60. I believe this holds regardless of which day I
use.

>>> import time,datetime
>>> x=datetime.datetime(2010,7,23)
>>> dt_localtime=x.fromtimestamp(time.time())
>>> dt_localtime

datetime.datetime(2010, 7, 24, 7, 17, 46, 122642)
>>> dt_twothirty=dt_localtime.replace(hour=2,minute=30 ,second=0,microsecond=0)
>>> print dt_twothirty

2010-07-24 02:30:00
>>> dd_diff=dt_twothirty-dt_localtime
>>> print dd_diff

-1 day, 19:12:13.877358
>>> dt_tomorrow_twothirty=dt_localtime.replace(day=25, hour=2,minute=30,second=0,microsecond=0)
>>> print dt_tomorrow_twothirty

2010-07-25 02:30:00
>>> dd_tomorrow_diff=dt_tomorrow_twothirty-dt_localtime
>>> print dd_tomorrow_diff

19:12:13.877358

Tested it, of course. Not that I haven't gotten things wrong in the
past, even though I tested them. :-}

Jim