Python string time [duplicate] - python-3.x

How can I run a function in Python, at a given time?
For example:
run_it_at(func, '2012-07-17 15:50:00')
and it will run the function func at 2012-07-17 15:50:00.
I tried the sched.scheduler, but it didn't start my function.
import time as time_module
scheduler = sched.scheduler(time_module.time, time_module.sleep)
t = time_module.strptime('2012-07-17 15:50:00', '%Y-%m-%d %H:%M:%S')
t = time_module.mktime(t)
scheduler_e = scheduler.enterabs(t, 1, self.update, ())
What can I do?

Reading the docs from http://docs.python.org/py3k/library/sched.html:
Going from that we need to work out a delay (in seconds)...
from datetime import datetime
now = datetime.now()
Then use datetime.strptime to parse '2012-07-17 15:50:00' (I'll leave the format string to you)
# I'm just creating a datetime in 3 hours... (you'd use output from above)
from datetime import timedelta
run_at = now + timedelta(hours=3)
delay = (run_at - now).total_seconds()
You can then use delay to pass into a threading.Timer instance, eg:
threading.Timer(delay, self.update).start()

Take a look at the Advanced Python Scheduler, APScheduler: http://packages.python.org/APScheduler/index.html
They have an example for just this usecase:
http://packages.python.org/APScheduler/dateschedule.html
from datetime import date
from apscheduler.scheduler import Scheduler
# Start the scheduler
sched = Scheduler()
sched.start()
# Define the function that is to be executed
def my_job(text):
print text
# The job will be executed on November 6th, 2009
exec_date = date(2009, 11, 6)
# Store the job in a variable in case we want to cancel it
job = sched.add_date_job(my_job, exec_date, ['text'])

Might be worth installing this library: https://pypi.python.org/pypi/schedule, basically helps do everything you just described. Here's an example:
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
while True:
schedule.run_pending()
time.sleep(1)

Here's an update to stephenbez' answer for version 3.5 of APScheduler using Python 2.7:
import os, time
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime, timedelta
def tick(text):
print(text + '! The time is: %s' % datetime.now())
scheduler = BackgroundScheduler()
dd = datetime.now() + timedelta(seconds=3)
scheduler.add_job(tick, 'date',run_date=dd, args=['TICK'])
dd = datetime.now() + timedelta(seconds=6)
scheduler.add_job(tick, 'date',run_date=dd, kwargs={'text':'TOCK'})
scheduler.start()
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2)
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()

I've confirmed the code in the opening post works, just lacking scheduler.run(). Tested and it runs the scheduled event. So that is another valid answer.
>>> import sched
>>> import time as time_module
>>> def myfunc(): print("Working")
...
>>> scheduler = sched.scheduler(time_module.time, time_module.sleep)
>>> t = time_module.strptime('2020-01-11 13:36:00', '%Y-%m-%d %H:%M:%S')
>>> t = time_module.mktime(t)
>>> scheduler_e = scheduler.enterabs(t, 1, myfunc, ())
>>> scheduler.run()
Working
>>>

I ran into the same issue: I could not get absolute time events registered with sched.enterabs to be recognized by sched.run. sched.enter worked for me if I calculated a delay, but is awkward to use since I want jobs to run at specific times of day in particular time zones.
In my case, I found that the issue was that the default timefunc in the sched.scheduler initializer is not time.time (as in the example), but rather is time.monotonic. time.monotonic does not make any sense for "absolute" time schedules as, from the docs, "The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid."
The solution for me was to initialize the scheduler as
scheduler = sched.scheduler(time.time, time.sleep)
It is unclear whether your time_module.time is actually time.time or time.monotonic, but it works fine when I initialize it properly.

dateSTR = datetime.datetime.now().strftime("%H:%M:%S" )
if dateSTR == ("20:32:10"):
#do function
print(dateSTR)
else:
# do something useful till this time
time.sleep(1)
pass
Just looking for a Time of Day / Date event trigger:
as long as the date "string" is tied to an updated "time" string, it works as a simple TOD function. You can extend the string out to a date and time.
whether its lexicographical ordering or chronological order comparison,
as long as the string represents a point in time, the string will too.
someone kindly offered this link:
String Comparison Technique Used by Python

