1. Purpose & Ideas
The timestamp of this secondary construction , There are mainly 2 Uses :
headers It is necessary to transmit the... Corresponding to the current time 13 position ( millisecond ) Time stamp
Query to obtain data in a certain period of time ( Such as 30 Days ago, ~ current time )
What to do next :
Get current date , Such as 2021-12-16, Set the end time
Set the time offset , obtain 30 The date corresponding to the day before , Set the start time
Convert the start time and end time to a timestamp
2. An easy to understand example
According to the above thinking , The creation process of timestamp parameters is as follows
`import datetime
today = datetime.datetime.now() # Get today's time print(" The current date is :{}".format(today)) end_time = int(round(today.timestamp()*1000)) # Take today's time as the query end time , And turn to 13 A time stamp (int() Indicates that the integer part is reserved ) offset = datetime.timedelta(days=-30) # Define offset , That is, the time interval from the current time start_time = int(round((today + offset).timestamp()*1000)) # Define query start time = Current time fallback 30 God , Turn to time stamp print(" The start date is :{}, The corresponding time stamp :{}".format(today + offset, start_time)) print(" The end date is :{}, The corresponding time stamp :{}".format(today, end_time)
Print the results
The current date is :2021-12-16 16:50:58.543452
The start date is :2021-11-16 16:50:58.543452, The corresponding time stamp :1637052658543
The end date is :2021-12-16 16:50:58.543452, The corresponding time stamp :1639644658543
Find a time stamp conversion website , Check whether the timestamp of the above generated start date corresponds to the original date
You can see it , It can roughly correspond to ( Many people on the Internet use round() The method is rounded , Because I don't have such high requirements for accuracy , So just round it up )
It should be noted that :timestamp() Method generates by default 10 position ( Second level ) Time stamp , If you want to convert to 13 position ( millisecond ) Words , Result *1000 Talent
Add timedelta Several parameters of
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0