how to detect change in hour of system time - python

I am trying to make a program in Python that beeps every hour. and no of beeps should be equal to no of hours. such as for 12 o'clock it should beep 12 times.
but I have no idea how to detect change in hour.
def bep(h):
for i in range(h):
winsound.Beep(2000,800)
sleep(2)
the above code beeps for h no of times and
def hour():
return hour=int(time.ctime(time.time()).split()[3].split(':')[0])
it gives the hour.
but how to detect that hour has changed.
whether should I check every regular time interval and use previous hour and current hour to detect changes? I think this idea is not effective. because of time delay of interval time i.e.
check current time every 5 second and compare two successive check to detect change in hours.
is there any method to accomplish it directly.

At its simplest:
import datetime
import time
current = 0
while True:
time.sleep(5)
if datetime.datetime.now().hour != current:
current = datetime.datetime.now().hour
print "beep" , str(current)
Note you can test the code by using .minute rather than .hour Which will allow you to see if it fits your purposes.
You will have to replace the print "beep", str(current) with a call to your function bep(current)
Also you might want to consider adding a little extra code to your bep(h) function.
if h>12: h=h-12
if h == 0: h = 12
To ensure that for example: at 16:00 you only hear 4 beeps rather than 16 and at midnight, you hear 12 beeps, rather than none.

Related

How to count "times per second" in a correct way?

