- 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 ctime() Method
The Python time ctime() method converts Python time to a string representing local time. The Python time refers to the time elapsed (in seconds) since the epoch of a system. This method accepts a floating-point number, referring to the number of seconds elapsed, as an argument and provides the local time in the form of a timestamp (or a string representation).
This timestamp will have the structure as follows: day, month, date, 24-hour format current local time (in HH-MM-SS order), and year. Since it is local time, the time returned by this method will depend on your geographical location.
Note: If the argument is not passed or is passed as None, the method uses the value returned by time() as its argument by default. Also, this method works similar to the asctime() method where the only difference occurs in the type of arguments given to these methods. Locale information is not used by ctime().
Syntax
Following is the syntax for the Python time ctime() method −
time.ctime([ sec ])
Parameters
sec − (Optional) These are the number of seconds to be converted into string representation.
Return Value
This method returns a string representation of time elapsed since the epoch in a system.
Example
The following example shows the usage of the Python time ctime() 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 the 24-character string.
import time ct = time.ctime() print("Current local time:", ct)
When we run above program, it produces following result −
Current local time: Mon Jan 9 16:25:37 2023
Example
If the argument passed is an integer representing seconds elapsed from the epoch of the system, the method returns the string 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". This method returns the time after the elapsed seconds.
import time # Passing the seconds elapsed as an argument to this method ct = time.ctime(1000) print("Time after elapsed seconds:", ct)
On executing the program above, the output is obtained as follows −
Time after elapsed seconds: Thu Jan 1 05:46:40 1970
Example
The ctime() method can also be used to get the epoch of a system.
The method is said to return the 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 based on the geographical location. If one needs to find the UTC time of the epoch, gmtime() method is used.
import time # Passing the seconds elapsed as an argument to this method ct = time.ctime(0) print("The epoch of this system:", ct)
Let us compile and run the program above, to produce the following result −
The epoch of this system: Thu Jan 1 05:30:00 1970
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 ctime() 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 ct = time.ctime(2.99) print("Time after elapsed seconds:", ct)
If we compile and run the program above, the output is as follows −
Time after elapsed seconds: Thu Jan 1 05:30:02 1970
To Continue Learning Please Login
Login with Google