Python Time ModuleWith the help of practical examples, we will study the Python time module and the different functions supplied by this module in this tutorial. As its name suggests, the Python time module enables working with time in Python. It provides features like obtaining the current datetime and the functionality to delay the programme's execution. We must import this module before we can begin using its functionality. Importing time moduleIt is not necessary to install the Python time module outside because it is included in the standard utility module of Python. Using the import command, we can import it quickly. Python Time in Seconds as a Floating Point NumberThe first thing that the time.time() function returns are the number of passed seconds since the epoch. The time returned by this function is a floating data type to account for the fractional seconds. Code Output: 1659877305.6220002 The reference point to determine the epoch may vary considerably on your machine. Therefore, you might get a different answer. There are several benefits to measuring time in seconds:
However, there are situations when you would wish to view the current time displayed as a string object. You can achieve this by feeding the number of seconds returned by the time() function into the time.ctime() function. The time.ctime() functionality of the time module accepts time in seconds as an input and calculates time up to those seconds before returning a 24-character time string object. Time is computed up until the current moment if there is no argument. Code Output: Current time: Sun Aug 7 18:31:45 2022 The Epoch We discovered in the last section that we could get datetime in Python as a floating point object that indicates the amount of time that has passed from the start of an epoch. We describe an epoch as:
Here, it's crucial to understand that, while working with Python datetime, you're thinking about a time period denoted by a reference point. This reference point is referred to as the epoch in computing. Therefore, the epoch serves as the baseline for gauging the progression of time. For instance, we can depict the midnight of April 2, 2022, IST as the epoch. Then 86400 seconds must have passed since the epoch if we find time on midnight of April 3 2022 There are 60 seconds in one minute, 60 minutes in one hour, and 24 hours in one day. Therefore, we can manually calculate the seconds passed between two times. Code Output: 86400 Remembering that we can still depict time before the epoch is crucial, and the result will be negative in this case. Using an epoch of April 2, 2022, we might depict midnight of April 1, 2022, IST as -86400 seconds. Generally, on January 1, 1970, UTC is a frequently used epoch, but it is not the only one. Various filesystems, operating systems, and APIs occasionally employ various epochs. UNIX systems specify its epoch as January 1, 1970. On the other hand, the Win32 API defines its epoch as January 1, 1601. To determine the epoch on your system, we can use the time.gmtime() function of the time module: Code Output: 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) 1970-01-01 00:00:00 We can see from the above program that the epoch displayed using the gmtime() function is 1 January 1970 for the system we ran it on. As there are 86400 seconds in a day, this implies that the date 2 January 1970 can be represented as 86400 seconds since the system's epoch. Note: Although it will be negative, we may still express the time before the epoch in seconds. For instance, we will use -86400 seconds to represent 31 December 1969.Delaying Execution of ProgramsExecution can be delayed using time.sleep() method. We use this function to halt the execution of the program for the time specified in the arguments. Code Output: Fri Aug 12 07:32:46 2022 0 2 4 6 8 Fri Aug 12 07:33:16 2022 time.struct_time ClassAccording to the description of the struct time class of the time module of Python, it aids in accessing local time, mostly non-epochal timestamps. The output is often a namedtuple object, where the values are determined by the name and index of the attributes. The following list includes the many characteristics linked to the struct_time object:
This class provides many functions; let us look at some of them. time.localtime() MethodThe struct time object is returned by the localtime() function representing the local time. It accepts as a parameter the number of seconds that have passed since the epoch. We got the current time in seconds using the time() function, which we passed to the localtime() function. It gave a struct_time object which we converted into standard datetime. Code Output: time.struct_time(tm_year=2022, tm_mon=8, tm_mday=7, tm_hour=23, tm_min=0, tm_sec=57, tm_wday=6, tm_yday=219, tm_isdst=0) 2022-08-07 23:00:57 time.mktime() MethodThe inverse of the time.localtime() function is the time.mktime() function. The time is represented as a time.struct_time object by the localtime() function is converted into the number of seconds passed since the epoch using the time.mktime() function. OverflowError or ValueError will be generated if the input time cannot be interpreted as a valid time (this depends on whether the invalid time value is passed to the function or the underlying C libraries). Platform-specific limitations apply to the oldest date up to which the function can produce a time. Code Output: Struct_time object: time.struct_time(tm_year=2022, tm_mon=8, tm_mday=7, tm_hour=17, tm_min=38, tm_sec=29, tm_wday=6, tm_yday=219, tm_isdst=0) Local time (in seconds): 1659874109. time.strftime() MethodThe function time.strftime() converts a namedtuple or struct time object representing the time provided by the gmtime() or localtime() function into a string object in the format specified as an argument. If t is not specified, the localtime() value for the current time is used. The format argument must be a string. If any element in t is outside the acceptable range, ValueError is raised. Code Output: Sun, Aug 07 22 17:48:21s The format string can contain the following commands. The strftime() output replaces them with the specified characters and displays them.
time.asctime() MethodTo convert a tuple or a struct time object expressing the time returned by the functions time.gmtime() or time.localtime() to a string object of the following format, we can use the time.asctime() function: Day Month Date Hour:Mid:Second Year Code Output: Struct_time object: time.struct_time(tm_year=2022, tm_mon=8, tm_mday=8, tm_hour=12, tm_min=24, tm_sec=37, tm_wday=0, tm_yday=220, tm_isdst=0) Time in string: Mon Aug 8 12:24:37 2022 Struct_time object: time.struct_time(tm_year=2022, tm_mon=8, tm_mday=8, tm_hour=17, tm_min=54, tm_sec=37, tm_wday=0, tm_yday=220, tm_isdst=0) Time in string: Mon Aug 8 17:54:37 2022 In this tutorial, we learned all the functions the time module of Python provides. We need to remember to use these functionalities. We need to first import the library into our program. More information about the module can be obtained from the documentation of the module.
Next TopicSklearn Linear Regression Example
|