had a really hard time getting these answers to work how i needed it to,
but i got this working and its accurate to .01 seconds
from apscheduler.schedulers.background import BackgroundScheduler
sched = BackgroundScheduler()
sched.start()
def myjob():
print('job 1 done at: ' + str(dt.now())[:-3])
dt = datetime.datetime
Future = dt.now() + datetime.timedelta(milliseconds=2000)
job = sched.add_job(myjob, 'date', run_date=Future)
tested accuracy of timing with this code:
at first i did 2 second and 5 second delay, but wanted to test it with a more accurate measurement so i tried again with 2.55 second delay and 5.55 second delay
dt = datetime.datetime
Future = dt.now() + datetime.timedelta(milliseconds=2550)
Future2 = dt.now() + datetime.timedelta(milliseconds=5550)
def myjob1():
print('job 1 done at: ' + str(dt.now())[:-3])
def myjob2():
print('job 2 done at: ' + str(dt.now())[:-3])
print(' current time: ' + str(dt.now())[:-3])
print(' do job 1 at: ' + str(Future)[:-3] + '''
do job 2 at: ''' + str(Future2)[:-3])
job = sched.add_job(myjob1, 'date', run_date=Future)
job2 = sched.add_job(myjob2, 'date', run_date=Future2)
and got these results:
current time: 2020-12-10 19:50:44.632
do job 1 at: 2020-12-10 19:50:47.182
do job 2 at: 2020-12-10 19:50:50.182
job 1 done at: 2020-12-10 19:50:47.184
job 2 done at: 2020-12-10 19:50:50.183
accurate to .002 of a second with 1 test
but i did run a lot of tests and accuracy ranged from .002 to .011
never going under the 2.55 or 5.55 second delay

#everytime you print action_now it will check your current time and tell you should be done
import datetime
current_time = datetime.datetime.now()
current_time.hour
schedule = {
'8':'prep',
'9':'Note review',
'10':'code',
'11':'15 min teabreak ',
'12':'code',
'13':'Lunch Break',
'14':'Test',
'15':'Talk',
'16':'30 min for code ',
'17':'Free',
'18':'Help ',
'19':'watever',
'20':'watever',
'21':'watever',
'22':'watever'
}
action_now = schedule[str(current_time.hour)]

Related

Is there a way to find the local timezone with datetime?

I am trying to make some code that creates a clock using the user's local time with the datetime library.
import datetime as dt
import os
import time
z = dt.datetime.now()
def local_time():
def time_check(t):
if t < 10:
t = "0{}".format(t)
else:
t = t
return t
p = dt.datetime.now()
hour = p.hour
minute = p.minute
second = p.second
hour = time_check(hour)
minute = time_check(minute)
second = time_check(second)
local_time = '{}:{}:{}'.format(hour, minute, second)
return local_time
time_zone = z.timezone()
for i in range(999999999999999999999):
print("Time: {} {}".format(local_time(), time_zone))
time.sleep(1)
os.system("cls")
What I am doing is gathering the hour, minute, and second of the local time, and constantly updating it in the terminal, and deleting the previous time.
This works fine, but I am also trying to display the timezone of the user, and I am having a hard time trying to find a way to do it. Does anyone know a way? Thanks.
Importing the python-dateutil library should make this easier. Install the library with the pip install python-dateutil command at the terminal if you don't have it installed beforehand. Once you do that, test the below code:
from datetime import *
from dateutil.tz import *
print(datetime.now(tzlocal()).tzname())
print(datetime(2021, 6, 2, 16, 00, tzinfo=tzlocal()).tzname())
#Output
#SA Western Standard Time
#SA Western Standard Time
Using tzlocal() and tzname() together will give you the current timezone. The dateutil library is very powerful and useful. Check out the full documentation HERE.
Hopefully that helped.

Do Something At a Consistent Frequency in Python

