informatica:programacion:python:fechas
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| informatica:programacion:python:fechas [2020/11/13 16:13] – [Formatos] tempwin | informatica:programacion:python:fechas [2021/11/04 17:04] (actual) – [Intervalos] tempwin | ||
|---|---|---|---|
| Línea 3: | Línea 3: | ||
| <code python> | <code python> | ||
| import datetime as dt | import datetime as dt | ||
| + | # from datetime import datetime | ||
| </ | </ | ||
| Línea 26: | Línea 27: | ||
| <code python> | <code python> | ||
| d = dt.date(2020, | d = dt.date(2020, | ||
| + | </ | ||
| + | |||
| + | Utilizando el método '' | ||
| + | |||
| + | <code python> | ||
| + | d = datetime.strptime(" | ||
| </ | </ | ||
| Línea 31: | Línea 38: | ||
| <code python> | <code python> | ||
| - | d = dt.datetime(2020, | + | from datetime import datetime |
| + | d = datetime(2020, | ||
| </ | </ | ||
| - | Para poder ver la fecha como una cadenad | + | Para poder ver la fecha como una cadena |
| <code python> | <code python> | ||
| Línea 42: | Línea 50: | ||
| # 2020-12-31 12:59:59 | # 2020-12-31 12:59:59 | ||
| </ | </ | ||
| + | |||
| + | Otra forma de formatear una fecha es utilizando el método '' | ||
| + | |||
| + | <code python> | ||
| + | fecha.strftime(" | ||
| + | |||
| + | # Resultado: | ||
| + | # ' | ||
| + | </ | ||
| + | |||
| + | ==== Zona horaria ==== | ||
| + | |||
| + | Los objetos DateTime en Python no tienen en cuenta la zona horaria debemos hacer que sean conscientes. Para ello nos ayudamos del módulo **pytz**: | ||
| + | |||
| + | <code python> | ||
| + | from datetime import datetime | ||
| + | import pytz | ||
| + | |||
| + | # Listas las diferentes zonas horarias que tiene Pytz: | ||
| + | for tz in pytz.all_timezones: | ||
| + | print(tz) | ||
| + | |||
| + | # Fecha y hora actual en la zona horaria actual | ||
| + | now = datetime.now() | ||
| + | |||
| + | print(now) | ||
| + | |||
| + | # Resultado | ||
| + | # 2020-11-13 17: | ||
| + | |||
| + | # Creamos una hora en UTC (aunque no tiene información sobre zona horaria) | ||
| + | utcnow = datetime.utcnow() | ||
| + | |||
| + | # Asignamos una zona horaria al objeto anterior | ||
| + | utcnow = utcnow.replace(tzinfo=pytz.utc) | ||
| + | |||
| + | print(utcnow) | ||
| + | |||
| + | # Resultado | ||
| + | # 2020-11-13 16: | ||
| + | </ | ||
| + | |||
| + | <WRAP center round tip 60%> | ||
| + | Por regla general, se recomienda guardar las fechas en UTC y hacer posteriormente las conversiones a la zona horaria deseada | ||
| + | </ | ||
| + | |||
| + | |||
| + | Para convertir zonas horarias: | ||
| + | |||
| + | <code python> | ||
| + | madrid = utcnow.astimezone(pytz.timezone(" | ||
| + | |||
| + | print(madrid) | ||
| + | </ | ||
| + | |||
| ===== Extracción ===== | ===== Extracción ===== | ||
| Línea 111: | Línea 174: | ||
| print(d1 > d2) # False | print(d1 > d2) # False | ||
| </ | </ | ||
| + | |||
| + | ==== Fecha ==== | ||
| + | |||
| + | Si solo queremos comparar la fecha (año, mes y día): | ||
| + | |||
| + | <code python> | ||
| + | import datetime | ||
| + | | ||
| + | # date and time in yyyy/mm/dd hh:mm:ss format | ||
| + | d1 = datetime.datetime(2020, | ||
| + | d2 = datetime.datetime(2020, | ||
| + | |||
| + | print(d1.date() < d2.date()) # True | ||
| + | |||
| + | print(d1.date() > d2.date()) # False | ||
| + | </ | ||
| + | |||
| + | ==== Tiempo ==== | ||
| + | |||
| + | Para comparar solo el tiempo (horas, minutos, segundos...) | ||
| + | |||
| + | <code python> | ||
| + | import datetime as dt | ||
| + | | ||
| + | # date and time in yyyy/mm/dd hh:mm:ss format | ||
| + | d1 = dt.datetime(2020, | ||
| + | d2 = dt.datetime(2020, | ||
| + | d3 = dt.datetime(2020, | ||
| + | |||
| + | print(d1.time() == d2.time()) # False | ||
| + | print(d1.time() == d3.time()) # True | ||
| + | print(d1.time() < d2.time()) # False | ||
| + | print(d1.time() < d3.time()) # False | ||
| + | print(d1.time() > d2.time()) # True | ||
| + | print(d1.time() > d3.time()) # False | ||
| + | </ | ||
| + | |||
| + | ===== Intervalos ===== | ||
| + | |||
| + | Los objetos '' | ||
| + | |||
| + | <code python> | ||
| + | from datetime import timedelta | ||
| + | |||
| + | nuevo_dia = cualquier_dia + timedelta(days=1) | ||
| + | |||
| + | # añade un día a '' | ||
| + | </ | ||
| + | |||
| + | Podríamos hacer comparaciones: | ||
| + | |||
| + | <code python> | ||
| + | cualquier_dia > nuevo_dia | ||
| + | |||
| + | # Resultado: | ||
| + | # False | ||
| + | </ | ||
| + | |||
| + | ===== Tiempos ===== | ||
| + | |||
| + | '' | ||
| + | |||
| + | Un uso típico es ver la duración de la ejecución de un programa. | ||
| + | |||
| + | <code python> | ||
| + | from time import time | ||
| + | |||
| + | t0 = time() | ||
| + | |||
| + | for x in range(100000): | ||
| + | a = x | ||
| + | |||
| + | t1 = time() | ||
| + | |||
| + | t1 - t0 | ||
| + | |||
| + | # Resultado: | ||
| + | # 0.007186412811279297 | ||
| + | </ | ||
| + | |||
informatica/programacion/python/fechas.1605280399.txt.gz · Última modificación: por tempwin
