time between now and the next 2:30 am?

MRAB python at mrabarnett.plus.com
Fri Jul 23 20:52:10 EDT 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.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.

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




More information about the Python-list mailing list