Python Time Module
Anonymous contributor
Published Apr 19, 2022
Contribute to Docs
Python has a built-in time module that can be used for time related tasks. This module utilizes a number of different standards and objects that may be unfamiliar such as:
- The epoch refers to a reference point in time from which calculations can be made. It is designated as midnight on January 1, 1970.
- There are functions that will return values formatted using Daylight Savings Time (DST).
- There is a specific time data type called
struct_time.
The following functions are provided by this module.
Time Module
- .ctime()
- Takes a time, expressed in seconds elapsed since the epoch, and returns a formatted string.
- .gmtime()
- Converts a time expressed in seconds since the epoch to a `struct_time` in UTC.
- .localtime()
- Converts a time expressed in seconds since the epoch to the local time.
- .perf_counter_ns()
- Returns the value of a high-resolution performance counter in nanoseconds.
- .strftime()
- Returns a time as a string based on one or more formatting codes.
- .strptime()
- Parses a string representing a date/time into a struct_time object according to a specified format.
- asctime()
- Converts a time tuple or `struct_time` to a 24‑character human‑readable string.
- perf_counter()
- Returns a high-resolution timer value, useful for measuring short durations, including time elapsed during sleep.
- sleep()
- Suspends execution of the current thread for a specified number of seconds
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours