The “Python In Action – Building a Stock Based Quantitative Trading System” booklet focuses on Python in action, but it provides a pre-introductory chapter to help readers quickly grasp the use of basic tools. Therefore, the booklet is suitable for people with only the most basic programming experience in Python.

In the meantime, we will continue to update some basic articles on Python and quantization to help you lay a solid foundation. Let’s look at Python’s time module.

The built-in modules in Python that provide time and date processing are Time, DateTime, and Calendar.

Most of the functions in the Time module call functions of the same name in the PLATFORM C library, so they are more dependent on the operating system level. Therefore, some functions in the Time module are platform-related and may have different effects on different platforms. Special attention should be paid to this, that is, the functions of the Time module are not applicable to all platforms.

You need to import the time module, as shown below:

import time
Copy the code

There are three formats of time representation in the Time module:

  • Timestamp indicates the timestamp. The timestamp represents an offset in seconds from the epoch, and any operating system can run time.gmtime(0) to find the epoch for this system. The maximum limit date for the timestamp depends on the date supported by the C library in the system, which is 2038 for 32-bit systems. If you need to process dates outside the range described, you need to consider using the Datetime module. As follows:
# Find a new era for this system
print(time.gmtime(0))
#time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

Generate timestamp of current time
print(time.time())# 1556973222.546307
Copy the code
  • Struct_time time tuple: gmtime(), localtime() and strptime() are all returned as time primitives. As follows:
# generate struct_time
print(time.localtime())#time.struct_time(tm_year=2019, tm_mon=5, tm_mday=11, tm_hour=12, tm_min=20, tm_sec=58, tm_wday=5, tm_yday=131, tm_isdst=0)
Copy the code

Struct_time group attributes and values are as follows:

  • Format time Format time. The format structure can make the time more readable. There are two main formats: custom format and fixed format. For example:
# generate format_time
# Generate custom format time representation format
print(time.strftime("%Y-%m-%d %X",time.localtime()))# 2019-05-04 20:40:01
# Generate fixed format time representation format
print(time.asctime(time.localtime()))#Sat May 11 19:45:16 2019
print(time.ctime(time.time()))#Sat May 11 19:45:16 2019
print(time.ctime(time.time()+10))#Sat May 11 19:45:26 2019
Copy the code

Format time Indicates the structured format.

The three time formats timestamp, struct_time and format time in the time module are converted in the following way:

# struct_time to timestamp note: time localtime () - struct_time
print(time.mktime(time.localtime()))
# 1556975223.0

# timestamp to struct_time GMT note:time.time() -- timestamp
print(time.gmtime(time.time()))
#time.struct_time(tm_year=2019, tm_mon=5, tm_mday=11, tm_hour=4, tm_min=20, tm_sec=58, tm_wday=5, tm_yday=131, tm_isdst=0)
Copy the code

#format_time to struct_time
print(time.strptime('the 2011-05-05 16:37:06'.'%Y-%m-%d %X'))
#time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=3, tm_yday=125, tm_isdst=-1)

#struct_time to format_time
print(time.strftime("%Y-%m-%d %X"))# 2019-05-11 08:45:48
print(time.strftime("%Y-%m-%d %X",time.localtime()))# 2019-05-11 08:45:48

Copy the code

As mentioned above about time.asctime() and time.ctime(), it is possible to generate fixed format time from struct_time and timestamp time formats respectively.

The time module has the following functions for processing system time:

Time.clock () returns the current CPU elapsed time in seconds, which is used to measure the elapsed time of different programs and is more practical than time.time(). However, it is not recommended after Python3.3 because it is operating system dependent and is officially recommended to use per_counter(return system running time) or process_time(return process running time) instead. As follows:

print(time.clock())# 0.221209
#DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead

Copy the code

Time.perf_counter () returns the running time of the system (the precise time of the timer), including the sleep time of the entire system. Since the reference point of the return value is undefined, only the difference between the results of successive calls is valid. As follows:

print(time.perf_counter())# 4.373607855
time.sleep(5)
print(time.perf_counter())# 9.374290978
Copy the code

Time.process_time () returns the total amount of CPU time for the current process, excluding sleep time. Since the reference point of the return value is undefined, only the difference between the results of successive calls is valid. As follows:

print(time.process_time())# 0.385954
time.sleep(5)
print(time.process_time())# 0.385982Time.sleep (secs) delays the execution of the calling thread in seconds. Time.sleep (5)Copy the code

About the complete code can join the pamphlet exchange group to obtain. More quantitative trading content welcome everyone to subscribe to read the booklet!!