Python Daytime Check

Basically, I want my script to pause between 4 and 5 in the morning. The only way I can do this I have come up with so far:

seconds_into_day = time.time() % (60*60*24) if 60*60*4 < seconds_into_day < 60*60*5: sleep(time_left_till_5am) 

Any β€œright” way to do this? Aka built-in / lib function for calculating time; and not just use seconds all the time?

+4
source share
4 answers

Python has a built-in datetime library: http://docs.python.org/library/datetime.html

This will probably help you after you:

 import datetime as dt from time import sleep now = dt.datetime.now() if now.hour >= 4 andnow.hour < 5: sleep((60 - now.minute)*60 + (60 - now.second)) 

OK, the above works, but here is a cleaner, less error prone solution (and what I originally thought about, but suddenly forgot how to do it):

 import datetime as dt from time import sleep now = dt.datetime.now() pause = dt.datetime(now.year, now.month, now.day, 4) start = dt.datetime(now.year, now.month, now.day, 5) if now >= pause and now < start: sleep((start - now).seconds) 

That, from where my original "timedelta" comment came from, what you get from subtracting two datetime objects is a timedelta object (which in this case draws the "second" attribute).

+2
source

Do you want datetime

The datetime module provides classes for managing dates and times in both simple and complex ways.

If you use date.hour from datetime.now() , you will get the current hour:

 datetimenow = datetime.now(); if datetimenow.hour in range(4, 5) sleep(time_left_till_5am) 

You can calculate time_left_till_5am by taking 60 - datetimenow.minute time_left_till_5am 60 and adding to 60 - datetimenow.second .

+3
source

The following code covers the more general case when a script needs to be paused for any fixed window lasting less than 24 hours. Example: must sleep from 11:00 to 01:00.

 import datetime as dt def sleep_duration(sleep_from, sleep_to, now=None): # sleep_* are datetime.time objects # now is a datetime.datetime object if now is None: now = dt.datetime.now() duration = 0 lo = dt.datetime.combine(now, sleep_from) hi = dt.datetime.combine(now, sleep_to) if lo <= now < hi: duration = (hi - now).seconds elif hi < lo: if now >= lo: duration = (hi + dt.timedelta(hours=24) - now).seconds elif now < hi: duration = (hi - now).seconds return duration tests = [ (4, 5, 3, 30), (4, 5, 4, 0), (4, 5, 4, 30), (4, 5, 5, 0), (4, 5, 5, 30), (23, 1, 0, 0), (23, 1, 0, 30), (23, 1, 0, 59), (23, 1, 1, 0), (23, 1, 1, 30), (23, 1, 22, 30), (23, 1, 22, 59), (23, 1, 23, 0), (23, 1, 23, 1), (23, 1, 23, 59), ] for hfrom, hto, hnow, mnow in tests: sfrom = dt.time(hfrom) sto = dt.time(hto) dnow = dt.datetime(2010, 7, 5, hnow, mnow) print sfrom, sto, dnow, sleep_duration(sfrom, sto, dnow) 

and here is the conclusion:

 04:00:00 05:00:00 2010-07-05 03:30:00 0 04:00:00 05:00:00 2010-07-05 04:00:00 3600 04:00:00 05:00:00 2010-07-05 04:30:00 1800 04:00:00 05:00:00 2010-07-05 05:00:00 0 04:00:00 05:00:00 2010-07-05 05:30:00 0 23:00:00 01:00:00 2010-07-05 00:00:00 3600 23:00:00 01:00:00 2010-07-05 00:30:00 1800 23:00:00 01:00:00 2010-07-05 00:59:00 60 23:00:00 01:00:00 2010-07-05 01:00:00 0 23:00:00 01:00:00 2010-07-05 01:30:00 0 23:00:00 01:00:00 2010-07-05 22:30:00 0 23:00:00 01:00:00 2010-07-05 22:59:00 0 23:00:00 01:00:00 2010-07-05 23:00:00 7200 23:00:00 01:00:00 2010-07-05 23:01:00 7140 23:00:00 01:00:00 2010-07-05 23:59:00 3660 
+1
source

When it comes to dates and times in Python, I still prefer mxDateTime over the Python datetime module, as the built-in interface has improved a lot over the years, but it's still pretty inconvenient and missing in comparison. Therefore, if you are interested, go here: mxDateTime It is free to download and use. Makes life a lot easier with datetime math.

 import mx.DateTime as dt from time import sleep now = dt.now() if 4 <= now.hour < 5: stop = dt.RelativeDateTime(hour=5, minute=0, second=0) secs_remaining = ((now + stop) - now).seconds sleep(secs_remaining) 
0
source

Source: https://habr.com/ru/post/1314684/


All Articles