time between now and the next 2:30 am?

J

Jim

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
 
N

Neil Cerutti

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

David Bolen

Neil Cerutti said:
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
 
J

Jim

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.UPDATE_TIME_HOURS,minute=settings.UPDATE_TIME_MINS,second=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_MINS,WARNING_MINS,)
else:
w=None
return {'CURRENT_LOCAL_TIME': time.asctime(time.localtime()),
'TIME_WARNING':w}
 
M

MRAB

Jim said:
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.UPDATE_TIME_HOURS,minute=settings.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.
 
J

John Nagle

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
 
J

Jim

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top