Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
time intervals
#1
Hi,

I'm currently trying to get 2 different time displays each time I call a function using the datetime library. However, in my code, I call the function twice, and each time, the milliseconds display the same, which I thought would be slightly different.

Also, I tried to place a 2 second stop in the code using the time library, but when I did that, the entire code didn't work.
(These have the comments before them.)

Any ideas what's going on?

from datetime import date, time, datetime

#import time

def get_datatime():
     today = date.today()  # date
     current_time = time(now.hour, now.minute, now.second, now.microsecond)  # time
     #print("date:", today, "time:", current_time)
     return today, current_time


now = datetime.now()  # object to class datatime`

x = get_datatime()  # store date and time in a 2-element tuple, [today, current_time]
print("time 1 = ", x[1])
#time.sleep(2)       # wait (seconds)
y = get_datatime()
print("time 2 = ", y[1])
Reply
#2
You're not looking up the time in the function at all. You're looking up the time outside the function on line 12.

The variable now is set there, and all the function does is use the information in that variable.

Move the datetime.now() call into the function.
Reply
#3
That seems like such an oversight now my part! Thank you. I moved that into the function, but for some, the print statements still shows milliseconds the same, but I don't understand why? They should be time-separated function calls.

from datetime import date, time, datetime

#import time

def get_datatime():
     now = datetime.now()  # object to class datatime
     today = date.today()  # date
     current_time = time(now.hour, now.minute, now.second, now.microsecond)  # time
     #print("date:", today, "time:", current_time)
     return today, current_time


x = get_datatime()  # store date and time in a 2-element tuple, [today, current_time]
print("time 1 = ", x[1])
#time.sleep(2)       # wait (seconds)
y = get_datatime()
print("time 2 = ", y[1])
Reply
#4
Can you show the output? What OS are you running this on? On MacOS, the times are different, but within the same millisecond. As there's no delay between the calls, this seems reasonable to me. This is the output from your program with python3 on a mac.

Output:
time 1 = 14:47:34.525161 time 2 = 14:47:34.525226
Reply
#5
(Jan-30-2021, 09:44 PM)bowlofred Wrote: You're not looking up the time in the function at all. You're looking up the time outside the function on line 12.

The variable now is set there, and all the function does is use the information in that variable.

Move the datetime.now() call into the function.

(Jan-30-2021, 10:50 PM)bowlofred Wrote: Can you show the output? What OS are you running this on? On MacOS, the times are different, but within the same millisecond. As there's no delay between the calls, this seems reasonable to me. This is the output from your program with python3 on a mac.

Output:
time 1 = 14:47:34.525161 time 2 = 14:47:34.525226

time 1 = 18:00:21.688303
time 2 = 18:00:21.688303


Strangest thing, when I reran it, it did show different times, but only once. 10 more times, now they're the same gain.

Windows 10, x64, PyCharm IDE
Reply
#6
I see some notes that time.time() on windows only has 16ms precision. Per PEP 564, you might get better precision via time.time_ns(). So maybe something like this?

from datetime import date, time, datetime

import time as tm

def get_datatime():
     now = datetime.fromtimestamp(tm.time_ns()/10**9)  # object to class datatime
     today = date.today()  # date
     current_time = time(now.hour, now.minute, now.second, now.microsecond)  # time
     #print("date:", today, "time:", current_time)
     return today, current_time


x = get_datatime()  # store date and time in a 2-element tuple, [today, current_time]
print("time 1 = ", x[1])
#time.sleep(2)       # wait (seconds)
y = get_datatime()
print("time 2 = ", y[1])
The precision doesn't seem obviously better on a Mac, and I won't have an opportunity to try it on Windows for a bit. So not certain that will make a difference.
Reply
#7
fyi datetime.now() just calls time.time(). It doesn't have a higher precision, so using it's return value as arguments to time.time() doesn't really make sense.

https://github.com/python/cpython/blob/m...e.py#L1696

import time as _time

# ...

    @classmethod
    def now(cls, tz=None):
        "Construct a datetime from time.time() and optional time zone info."
        t = _time.time()
        return cls.fromtimestamp(t, tz)
Reply
#8
I solved it, and the time stamps looks approximately difference by 1.01s, which I assume means there's 0.01s need for processing time. I'm assuming there's a better way to do time deltas, but for data logging, I think this would be ok, no?

from datetime import datetime
import time

print("\n*Timestamps*")

def time_stamps():
    return datetime.now()  # object to class datatime

x = time_stamps()
time.sleep(1)
y = time_stamps()
print(x)
print(y)

# Time Delays
    #time.sleep(1) # wait 1 second (s)
    #time.sleep(1/1000) # wait 1 millisecond (ms)
    #time.sleep(1/1000000) # wait 1 microsecond (us)
    #time.sleep(1 / 100000000)  # wait 1 nanosecond (ns)


if x > y:
    print("\ninstance 1 (x) is greater by: ",abs(x-y), "seconds")

else:
    print("\ninstance 2 (y) is greater by:", abs(x-y), "seconds")


print("\n*DATETIME attributes use *")
now = datetime.now()  # create an object to class datetime
print("today = ", now.date()) # returns year-month-day
print("month =",now.year) # year
print("year =",now.month) # month
print("day =", now.day) # day
print("hour =", now.hour) # hours
print("minute =", now.minute) # minutes
print("seconds =", now.second) # seconds
print("microseconds =", now.microsecond)  # microseconds


# print all attributes in class datetime
print("\n*DATETIME attributes use *")
for x in dir(datetime):
        print(x)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plotting “simple” diagram with gridlines at specified intervals schniefen 1 2,425 Dec-03-2020, 05:54 PM
Last Post: schniefen
  Setting Tick Intervals In Sub Plots JoeDainton123 1 1,805 Oct-03-2020, 11:52 PM
Last Post: scidam
  Help Grouping by Intervals on list paul41 1 2,130 Dec-03-2019, 09:43 PM
Last Post: michael1789
  Grouping a list of various time into intervals paul41 1 3,786 Nov-24-2019, 01:47 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020