Created
November 22, 2019 13:48
-
-
Save messa/b4d7db171ebc61be536c006cb07d5a08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "datetime lokalizace.ipynb", | |
"provenance": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "-SJkVAjpWTGR", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"V Pyhonu většinou pracujeme s datetime v UTC, ale navíc bez nastavené timezone - tzv. naive" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "1WIhHvViWPHP", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 85 | |
}, | |
"outputId": "f7043165-e19d-44ed-ea83-6f1ed77efb01" | |
}, | |
"source": [ | |
"from datetime import datetime\n", | |
"\n", | |
"dt_naive = datetime.utcnow()\n", | |
"print(dt_naive)\n", | |
"print(dt_naive.isoformat())\n", | |
"print(dt_naive.tzinfo)\n", | |
"print(dt_naive.tzname())" | |
], | |
"execution_count": 19, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"2019-11-22 13:48:10.817739\n", | |
"2019-11-22T13:48:10.817739\n", | |
"None\n", | |
"None\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "2F46WWf0Ws8K", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Pomocí knihovny [pytz](https://pypi.org/project/pytz/) si to převedeme do datetime se správně nastaveným tzinfo na UTC (protože ten naive datetime byl v UTC)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "CGCvDZrgWkIS", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 85 | |
}, | |
"outputId": "f8b17288-08ae-44f2-9550-c714ed5341f4" | |
}, | |
"source": [ | |
"import pytz\n", | |
"\n", | |
"dt_utc = pytz.utc.localize(dt_naive)\n", | |
"print(dt_utc)\n", | |
"print(dt_utc.isoformat())\n", | |
"print(dt_utc.tzinfo)\n", | |
"print(dt_utc.tzname())" | |
], | |
"execution_count": 20, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"2019-11-22 13:48:10.817739+00:00\n", | |
"2019-11-22T13:48:10.817739+00:00\n", | |
"UTC\n", | |
"UTC\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "5dFAb-QxXAvh", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"A teď už to můžeme opět pomocí pytz převést do libovolné timezone" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "N81AUo9-W3Le", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 85 | |
}, | |
"outputId": "5ec461ea-f753-4b18-e54e-260f3eee0629" | |
}, | |
"source": [ | |
"dt_prg = pytz.timezone('Europe/Prague').normalize(dt_utc)\n", | |
"print(dt_prg)\n", | |
"print(dt_prg.isoformat())\n", | |
"print(dt_prg.tzinfo)\n", | |
"print(dt_prg.tzname())" | |
], | |
"execution_count": 21, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"2019-11-22 14:48:10.817739+01:00\n", | |
"2019-11-22T14:48:10.817739+01:00\n", | |
"Europe/Prague\n", | |
"CET\n" | |
], | |
"name": "stdout" | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment