El método de tiempo de Python time.asctime() se usa para convertir una tupla o un objeto time.struct_time que representa un tiempo devuelto por el método time.gmtime() o time.localtime() en una string de la siguiente forma:
Day Mon Date Hour:Min:Sec Year For example: Thu 08 22 10:46:56 2019
tiempo.asctime([t])
Parámetro:
t (opcional): una tupla u objeto time.struct_time que representa una hora devuelta por el método time.gmtime() o time.localtime() . Si no se proporciona el parámetro ‘t’ o Ninguno, entonces se usa la hora actual devuelta por el método time.localtime().
Tipo de devolución:
Este método devuelve una string con el formato Día Lun Fecha Hora:Min:Seg Año
Ejemplo de tiempo de Python asctime()
Ejemplo 1: Uso del método time.asctime()
Python3
# Python program to explain time.asctime() method # importing time module import time # Convert the current time # as returned by time.time() method # to a time.struct_time object in UTC # using time.gmtime() method obj = time.gmtime() # Print time.struct_time object print("time.struct_time object as returned by time.gmtime() method:") print(obj) # Convert the time.struct_time # object to a string of the # form 'Day Mon Date Hour:Min:Sec Year' # using time.asctime() method time_str = time.asctime(obj) # Print the time.struct_time object # to the string of the # form 'Day Mon Date Hour:Min:Sec Year' print("\ntime.struct_time obj in string of the form \ 'Day Mon Date Hour:Min:Sec Year':") print(time_str) # Convert the current time # as returned by time.time() method # to a time.struct_time object in local time # using time.localtime() method obj = time.localtime() # Print time.struct_time object print("\ntime.struct_time object as returned by \ time.localtime() method:") print(obj) # Convert the time.struct_time # object to a string of the # form 'Day Mon Date Hour:Min:Sec Year' # using time.asctime() method time_str = time.asctime(obj) # Print the time.struct_time object # to the string of the # form 'Day Mon Date Hour:Min:Sec Year' print("\ntime.struct_time obj in string of the form \ 'Day Mon Date Hour:Min:Sec Year':") print(time_str)
Producción:
objeto time.struct_time devuelto por el método time.gmtime():
time.struct_time(tm_year=2021, tm_mon=10, tm_mday=7, tm_hour=15, tm_min=30, tm_sec=51, tm_wday=3, tm_yday=280, tm_isdst=0)
time.struct_time obj en una string de la forma ‘Día Lun Fecha Hora:Min:Seg Año’:
jue oct 7 15:30:51 2021
objeto time.struct_time devuelto por el método time.localtime():
time.struct_time(tm_year=2021, tm_mon=10, tm_mday=7, tm_hour=21, tm_min=0, tm_sec=51, tm_wday=3, tm_yday=280, tm_isdst=0)
time.struct_time obj en una string de la forma ‘Día Lun Fecha Hora:Min:Seg Año’:
jue oct 7 21:00:51 2021
Ejemplo 2: si no se proporciona el parámetro ‘t’
Python3
# Python program to explain time.asctime() method # importing time module import time # If the parameter 't' # in time.asctime() method # is not provided then # the current time as # returned by time.localtime() # method is used # Convert the current time # as returned by time.localtime() method # to a string of the # form 'Day Mon Date Hour:Min:Sec Year' # using time.asctime() method time_str = time.asctime() # Print the string print(time_str)
Producción:
Thu Oct 7 21:01:45 2021
Ejemplo 3: Si el parámetro ‘t’ es una tupla
Python3
# Python program to explain time.asctime() method # importing time module import time # A tuple with 9 attributes # representing a time t = (2019, 8, 22, 11, 21, 48, 3, 234, 0) # Convert the tuple # to a string of the # form 'Day Mon Date Hour:Min:Sec Year' # using time.asctime() method time_str = time.asctime(t) # Print the string print(time_str)
Producción:
Thu Aug 22 11:21:48 2019