- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Object & Classes
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python time localtime() Method
The Python time localtime() method converts the Python time to a local time. Python time is said to be the number of seconds that have been elapsed since the epoch of a system; whereas the local time is the time relative to the geographical location of the system. The local time is also represented by an object.
This method is similar to localtime() method and accepts a floating-point number as its optional argument. If it is not provided or is None, the current time as returned by the time() method is used. However, unlike the localtime() method, the dst flag in the object is set to 1 when DST is applied to the given time.
Syntax
Following is the syntax for the Python time localtime() method −
time.localtime([ sec ])
Parameters
sec − (Optional) A floating-point number representing the number of seconds elapsed.
Return Value
This method returns the time elapsed since the epoch in a system as an object.
Example
The following example shows the usage of the Python time localtime() method. We are not passing any values to the optional parameter of this method. Hence, the method takes the return value of the time() method as its argument by default. The method returns the current time as an object.
import time lt = time.localtime() print("Current local time:", lt)
When we run above program, it produces following result −
Current local time: time.struct_time(tm_year=2023, tm_mon=1, tm_mday=9, tm_hour=12, tm_min=28, tm_sec=40, tm_wday=0, tm_yday=9, tm_isdst=0)
Example
If the argument passed is an integer representing seconds elapsed from the epoch of the system, the method returns the object representation of the date after the elapsed time.
In the example below, we are trying to find the date after 1000 seconds from the epoch. The epoch of the system this program is executed in, is "Thu Jan 1 05:30:00 1970" in local time. This method returns the time after the elapsed seconds.
import time # Passing the seconds elapsed as an argument to this method lt = time.localtime(1000) print("Python local time:", lt)
Python local time: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=46, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)
Example
The localtime() method can also be used to get the epoch of a system, in local time.
The method is said to return the local time after the given elapsed seconds, calculated based on the epoch of a system. Thus, if we pass the argument as '0', the method returns the epoch of the system.
import time # Passing the seconds elapsed as an argument to this method lt = time.localtime(0) print("The epoch of the system:", lt)
Let us compile and run the program above, to produce the following result −
The epoch of the system: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=30, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
Example
This method does not consider the fractional part of the floating-point number arguments that represent elapsed seconds.
Let us pass 2.99 seconds as an argument to the localtime() method. Even if the number is almost equal to 3 seconds, the method is expected to completely ignore the fractional part and consider the argument as only 2 seconds. It is shown in the example below.
import time # Passing the seconds elapsed as an argument to this method lt = time.localtime(2.99) print("Time after elapsed seconds:", lt)
If we compile and run the program above, the output is as follows −
Time after elapsed seconds: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=30, tm_sec=2, tm_wday=3, tm_yday=1, tm_isdst=0)