Goal: I would like to see how many times python is able to print something per 1 second.
For educational purposes I'm trying to make a script that shows how many times per every second a random module will appear in a loop. How to do it in a fastest pythonic way?
At first, to count seconds I wrote this code:
import time
sec = 0
while True:
print(sec)
time.sleep(1)
sec += 1
But this one seems slower than a real seconds.
So I decided to use local seconds. Also, before continue my script I wanted to count how many times python will print 'you fool' manually, so I wrote following code:
import time
def LocalSeconds():
local_seconds = time.gmtime()[5:-3]
local_seconds = int(local_seconds[0])
return local_seconds
while True:
print(LocalSeconds(), 'you fool')
Output:
first second - 14 times per second;
next second - 13 times;
next second - 12 times, etc. Why it goes slower?
Where I end / stuck right now:
import time, random
def RandomNumbers():
return random.randint(3,100)
def LocalSeconds():
local_seconds = time.gmtime()[5:-3]
local_seconds = int(local_seconds[0])
return local_seconds
def LocalSecondsWithPing():
local_seconds_ping = time.gmtime()[5:-3]
local_seconds_ping = int(local_seconds[0:1])
return local_seconds_ping
record_seconds = []
record_seconds_with_ping = []
while True:
record_seconds.append(LocalSeconds())
record_seconds_with_ping.append(LocalSecondsWithPing())
if record_seconds == record_seconds_with_ping:
RandomNumbers()
del record_seconds_with_ping[0]
del record_seconds[-1]
Also, I guess I need to use "for" loop, not "while"? How to do this script?
Counting a single second won't give you a good result. The number of prints in a single second may vary depending on things like other threads currently running on your system (for the OS or other programs) and may be influenced by other unknown factor.
Consider the followind code:
import calendar
import time
NumOfSeconds=100 #Number of seconds we average over
msg='Display this message' #Message to be displayed
count=0 #Number of time message will end up being displayed
#Time since the epoch in seconds + time of seconds in which we count
EndTime=calendar.timegm(time.gmtime()) + NumOfSeconds
while calendar.timegm(time.gmtime())<EndTime: #While we are not at the end point yet
print(msg) #Print message
count=count+1 #Count message printed
print(float(count)/NumOfSeconds) #Average number of prints per second
Here calendar.timegm(time.gmtime()) gives us the time in seconds since the epoch (if you don't know what that is, read this. But basically it's just a fixed point in time most computer system now days use as a reference point.
So we set the EndTime to that point + the number of seconds we want to average over. Then, in a loop, we print the message we want to test and count the number of times we do that, between every iteration checking that we are not past the end time.
Finally we print the average number of times per seconds that we printed the message. This helps with the fact that we may end up start counting near the end of a whole second since the epoch, in which case we won't actually have a whole second to print messages, but just a fraction of that. If we make sure NumOfSeconds is large enough, that error addition will be small (for example, for NumOfSeconds=100 that error is ~1%).
We should note that the actual number would also depend on the fact that we are constantly testing the current time and counting the number of prints, however, while I haven't tested that here, it is usually the case that printing to the screen takes significantly longer time than those operations.

Datetime seconds are not accurate

I am trying to print the current time before my normal prints
global begtime
begtime = str(datetime.datetime.now()).split('.')[0]
global secondtime
secondtime = begtime.split(' ')[1]
global time
time = '[' + secondtime + ']' + ':'
print time
datetime.datetime.now returns in the format of :
year.month.date hour.minute.second
so I first split at the '.' to get the individual times, then I split at the space to get just the time.
then I formatted it as [hour:min:sec]
It works, but the time is not correct, it will print same time for all prints even if they happen minutes apart.
I want the exact time for every print.
For your code, you're probably setting the time earlier in your program and then accessing it later thinking it is generating a new time. It won't generate a new time every time you print it. It will only generate a new time every time you run begtime = str(datetime.datetime.now()).split('.')[0]
The better way to do this would be to use Date.strftime(format). I've included an example of this below.
import datetime
import time
now = datetime.datetime.now()
print(now.strftime('[%I:%M:%S]'))
time.sleep(10)
now = datetime.datetime.now()
print(now.strftime('[%I:%M:%S]'))
This will print the current time and then 10 seconds after the current time after 10 seconds.

condition parameter adjustment

I try to use 'IF' in python in order to achieve the algorithm that can automatically ajust the value of a parameter in 'IF' according to some stock trasactions.
if self.sellcount==0 and int(time.time())-self.programstarttime>600:
if cur_sum/total_sum>0.15:
Other Code
else:
if cur_sum/total_sum>0.35:
Other Code
I try to achieve that if my algorithm do not sell any stock for 10 minutes, the algorithm can automatically change the condition from 0.35 to 0.15. However, the code above will change from 0.15 to 0.35 after selling stocks for one time. I want the code to keep 0.15 after selling stocks for one time.
I'd like to start with a disclaimer to be careful, stock trading is not this easy and you can lose a lot of money with a simple algorithm (just as you can with a complex one)
However, this is also a nice example to understand how to deal with running a program over time in Python and understanding conditional logic.
There are a few basic constructs you'll want to know for this. The first concept is that to keep track of time constantly in your program, you likely want to put your code in an infinite loop. That will keep your programming doing what you want until you are done. This can be done like this:
while True:
Now that you have this setup, we just need to keep track of time. This can be done easily by setting a variable and incrementing it by how long you wait between iterations. However, we still need to track time. Python has a nice sleep function implemented in the time module. This function causes your program to pause for a number of seconds that you desire and then resume going through the rest of your code.
from time import sleep
last_sold_stock_time = 0
wait_time = 1
while True:
# <Condition Code goes here>
# This is in seconds
sleep(wait_time)
# Keep track of how much time has passed.
last_sold_stock_time += wait_time
Now, you just need to change your condition value based on the time. The full code will probably end up looking something like this:
from time import sleep
# The number of seconds since last bought a stock, assumes start is 0
last_sold_stock_time = 0
# This is in seconds
wait_time = 1
# ten minutes in seconds
ten_minutes = 600
while True:
# Figure out these values however you do
cur_sum = 0
total_sum = 1
if last_sold_stock_time <= ten_minutes:
condition_value = 0.35
else:
condition_value = 0.15
if cur_sum/total_sum > condition_value:
# Do something
pass
sleep(wait_time)
last_sold_stock_time += wait_time

How to start process 2hours before sunrise and stop 1 hour before sunset?

I'm checking time about every minute, but don't have a good way to check whether I'm in this 'on' mode of operation. I want to be 'on' 2hours before daylight up until 1 hour before sunset. If I continuously check using next_rising() and next_setting(), the moment the sun rises, my logic seems to fail because after that point it starts computing the sunrise for tomorrow. My is_daytime() is broken.
def is_daytime():
"""
Returns whether we should operate in the 'daytime' mode. Note that this
mode is shifted earlier to begin before sunrise and end before sunset.
"""
# Get the localized hour of the day
now = datetime.datetime.now()
# Get the next sunrise/sunset
here.date = now
sunrise = ephem.localtime(here.next_rising(ephem.Sun))
sunset = ephem.localtime(here.next_setting(ephem.Sun))
sunrise_shift = datetime.timedelta(hours=START_BEFORE_SUNSRISE_HR)
sunset_shift = datetime.timedelta(hours=END_BEFORE_SUNSET_HR)
# Return whether it is some amount of time before sunrise AND sunset
return ((sunrise - now) < sunrise_shift) and ((sunset - now) < sunset_shift)
Edit: Updated after reading solution
# Dependencies
import time
import datetime
import pytz
import ephem
# Choose your location for sunrise/sunset calculations
MY_TIMEZONE = "America/Los_Angeles"
MY_LONGITUDE = '37.7833' # +N
MY_LATITUDE = '-122.4167' # +E
MY_ELEVATION = 0 # meters
# Choose when to start and stop relative to sunrise and sunset
START_BEFORE_SUNSRISE_HR = 1
END_BEFORE_SUNSET_HR = 1
here = ephem.Observer()
def is_daytime():
"""
Returns whether we should operate in the 'daytime' mode. Note that this
mode is shifted earlier to begin before sunrise and end before sunset.
Assumes sunset NEVER comes after midnight
"""
# Get the localized hour of the day
now = datetime.datetime.now()
# Get the next sunrise/sunset
here.date = now
next_sunrise = ephem.localtime(here.next_rising(ephem.Sun()))
next_sunset = ephem.localtime(here.next_setting(ephem.Sun()))
sunrise_shift = datetime.timedelta(hours=START_BEFORE_SUNSRISE_HR)
sunset_shift = datetime.timedelta(hours=END_BEFORE_SUNSET_HR)
# If it's daytime
if (next_sunset < next_sunrise):
return (now < (next_sunset - sunset_shift))
# Otherwise it's nighttime
else:
return ((next_sunrise - sunrise_shift) < now)
def main():
# Configure the timezone
pytz.timezone(MY_TIMEZONE)
# Configure the ephem object
here.lat = MY_LATITUDE
here.lon = MY_LONGITUDE
here.elevation = MY_ELEVATION
while True:
if is_daytime():
print "It's daytime!"
time.sleep(60)
if __name__ == '__main__':
main()
the moment the sun rises, my logic seems to fail because after that point it starts computing the sunrise for tomorrow.
Think through the logic. What are the cases?
Before both sunrise and sunset. In that case, you only need to check <= sunrise-2H; the check for sunset-1H is irrelevant, but harmless.
Between sunrise and sunset. In that case, you only need to check <= sunset-1H; the check for sunrise-2H is not only irrelevant, but harmful.
After both sunrise and sunset. This is actually the same as the first case. (In San Francisco, sunset never comes after midnight and sunrise never comes before, but if you want your code to work in, say, Oulo, it might be a problem.)
So, how do you know which case you're in? Simple. If sunset > sunrise, you're in case 1 or 3; just check sunrise-2H; otherwise, you're in case 2, just check sunset-1H.
What if you go even farther north than Oulo? For example, in Rovaniemi, in early of June, the next sunset is a month away. Does that mean you want your program to stay on all month? Or that you want to pick an arbitrary time to start up and shut down (e.g., start 2 hours before "midnight" and end an hour after)? Whatever rule you come up with, I think ephem has enough data for you to write it. Or, if you don't know, at least test the code and see what rule it is applying, so you can document that. (I'm guessing people who live up there are used to looking at the docs for time-related programs like this…)

