From: Jim on
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}
From: MRAB on
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}

From: John Nagle on
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

From: Jim on
On Jul 23, 8:52 pm, MRAB <pyt...(a)mrabarnett.plus.com> wrote:
> > dt_twothirty=dt_localtime.replace(hour=settings.UPDATE_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
First  |  Prev  | 
Pages: 1 2
Prev: Unicode error
Next: Function closure inconsistency