Assigning a time to a variable


(Khalil Mehtal) #1

i’m trying to get the start and end time of task using this method but everytime i refresh i get a new time how can i make it assign time only on change from false to true

def get_start_time(self, time1=None, ):
        if self.start == True and time1 is None:
            time1 = timezone.now()

        else:
            time1 = "time1 is None"

        return "time 1 : {}".format(time1)

(Erik Brown) #2

Khalil,

You don’t say how self.start is assigned, and your “if” statement seems a little suspect. If self.start is False then you will set a new time regardless the value of time1.

Also, why do you want to do this? Seems a rather complex way to measure the time. Simpler to just get the start and end time directly:

start = timezone.now()
(do some complex task)
end = timezone.now()

This gives you a direct measure of your start and end time. You can subtract them to get the time difference, or other operations as needed. No fuss.

Erik


(Khalil Mehtal) #3

i’m just trying to assigne a time on boolean value change when start goes true and when end goes true something similar to auto_now and auto_now_add


(Erik Brown) #4

Okay, but how does self.start get set? Do you mean to alter the value within this method? Something like:

def get_start_time(self, time1=None, ):
        if self.start == True and time1 is None:
            time1 = timezone.now()
            self.start = False
        else:
            # time1 was previously set
            time1 = "time1 is None"

        return "time1 : {}".format(time1)

(Khalil Mehtal) #5

how self.start is set is irrelevant on this method i want to get the timestamp every time the self.start is updated as i mentioned earlier similiar to auto_now