Python Gets the implementation code for a random, non-repeating point in time for a specified period

Source: Internet
Author: User
This article describes the implementation code for Python to get random, non-repeating points of time over a specified period of time

Scenario 1: Take a random time between N 07:30:00-09:30:33.

Here's My Code:

#2016 -12-10 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) * + 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 (r  Ange (sts,ets), #rt = = [28931, 29977, 33207, 33082, 31174, 30200, 27458, 27434, 33367, 30450]rt.sort () #对时间从小到大排序for R in RT:    Print (Seconds2time (R)) "" "Output: 0 7:43:1207:54:3108:08:3308:27:4608:46:5308:48:1708:55:2008:59:1609:10:2309:15:58 "" "

From the code can be found that the idea is to turn the time into seconds, then you can use the range of the time between the number of seconds between 07:30-09:30, and then use Random.sample to take out a number of n seconds, and then turn the number of seconds to the desired time format.

Scenario 2: Time-size comparisons and time-range judgments

>>> "09:30:00" > "9:30:00" false>>> "09:30:00" = = "9:30:00" False

string-based judgments may appear like above, and I feel that unification turns into numbers and then computes more reliably.

Scenario 3: Time stamp that is exactly equal to January 1, 1970 after the time is converted to a number of seconds

Unix time, or POSIX time, is the time representation used by UNIX or Unix-like systems: The total number of seconds from January 1, 1970 0:0 0 seconds to the current UTC.

Any time that is 24 hours in a day is converted to a number of seconds, which is exactly equal to the timestamp of UTC January 1, 1970. So if you need to, you can use the built-in timestamp function in the programming language to convert.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.