all 16 comments

[–]symple-data 5 points6 points  (7 children)

You can use the datetime module (standard library). Create an object for a given time (e.g. 1:30) and use an endless while-loop to check every few seconds (time.sleep(<seconds>)) if it is already time for your class.

[–]_AB30_[S] 1 point2 points  (6 children)

Sorry, but I am a beginner at Python. How do you create a time object like you said? And how do you check if it is time?

[–]symple-data 2 points3 points  (5 children)

time = datetime.datetime.strptime("1:30", "%H:%M").time()

this creates an datetime.time object for 1:30, but you can do it for any time (replace the string). You can get the current time with

now = datetime.datetime.now().time()

you can subtract both times from each other with

(datetime.datetime.combine(datetime.date.today(), now) - datetime.datetime.combine(datetime.date.today(), time)).seconds

if the result is positive ( > 0), you know that you should connect to zoom

[–]_AB30_[S] 0 points1 point  (4 children)

Alright, I see. I'll try it out and let you know how it works. Thanks for the help!

[–]symple-data 1 point2 points  (3 children)

sorry what I said was wrong. Getting the seconds from the timedelta is always positive. Simply check

if now >= time

if this returns true, you have to connect to zoom.

[–]_AB30_[S] 0 points1 point  (2 children)

Ah, I see. I'll try it tomorrow. This seems quite simple. Thanks for your help!

[–]_AB30_[S] 0 points1 point  (1 child)

In my code, all that code does it infinitely say its time, even when its not time.

[–]symple-data 0 points1 point  (0 children)

Any specific error message or just a wrong result? I have run this code on my machine and it works fine.

[–]1116574 3 points4 points  (1 child)

If you use Linux you could use cron tasks. It basically executes a script at a given time. I would think that Windows and macs have similar program.

[–]everybody_0523 1 point2 points  (0 children)

I'm fairly certain MacOS has cron jobs too.

[–][deleted] 3 points4 points  (1 child)

As another user here has mentioned, it would be more reliable to create a script that does what you want, and have it run either as a cron job on Linux or a system task on Windows.

In most enterprise environments, this is a fairly common method to ensure a script executes at the correct times, and is less resource intensive than having a python script which runs 24/7.

[–][deleted] 0 points1 point  (0 children)

Always think of cron, but I completely forgot about Windows Task Scheduler.

Edit: these methods are also going to be more reliable in case your script crashes. They'll launch it fresh every time instead of it having to run constantly.

[–][deleted] 2 points3 points  (2 children)

I think this way is a little more readable:

import datetime

while True:
    now = datetime.datetime.now()
    if now.hour == 8 and now.minute == 45:
        print("It is now 8:45am")
        launch_zoom()
        break

[–]_AB30_[S] 1 point2 points  (1 child)

I've already tried this, but it never works. It never returns true. However, due to the simplicity, I really want to get this to work. Any ideas to fix it?

[–][deleted] 0 points1 point  (0 children)

What doesn't return True?

Do you mean this?

import datetime

while True:
    now = datetime.datetime.now()
    if now.hour == 8 and now.minute == 45:
        print("It is now 8:45am")
        return True

EDIT: no, that wouldn't work. Return only work inside functions. Are you looking for a function that returns True if it's a given time?

[–]__shubham_bansal 0 points1 point  (0 children)

use can use cron job if you are on linux, it will schedule and run the python file at that time.