time.mktime()
El método del módulo Time se utiliza para convertir un objeto time.struct_time o una tupla que contiene 9 elementos correspondientes al objeto time.struct_time en tiempo en segundos transcurridos desde la época en la hora local.
Este método es la función inversa del time.localtime()
cual convierte el tiempo expresado en segundos desde la época en un objeto time.struct_time en hora local.
Los siguientes son los valores presentes en el objeto time.struct_time:
Índice | Atributo | Valores |
---|---|---|
0 | tm_año | (por ejemplo, 1993) |
1 | tm_mon | rango [1, 12] |
2 | tm_mday | rango [1, 31] |
3 | tm_hora | rango [0, 23] |
4 | tm_min | rango [0, 59] |
5 | tm_sec | rango [0, 61] |
6 | tm_wday | rango [0, 6], el lunes es 0 |
7 | tm_yday | rango [1, 366] |
8 | tm_isdst | 0, 1 o -1 |
N / A | tm_zone | abreviatura del nombre de la zona horaria |
N / A | tm_gmtoff | desplazamiento al este de UTC en segundos |
Nota: La época es el punto donde comienza el tiempo y depende de la plataforma. En Windows y la mayoría de los sistemas Unix, la época es el 1 de enero de 1970 a las 00:00:00 (UTC) y los segundos bisiestos no cuentan para el tiempo en segundos desde la época. Para verificar cuál es la época en una plataforma determinada, podemos usar time.gmtime (0).
Sintaxis: tiempo.mktiempo(t)
Parámetro:
t : Un objeto time.struct_time o una tupla que contiene 9 elementos correspondientes al objeto time.struct_timeTipo de devolución: este método devuelve un valor flotante que representa el tiempo expresado en segundos desde la época.
Código #1: Uso del time.mktime()
método
# Python program to explain time.mktime() method # importing time module import time # time.gmtime() method will returns # a time.struct_time object in UTC # for the time expressed in seconds # since the epoch seconds = 1000000 obj1 = time.gmtime(seconds) # Print time.struct_time object (in UTC) print(obj1) # Convert the time.struct_time # object to local time expressed in # seconds since the epoch # using time.mktime() method time_sec = time.mktime(obj1) # Print the local time in seconds print("\nLocal time (in seconds):", time_sec) # time.strptime() method parse # a string representing a time # according to the given format # and returns a time.struct_time object # Time string t = "14 Sep 2019 10:50:00" # Parse the time string using # time.strptime() method obj2 = time.strptime(t, "% d % b % Y % H:% M:% S") # Convert the time.struct_time # object to local time expressed in # seconds since the epoch # using time.mktime() method time_sec = time.mktime(obj2) # Print the local time in seconds print("\nLocal time (in seconds):", time_sec)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=12, tm_hour=13, tm_min=46, tm_sec=40, tm_wday=0, tm_yday=12, tm_isdst=0) Local time (in seconds): 980200.0 Local time (in seconds): 1568438400.0
Código #2: Si el parámetro es una tupla
# Python program to explain time.mktime() method # importing time module import time # A tuple containing 9 elements # corresponding to time.struct_time object # for example: consider the below object # time.struct_time(tm_year = 2019, tm_mon = 9, tm_mday = 13, # tm_hour = 1, tm_min = 30, tm_sec = 26, tm_wday = 4, # tm_yday = 256, tm_isdst = 0) # Tuple corresponding to above # time.struct_time object will be tup = (2019, 9, 13, 1, 30, 26, 4, 256, 0) # Convert the above specified tuple # to local time expressed in seconds # since the epoch # using time.mktime() method time_sec = time.mktime(tup) # Print the time print("Local Time (in seconds since the epoch):", time_sec)
Local Time (in seconds since the epoch): 1568318426.0
Código #3: Para mostrar el time.mktime()
método es función inversa del time.localtime()
método
# Python program to explain time.mktime() method # importing time module import time # Get the current time # expressed in seconds # since the epoch using # time.time() method curr_time = time.time() # Print the value # returned by time.time() method print("Current time (in seconds since the epoch):", curr_time) # Convert the time expressed in seconds # since the epoch to # a time.struct_time object # in local time using # time.localtime() method obj = time.localtime(curr_time) # Print the time.struct_time object print("\ntime.struct_time object:") print(obj, "\n") # Convert the time.struct_time object # back to the time expressed # in seconds since the epoch # in local time using # time.mktime() method time_sec = time.mktime(obj) # Print the time print("Time (in seconds since the epoch):", time_sec)
Current time (in seconds since the epoch): 1568318426.2286296 time.struct_time object: time.struct_time(tm_year=2019, tm_mon=9, tm_mday=13, tm_hour=1, tm_min=30, tm_sec=26, tm_wday=4, tm_yday=256, tm_isdst=0) Time (in seconds since the epoch): 1568318426.0
Referencias: https://docs.python.org/3/library/time.html#time.mktime