Python time asctime() Method



Before we discuss the working of the Python time asctime() method, let us understand the different ways time is represented in.

In Python time module, time can be represented in two simple ways: as a tuple or as an object. A tuple is more abstract; it stores the date and time in an order where each element of the tuple represents a specific element of time. For example, (2023, 1, 9, 10, 51, 77, 0, 9, 0) denotes the time "Mon Jan  9 10:51:77 2023". But here, one cannot decipher which element denotes what just by looking at the tuple. This is why an object is introduced to have a clearer understanding of these elements.

An object, usually represented as struct_time, defines various fields to hold various elements of time. Hence, it becomes easier to access them. For example, an object represents the time as (tm_year=2023, tm_mon=1, tm_mday=9, tm_hour=10, tm_min=51, tm_sec=77, tm_wday=0, tm_yday=9, tm_isdst=0).

The asctime() method converts this tuple or struct_time representing a certain time, either in UTC or local time, to a 24-character string of the following form: 'Mon Jan  9 10:51:77 2023'. In this string, the day field is allotted two characters, so if its a single-digit day, space is padded before it.

However, unlike the asctime() C function, this method does not add a trailing newline character to the string.

Syntax

Following is the syntax for the Python time asctime() method −

time.asctime([t]))

Parameters

  • t − (Optional) This is a tuple of 9 elements or struct_time representing either a UTC time or a local time.

Return Value

This method returns 24-character string of the following form: 'Tue Feb 17 23:21:05 2009'.

Example

The following example shows the usage of the Python time asctime() method. In here, we are trying to represent the current local time of India, returned by the localtime() method, in a 24-character string format using the asctime() method.

import time

t = time.localtime()
lc = time.asctime(t)
print("Current local time represented in string:", lc)

When we run above program, it produces following result −

Current local time represented in string: Mon Jan  9 11:07:30 2023

Example

This method also converts the UTC time into a string format.

In this example, the current UTC time is retrieved using the gmtime() method and this UTC time is passed as an argument to the asctime() method. The current time is converted from the struct_time object to the 24-character string.

import time

t = time.gmtime()
gm = time.asctime(t)
print("Current UTC time represented in string:", gm)

Let us compile and run the program above, to produce the following result −

Current UTC time represented in string: Mon Jan  9 06:05:52 2023

Example

If no argument is passed to this method, the resultant string represents the local time by default.

import time

ct = time.asctime()
print("Current time represented in string is", ct)

Current time represented in string is Mon Jan  9 11:40:07 2023

Example

The return values obtained from the localtime() and gmtime() methods are in the form of a struct_time object; to pass the argument of this method as a tuple, it has to be created manually as shown in the example below.

import time

# Create a tuple representing the current local time
t = (2023, 1, 9, 11, 40, 57, 0, 9, 0)

# Pass this tuple as an argument to the method
ct = time.asctime(t)

# Display the string output
print("Current local time represented in string", ct)

Current local time represented in string Mon Jan  9 11:40:57 2023
python_date_time.htm
Advertisements