In Python, how can I put a thread to sleep until a specific time?

I know that I can cause a thread to sleep for a specific amount of time with:
time.sleep(NUM)
How can I make a thread sleep until 2AM? Do I have to do math to determine the number of seconds until 2AM? Or is there some library function?
( Yes, I know about cron and equivalent systems in Windows, but I want to sleep my thread in python proper and not rely on external stimulus or process signals.)
Here's a half-ass solution that doesn't account for clock jitter or adjustment of the clock. See comments for ways to get rid of that.
import time
import datetime
# if for some reason this script is still running
# after a year, we'll stop after 365 days
for i in xrange(0,365):
# sleep until 2AM
t = datetime.datetime.today()
future = datetime.datetime(t.year,t.month,t.day,2,0)
if t.hour >= 2:
future += datetime.timedelta(days=1)
time.sleep((future-t).total_seconds())
# do 2AM stuff
You can use the pause package, and specifically the pause.until function, for this:
import pause
from datetime import datetime
pause.until(datetime(2015, 8, 12, 2))
Slightly more generalized solution (based off of Ross Rogers') in case you'd like to add minutes as well.
def sleepUntil(self, hour, minute):
t = datetime.datetime.today()
future = datetime.datetime(t.year, t.month, t.day, hour, minute)
if t.timestamp() > future.timestamp():
future += datetime.timedelta(days=1)
time.sleep((future-t).total_seconds())
Another approach, using sleep, decreasing the timeout logarithmically.
def wait_until(end_datetime):
while True:
diff = (end_datetime - datetime.now()).total_seconds()
if diff < 0: return # In case end_datetime was in past to begin with
time.sleep(diff/2)
if diff <= 0.1: return
Building on the answer of #MZA and the comment of #Mads Y
One possible approach is to sleep for an hour. Every hour, check if the time is in the middle of the night. If so, proceed with your operation. If not, sleep for another hour and continue.
If the user were to change their clock in the middle of the day, this approach would reflect that change. While it requires slightly more resources, it should be negligible.
I tried the "pause" pacakage. It does not work for Python 3.x. From the pause package I extracted the code required to wait until a specific datetime and made the following def.
def wait_until(execute_it_now):
while True:
diff = (execute_it_now - datetime.now()).total_seconds()
if diff <= 0:
return
elif diff <= 0.1:
time.sleep(0.001)
elif diff <= 0.5:
time.sleep(0.01)
elif diff <= 1.5:
time.sleep(0.1)
else:
time.sleep(1)
adapt this:
from datetime import datetime, timedelta
from time import sleep
now = datetime.utcnow
to = (now() + timedelta(days = 1)).replace(hour=1, minute=0, second=0)
sleep((to-now()).seconds)
Slightly beside the point of the original question:
Even if you don't want to muck around with crontabs, if you can schedule python scripts to those hosts, you might be interested to schedule anacron tasks? anacron's major differentiator to cron is that it does not rely the computer to run continuously. Depending on system configuration you may need admin rights even for such user-scheduled tasks.
A similar, more modern tool is upstart provided by the Ubuntu folks: http://upstart.ubuntu.com/
This does not yet even have the required features. But scheduling jobs and replacing anacron is a planned feature. It has quite some traction due to its usage as Ubuntu default initd replacement. (I am not affiliated with the project)
Of course, with the already provided answer, you can code the same functionality into your python script and it might suit you better in your case.
Still, for others, anacron or similar existing systems might be a better solution. anacron is preinstalled on many current linux distributions (there are portability issues for windows users).
Wikipedia provides a pointer page: https://en.wikipedia.org/wiki/Anacron
If you do go for a python version I'd look at the asynchronous aspect, and ensure the script works even if the time is changed (daylight savings, etc) as others have commented already. Instead of waiting til a pre-calculated future, I'd always at maximum wait one hour, then re-check the time. The compute cycles invested should be negligible even on mobile, embedded systems.
Asynchronous version of Omrii's solution
import datetime
import asyncio
async def sleep_until(hour: int, minute: int, second: int):
"""Asynchronous wait until specific hour, minute and second
Args:
hour (int): Hour
minute (int): Minute
second (int): Second
"""
t = datetime.datetime.today()
future = datetime.datetime(t.year, t.month, t.day, hour, minute, second)
if t.timestamp() > future.timestamp():
future += datetime.timedelta(days=1)
await asyncio.sleep((future - t).total_seconds())
I know is way late for this, but I wanted to post an answer (inspired on the marked answer) considering systems that might have - incorrect - desired timezone + include how to do this threaded for people wondering how.
It looks big because I'm commenting every step to explain the logic.
import pytz #timezone lib
import datetime
import time
from threading import Thread
# using this as I am, check list of timezone strings at:
## https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
TIMEZONE = pytz.timezone("America/Sao_Paulo")
# function to return desired seconds, even if it's the next day
## check the bkp_time variable (I use this for a bkp thread)
## to edit what time you want to execute your thread
def get_waiting_time_till_two(TIMEZONE):
# get current time and date as our timezone
## later we remove the timezone info just to be sure no errors
now = datetime.datetime.now(tz=TIMEZONE).replace(tzinfo=None)
curr_time = now.time()
curr_date = now.date()
# Make 23h30 string into datetime, adding the same date as current time above
bkp_time = datetime.datetime.strptime("02:00:00","%H:%M:%S").time()
bkp_datetime = datetime.datetime.combine(curr_date, bkp_time)
# extract the difference from both dates and a day in seconds
bkp_minus_curr_seconds = (bkp_datetime - now).total_seconds()
a_day_in_seconds = 60 * 60 * 24
# if the difference is a negative value, we will subtract (- with + = -)
# it from a day in seconds, otherwise it's just the difference
# this means that if the time is the next day, it will adjust accordingly
wait_time = a_day_in_seconds + bkp_minus_curr_seconds if bkp_minus_curr_seconds < 0 else bkp_minus_curr_seconds
return wait_time
# Here will be the function we will call at threading
def function_we_will_thread():
# this will make it infinite during the threading
while True:
seconds = get_waiting_time_till_two(TIMEZONE)
time.sleep(seconds)
# Do your routine
# Now this is the part where it will be threading
thread_auto_update = Thread(target=function_we_will_thread)
thread_auto_update.start()
It takes only one of the very basic libraries.
import time
sleep_until = 'Mon Dec 25 06:00:00 2020' # String format might be locale dependent.
print("Sleeping until {}...".format(sleep_until))
time.sleep(time.mktime(time.strptime(sleep_until)) - time.time())
time.strptime() parses the time from string -> struct_time tuple. The string can be in different format, if you give strptime() parse-format string as a second argument. E.g.
time.strptime("12/25/2020 02:00AM", "%m/%d/%Y %I:%M%p")
time.mktime() turns the struct_time -> epoch time in seconds.
time.time() gives current epoch time in seconds.
Substract the latter from the former and you get the wanted sleep time in seconds.
sleep() the amount.
If you just want to sleep until whatever happens to be the next 2AM, (might be today or tomorrow), you need an if-statement to check if the time has already passed today. And if it has, set the wake up for the next day instead.
import time
sleep_until = "02:00AM" # Sets the time to sleep until.
sleep_until = time.strftime("%m/%d/%Y " + sleep_until, time.localtime()) # Adds todays date to the string sleep_until.
now_epoch = time.time() #Current time in seconds from the epoch time.
alarm_epoch = time.mktime(time.strptime(sleep_until, "%m/%d/%Y %I:%M%p")) # Sleep_until time in seconds from the epoch time.
if now_epoch > alarm_epoch: #If we are already past the alarm time today.
alarm_epoch = alarm_epoch + 86400 # Adds a day worth of seconds to the alarm_epoch, hence setting it to next day instead.
time.sleep(alarm_epoch - now_epoch) # Sleeps until the next time the time is the set time, whether it's today or tomorrow.
What about this handy and simple solution?
from datetime import datetime
import time
pause_until = datetime.fromisoformat('2023-02-11T00:02:00') # or whatever timestamp you gonna need
time.sleep((pause_until - datetime.now()).total_seconds())
from datetime import datetime
import time, operator
time.sleep([i[0]*3600 + i[1]*60 for i in [[H, M]]][0] - [i[0]*3600 + i[1]*60 for i in [map(int, datetime.now().strftime("%H:%M").split(':'))]][0])
Instead of using the wait() function, you can use a while-loop checking if the specified date has been reached yet:
if datetime.datetime.utcnow() > next_friday_10am:
# run thread or whatever action
next_friday_10am = next_friday_10am()
time.sleep(30)
def next_friday_10am():
for i in range(7):
for j in range(24):
for k in range(60):
if (datetime.datetime.utcnow() + datetime.timedelta(days=i)).weekday() == 4:
if (datetime.datetime.utcnow() + datetime.timedelta(days=i, hours=j)).hour == 8:
if (datetime.datetime.utcnow() + datetime.timedelta(days=i, hours=j, minutes=k)).minute == 0:
return datetime.datetime.utcnow() + datetime.timedelta(days=i, hours=j, minutes=k)
Still has the time-checking thread check the condition every after 30 seconds so there is more computing required than in waiting, but it's a way to make it work.

Categories

Resources