Module Time in Python

Tram Ho

Hello everyone in this article, I will introduce you to Mobule Time in Python, I will explain to you what their concept is, their syntax and usage. You can find out in my article!


In this article, we will learn more about the Time module. We will learn how to use the various time related functions identified in the time module and examples.

Python has a module named time to handle time -related tasks. To use functions defined in the module, we need to import the module first.


Below are the most commonly used time related functions

Python time.time ()

The time () function returns the detailed time to the number of seconds.


Python time.ctime ()

The time.ctime () function time.ctime () time as an argument and returns a string representing the current time.

If you run the program, the results will be as follows:


Python time.sleep ()

The sleep () function pauses the current thread for a certain time (the time is in seconds).


Python time.localtime ()

The localtime () function takes the number of seconds passed as an argument and returns struct_time in struct_time with the current local time.

When you run the program, the results will be as follows:


Python time.gmtime ()

The gmtime () function takes the number of seconds passed as an argument and returns struct_time will be UTC .

When you run the program, the results will be as follows:


Python time.mktime ()

The mktime () function will take struct_time (or a tuple containing 9 elements corresponding to struct_time ) as an argument and return the number of seconds elapsed in local time. It will be essentially the inverse of the localtime () function.


I will have the example below to show how mktime () and localtime () are related to each other.

When you run the program, the results will be as follows:


Python time.asctime ()

The asctime () function will take struct_time (or a tuple containing 9 elements corresponding to struct_time ) as an argument and return the specified detail time in the format from the date to the current time in seconds.

When you run the program, the results will be as follows:


Python time.strftime ()

The strftime () function takes struct_time (or its corresponding tuple) as an argument and returns the value of the time based on the format you specify.

When you run the program, the results will be as follows:

Below %Y , %m , %d , %H will be specified.

  • %Y : The year will be formatted as [0001, …, 2018, 2019, …, 9999]
  • %m : Month will be formatted as [01, 02, …, 11, 12]
  • %d : The date will be formatted as [01, 02, …, 30, 31]
  • %H : The hour will be formatted [00, 01, …, 22, 23]
  • %M : Minutes will be formatted [00, 01, …, 58, 59]
  • %S : Seconds will be formatted [00, 01, …, 58, 59]

Conclude

Below I have introduced to you about Module Time as well as some frequently used Time functions in Python. If you have any questions, please leave a comment below.


See more details

https://www.programiz.com/python-programming/time

Share the news now

Source : Viblo