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
python_date_time.htm
Advertisements