2

Here's my code, it's just supposed to basically change end to start + 25 minutes. I'm just wondering if there is a way to update datetime.datetime.now() to be the current time. As it stands, it just stays at whatever it was when I first used the module. So the if statement will never be true.

import datetime
start = datetime.datetime.now()
end = start + datetime.timedelta(minutes = 25)
if start == end:
    end = end + datetime.timedelta(minutes = 25)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
tommo
  • 599
  • 2
  • 9
  • 14
  • 1
    Hey man you really need to tag your question about which programming language you are using so that we can know how to help – Simon Wang Sep 12 '12 at 06:18
  • It is better if you tell what you intend to do with this code, because currently it doesn't make sense. `datetime.now()` always return the current time, at each call. If put in `start` it won't be updated since it is in a variable – CharlesB Sep 12 '12 at 06:36
  • It says what I intend to do at the top. But thanks. – tommo Sep 12 '12 at 08:43
  • related: [How to run a function periodically in python](http://stackoverflow.com/q/24174924/4279) – jfs Apr 05 '15 at 17:22

2 Answers2

2

As CharlesB has suggested, the start variable is not updated. You need to take the now value at the time you want to perform the test.

Rewrite the line:

if start == end:

To

if datetime.datetime.now() > end:

EDIT After Tommo's comment, I think another solution may be easier.

import time
while True:
    putMessageToScreen()
    time.sleep(25*60)
Hans Then
  • 10,935
  • 3
  • 32
  • 51
  • Thanks mate! :D Testing now. I also realise the == mistake. It wouldn't have worked even if it should have, unless I did exactly on the right time lol – tommo Sep 12 '12 at 08:53
  • :S Still doesn't work.... hmmmm.... EDIT: Ok, I think it's because end was still defined using start. – tommo Sep 12 '12 at 09:47
  • Nope.... :( Can't figure out why. Seems it can only be called once or something? – tommo Sep 13 '12 at 08:01
  • I am certain it can be called more than once. Can you post the complete code? (Or if this is the complete code, then you must explain to me what you are trying to achieve.) – Hans Then Sep 13 '12 at 09:54
  • It isn't the complete code. But what I posted is all there is at the moment. Basically I am trying to create a program that will put a message on the screen every 25 minutes. Do I need to put some code which will check the now() every minute or so? – tommo Sep 14 '12 at 02:40
  • Dear sir, you had us all on the wrong track ;-) I have edited my solution match your actual requirements. – Hans Then Sep 14 '12 at 07:22
  • Oh :S Sorry about that lol I'll try it now. Can I just ask, what does the sleep(25*60) do? Also I got "NameError: name 'putMessageToScreen' is not defined" – tommo Sep 15 '12 at 03:42
  • Nevermind, I realised what you're saying. And what the 25*60 is. But it still doesn't work, the datetime is still staying the same. – tommo Sep 15 '12 at 04:51
  • I removed datetime from the solution, so it should work _without_ changing the solution. It should simply put a message each 25 minutes, which is what you want right? – Hans Then Sep 15 '12 at 06:33
  • ohhhh.... yeah sort of, I do want to add more than that though, which is why I used datetime. I was playing around with it and I got it working with datetime though now, including with your fix. Thank you so much!!!! :D – tommo Sep 15 '12 at 14:07
  • Good to hear. Please consider accepting or upvoting answers that have been of help to you. This will provide feedback for other users. – Hans Then Sep 15 '12 at 19:02
  • I was trying to, it said I need 15 reputation first. I will come back when I have that :) – tommo Sep 16 '12 at 05:28
0

If you want a value to change on subsequent references, you want to use a function call, not a stored variable.

In principle, you could make the above code do what you want by subclassing datetime.datetime and defining the __add__ method to recompute datetime.datetime.now(), but it wouldn't be a good idea.

Mu Mind
  • 10,935
  • 4
  • 38
  • 69
  • Thanks man, I'll have a look at that, just for interest even if I shouldn't do it. – tommo Sep 12 '12 at 08:43
  • And I should mention that's a special case that will catch when you add, but not subtract, print, etc. Really, my point is just that there's some way to defer the action til later. – Mu Mind Sep 12 '12 at 09:17