Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Module Time in Python

Understand Module Time in Python with clear background, essential details, and practical takeaways.

Table of Contents

This updated guide examines Module Time in Python and organizes the essential facts, background, and practical takeaways in clear American English.

Python has a time module used to handle time-related tasks . To use the functions defined in the module, first import this module, do the following:

import time

Here are the most commonly used time-related functions.

time.time ()

The time () function returns the number of seconds from the epoch, or the timestamp value

For Unix systems, 00:00:00 on January 1, 1970 UTC hours are called epochs (the start of time).

import time seconds = time.time() print("So giay tinh tu epoch:", seconds)

The result will look like:

So giay tinh tu epoch: 1562922590.7720907

time.ctime ()

This method converts a time represented by the number of seconds from the epoch to a string representation.

import time # số giây tính từ epoch # viet boi TipsMake.com seconds = 1562983783.9618232 local_time = time.ctime(seconds) print("Local time:", local_time)

Run the program, the result returns the date and time corresponding to the number of seconds transmitted:

Local time: Sat Jul 13 09:09:43 2019

If not passing seconds, the program returns the current time value.

time.sleep ()

The sleep () function stops executing the current thread in the number of seconds passed.

import time print ("Start:", time.ctime()) time.sleep(3) print ("End:", time.ctime())

This method does not return any values, but only the executable delay. Try running the program to see the delay.

Start: Sat Jul 13 09:33:52 2019 End: Sat Jul 13 09:33:57 2019

Before going on to other time-related functions, learn through class time.struct_time .

Class time.struct_time

Some functions in module time, such as gmtime (), asctime (). have time.struct_time is the returned object.

Example of time.struct_time result .

time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=6, tm_min=35, tm_sec=17, tm_wday=3, tm_yday=361, tm_isdst=0)

IndexPropertiesDescribe 0tm_yearCurrent year: 0000,., 2018,., 9999 1tm_monCurrent month: 1, 2,., 12 2tm_mdayCurrent day: 1, 2,., 31 3tm_hourCurrent time: 0, 1,., 23 4tm_minCurrent time: 0, 1,., 59 5tm_secCurrent seconds: 0, 1,., 61 6tm_wdayDay of the week: 0, 1,., 6; Monday is calculated at 0 7tm_ydayDay of the year: 1, 2,., 366 8tm_isdstDetermine DST: 0, 1 or -1

time.localtime ()

The localtime () function in the time module takes the number of seconds passed into the argument and returns struct_time in local time.

import time result = time.localtime(1562983783) print("Ket qua:", result) print("nNam:", result.tm_year) print("Gio:", result.tm_hour)

Run the program, the result is:

Ket qua: time.struct_time(tm_year=2019, tm_mon=7, tm_mday=13, tm_hour=9, tm_min=9, tm_sec=43, tm_wday=5, tm_yday=194, tm_isdst=0) Nam: 2019 Gio: 9

If no number of seconds is provided or the value is transferred, the current time returned from the time () function will be used.

time.gmtime ()

The function gmtime () in the time module takes the number of seconds passed as an argument and returns the struct_time by UTC time.

import time result = time.gmtime(1562983783) print("Ket qua:", result) print("nNam:", result.tm_year) print("Gio:", result.tm_hour)

Run the program, the result is:

Ket qua: time.struct_time(tm_year=2019, tm_mon=7, tm_mday=13, tm_hour=2, m_min=9, tm_sec=43, tm_wday=5, tm_yday=194, tm_isdst=0) Nam: 2019 Gio: 2

If no number of seconds is provided or the value is transferred, the current time returned from the time () function will be used.

time.mktime ()

The mktime () function in the time module takes struct_time (or a tuple containing 9 elements corresponding to struct_time) as the argument and returns the number of seconds from the local time epoch. This is the inverse function of localtime ().

import time t = (2019, 7, 13, 9, 9, 43, 5, 194, 0) local_time = time.mktime(t) print("Gio dia phuong:", local_time)

Run the program, the result is:

Gio dia phuong: 1562983783.0

The example below shows how mktime () and localtime () are related.

import time seconds = 1562983783 # trả về struct_time # viet boi TipsMake.com t = time.localtime(seconds) print("t1: ", t) # trả về giây từ struct_time s = time.mktime(t) print("ns:", seconds)

Result:

t1: time.struct_time(tm_year=2019, tm_mon=7, tm_mday=13, tm_hour=9, tm_min=9, tm_sec=43, tm_wday=5, tm_yday=194, tm_isdst=0) s: 1562983783

time.asctime ()

The asctime () function in the time module takes struct_time (or a tuple containing 9 elements corresponding to struct_time) as the argument and returns a string representing that time.

import time t = (2019, 7, 13, 9, 9, 43, 5, 194, 0) result = time.asctime(t) print("Ket qua:", result)

Program results returned:

Ket qua: Sat Jul 13 09:09:43 2019

time.strftime ()

The strftime () function in the time module takes struct_time (or a tuple corresponding to struct_time) as the argument and returns a string representing the time based on the input format code.

import time named_tuple = time.localtime() # lấy struct_time time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple) print(time_string)

Run the program, the result is:

07/15/2019, 08:46:58

In this example,% Y,% m,% d,% H are format codes .

  • % Y: year [0001,., 2018, 2019,., 9999]
  • % m: month [01, 02,., 11, 12]
  • % d: date [01, 02,., 30, 31]
  • % H: hour [00, 01,., 22, 23
  • % M: month [00, 01,., 58, 59]
  • % S: seconds [00, 01,., 58, 61]

: strftime function () in the datetime Python module

time.strptime ()

The strptime () function in the time module analyzes a string representing a time, time, and returns struct_time.

import time time_string = "17 July, 2019" result = time.strptime(time_string, "%d %B, %Y") print(result)

The returned result has the form:

time.struct_time(tm_year=2019, tm_mon=7, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=198, tm_isdst=-1)

: strptime () function in the datetime Python module

Previous lesson: Convert the timestamp value in Python

Next lesson: Function sleep () in Python

FAQ

What is Module Time in Python about?

It provides a structured overview of Module time, explains the main context, and highlights practical takeaways for readers.

Why does this topic matter?

Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.

How should readers use this information?

Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.