0

I want to program time that is independent and runs by itself in the background and ticks every second, and automatically changes Date every 24 seconds (1 second = 1 hour)

def PrintTime():
    D = 1
    M = 1
    Y = 2019
    print(D,"/",M,"/",Y)
    Time = 0
    while Time < 24:
        Time += 1
        time.sleep(1)
        if Time == 24:
            Time = 0
            D += 1
            M += 1
            if M in list[2,4,6,9,11] and D > 30:
                D = 1
            if M in list[1,3,5,7,8,10,12] and D > 31:
                D = 1
            if M > 12:
                M = 1
                Y += 1

This is the idea I had in mind, but the problem with this is the whole program would be sleeping every second, which is what I don't want to happen. Instead I want to run this by itself, in the background, without bothering the rest of the program.

5
  • Just checking that you're aware of the datetime module? Commented Sep 20, 2018 at 10:56
  • Actually no, can you fill me up on that? EDIT: Never mind I can just click on the link Commented Sep 20, 2018 at 10:56
  • 1
    Check the link in my first comment (I edited it in) Commented Sep 20, 2018 at 10:57
  • 1
    None of this would guarantee that 1 second is exactly one hour. Your clock will likely lag behind more and more over time. Commented Sep 20, 2018 at 10:59
  • You should probably use a monotonic system clock and multiply it by whatever factor you want to speed it up by; but that depends on what purpose exactly you need this for. Commented Sep 20, 2018 at 11:02

1 Answer 1

1

You can use the threading module to start a thread running your function with other code simultaneously. For example like this:

import threading
import time


def PrintTime():
    D = 1
    M = 1
    Y = 2019
    print(D, "/", M, "/", Y)
    Time = 0
    while Time < 24:
        print("Background thread: Time=", Time)
        Time += 1
        time.sleep(1)
        if Time == 24:
            Time = 0
            D += 1
            M += 1
            if M in list[2, 4, 6, 9, 11] and D > 30:
                D = 1
            if M in list[1, 3, 5, 7, 8, 10, 12] and D > 31:
                D = 1
            if M > 12:
                M = 1


thread = threading.Thread(target=PrintTime)
thread.start()

for i in range(10):
    print("do sth else", i)
    time.sleep(.7)
Sign up to request clarification or add additional context in comments.

4 Comments

What does the "for i in range(10)" at the end do?
This is just example code so that you can see that the background thread is running while your program does some other work.
@Justauser did this help ?
It's working well for now, I'll update you if anything goes wrong because this is for my NEA coursework for school

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.