This article introduces python's implementation code for obtaining random and non-repetitive time points within a specified period of time.
This article introduces python's implementation code for obtaining random and non-repetitive time points within a specified period of time.
Scenario 1: Take N random times between 07:30:00 and 09:30:33.
The following is my code:
#7:06:29 codegayimport randomst = "07:30:00" et = "09:30:33" def time2seconds (t): h, m, s = t. strip (). split (":") return int (h) * 3600 + int (m) * 60 + int (s) def seconds2time (sec): m, s = pmod (sec, 60) h, m = pmod (m, 60) return "% 02d: % 02d: % 02d" % (h, m, s) sts = time2seconds (st) # sts = 27000ets = time2seconds (et) # ets = 34233rt = random. sample (range (sts, ets), 10) # rt = [28931,299 77, 33207,330 82, 31174,302 00, 27458,274 34, 33367,304 50] rt. sort () # sort time in ascending order for r in rt: print (seconds2time (r) "output: 07: 43: 1207: 54: 3108: 08: 3308: 27: 4608: 46: 5308: 48: 1708: 55: 2008: 59: 1609: 10: 2309: 15: 58 """
In the code, we can find that the train of thought is to convert the time into seconds, then we can use range to generate the time seconds between-, and then use random. sample extracts N seconds from the sample, and finally converts the seconds to the required time format.
Scenario 2: Time size comparison and time range determination
>>> "09:30:00" > "9:30:00"False>>> "09:30:00" == "9:30:00"False
String-based judgment may be like the above situation. I feel that computation is more reliable after unified conversion into numbers.
Scenario 3: After the time is converted to seconds, it is exactly the same as the timestamp of January 1, January 1, 1970.
UNIX time, or POSIX time, is the time representation used by UNIX or UNIX-like systems: the total number of seconds since 00:00:00, January 1, January 1, 1970, Coordinated Universal Time.
Any time within 24 hours of the current day is converted to the timestamp of UTC January 1, 1970. If necessary, you can use the timestamp function built in the programming language for conversion.
The above is the detailed content of the implementation code for python to obtain random and non-repetitive time points within a specified period of time. For more information, see other related articles in the first PHP community!