I have a python program that reads from a serial port, accesses registers then writes data to the CSV. Id like to perform a writing operation to the csv at a set frequency of 100Hz. Below is sample code related to the timing which attempts to limit writing to the csv. For simplicity it will only print to console rather than write to csv.
import datetime
from datetime import timezone
import time
FREQ = 5
CYCLETIME = 1/FREQ
def main():
while(1):
### Code that will read message bytes from a port
# Time start for Hz
start = time.monotonic()
delta = time.monotonic() - start
if delta < CYCLETIME:
time.sleep(CYCLETIME - delta)
# in the full program we write to a csv but in this simple program we will just print it
milliseconds_since_epoch = datetime.datetime.now(timezone.utc)
print(milliseconds_since_epoch)
if __name__ == "__main__":
main()
2020-08-24 18:57:15.572637+00:00
2020-08-24 18:57:15.773183+00:00
2020-08-24 18:57:15.973637+00:00
2020-08-24 18:57:16.174117+00:00
2020-08-24 18:57:16.374569+00:00
2020-08-24 18:57:16.575058+00:00
2020-08-24 18:57:16.775581+00:00
2020-08-24 18:57:16.976119+00:00
2020-08-24 18:57:17.176627+00:00
2020-08-24 18:57:17.377103+00:00
2020-08-24 18:57:17.577556+00:00
The output seems consistent for 5Hz but if I change it to 100Hz it seems inconsistent. It sounds like this could be a time accuracy drift associated to time.monotonic. Is this a good approach? Is time.montonic appropriate?
My knowledge with pythons thread library is limited but in the near future, I plan to create 2 child threads for each task. One which constantly reads from serial and another that will write (or in our case print to console every 100Hz).
Edit:
I took the solution below and modified it. The original solution seemed to only print once. Here is my new attempt:
import datetime
from datetime import timezone
import time
FREQ = 5
CYCLETIME = 1/FREQ
def main():
t0 = time.perf_counter() # Time ref point in ms
time_counter = t0 # Will be incremented with CYCLETIME for each iteration
while 1:
### Code that will read message bytes from a port
now = time.perf_counter()
elapsed_time = now - time_counter
if elapsed_time < CYCLETIME:
target_time = CYCLETIME - elapsed_time
time.sleep(target_time)
# In the full program we write to a csv but in this simple program we will just print it
milliseconds_since_epoch = datetime.datetime.now(timezone.utc)
print(milliseconds_since_epoch)
time_counter += CYCLETIME
if __name__ == "__main__":
main()
Output:
I used matplot lib to determine the frequency to create this output. I take the rolling window difference of the current and previous value and inverse it since frequency=1/(time diff).
There are 5 problems in your algorithm:
Using time.sleep() has low accuracy, sometimes with an error around 10-13ms, depending on your system. (ref)
start and delta variables do not track the time spent on print(), as delta is set right after start.
start and delta variables use two different time.monotonic(). You should call the function only once, and pass the value to the other variable to make sure the same time value is being used.
Tick rate of time.monotonic() is 64 per second. Use time.perf_counter() instead. (ref)
You are doing time.sleep() on a delta time, which has low accuracy. If you have two threads running together, the cycles will hardly be synchronized. You need a time tracker which makes it sleep based on elapsed time.
Fix:
The following code make use of time.perf_counter(), and uses time_counter to keep track of the previous loop iteration timestamp.
The next loop time is the previous loop timestamp + cycle time - elapsed time. Thus, we will make sure the time.sleep() sleeps the program until then.
def main():
t0 = time.perf_counter() # Time ref point
time_counter = t0 # Will be incremented with CYCLETIME for each iteration
while 1:
### Code that will read message bytes from a port
now = time.perf_counter()
elapsed_time = now - t0
target_time = time_counter + CYCLETIME
if elapsed_time < target_time:
time.sleep(target_time - elapsed_time)
# In the full program we write to a csv but in this simple program we will just print it
milliseconds_since_epoch = datetime.datetime.now(timezone.utc)
print(milliseconds_since_epoch)
time_counter += CYCLETIME

How to get python time to update

I would like to create a program that allows you to ask the current time. The problem is that the program only prints the time of its execution, not the actual current time.
My code:
import datetime
now = datetime.datetime.now()
user_input = input("")
if user_input == "what time is it":
print(str(now.hour) + ":" + str(now.minute))
If I started the program at 6:25, no matter what time it is, the program returns 6:25.
change your code to
import datetime
user_input = input("")
if user_input == "what time is it":
now = datetime.datetime.now()
print(str(now.hour) + ":" + str(now.minute))
the now variable is holding the timestamp of when you called the now function
The program is hitting the now = datetime.datetime.now() command when you run, not when you input. To solve this problem, just put the code inside the if statement. It will then time stamp the moment you enter your input.

how to get return value from apscheduler jobs

I'm using apscheduler to arrange some jobs. Some of the jobs have return values after the job be executed. How can I get the return values from these jobs ? Does anyone has idea on this ? Thanks very much.
The feature is still under development, but you can use global variables for now. Here's an example:
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
def fn():
'''Increase `times` by one and print it.'''
global times
times += 1
print(times)
sched = BlockingScheduler()
times = 0
# Execute fn() each second.
sched.add_job(fn, trigger=CronTrigger(second='*/1'))
sched.start()

python - loop at exact time intervals

I want to run a piece of code at exact time intervals (of the order of 15 seconds)
Initially I used time.sleep(), but then the problem is the code takes a second or so to run, so it will get out of sync.
I wrote this, which I feel is untidy because I don't like using while loops. Is there a better way?
import datetime as dt
import numpy as np
iterations = 100
tstep = dt.timedelta(seconds=5)
for i in np.arange(iterations):
startTime = dt.datetime.now()
myfunction(doesloadsofcoolthings)
while dt.datetime.now() < startTime + tstep:
1==1
Ideally one would use threading to accomplish this. You can do something like
import threading
interval = 15
def myPeriodicFunction():
print "This loops on a timer every %d seconds" % interval
def startTimer():
threading.Timer(interval, startTimer).start()
myPeriodicFunction()
then you can just call
startTimer()
in order to start the looping timer.
Consider tracking the time it takes the code to run (a timer() function), then sleeping for 15 - exec_time seconds after completion.
start = datetime.now()
do_many_important_things()
end = datetime.now()
exec_time = end - start
time.sleep(15-exec_time.total_seconds())
You can use a simple bash line:
watch -n 15m python yourcode.py

Resources