Last active
January 16, 2023 14:27
-
-
Save thevickypedia/c7bf2fef353ff1c94a9d644f23096987 to your computer and use it in GitHub Desktop.
Timezones using python
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "1cf07039", | |
"metadata": {}, | |
"source": [ | |
"### Step1: Create a virtual env" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f9f28ca7", | |
"metadata": {}, | |
"source": [ | |
"### Step2: Navigate Kernel -> Change Kernel -> adam_venv" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "b46d8b96", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Installed kernelspec timezone_venv in /Users/vicky/Library/Jupyter/kernels/timezone_venv\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!ipython kernel install --user --name=timezone_venv" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "c00c76b5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\u001b[33mDEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621\u001b[0m\u001b[33m\n", | |
"\u001b[0m\u001b[33mDEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621\u001b[0m\u001b[33m\n", | |
"\u001b[0m\u001b[33mDEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621\u001b[0m\u001b[33m\n", | |
"\u001b[0m\u001b[33mDEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621\u001b[0m\u001b[33m\n", | |
"\u001b[0m" | |
] | |
} | |
], | |
"source": [ | |
"!pip install --upgrade --no-warn-script-location --quiet pip\n", | |
"!pip install --quiet tzlocal timezonefinder==5.2.0 geopy==2.2.0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "c57689ae", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'CST-0600'" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import time\n", | |
"time.strftime('%Z%z')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "a613afa2", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CST\n" | |
] | |
} | |
], | |
"source": [ | |
"from datetime import datetime, timezone\n", | |
"print(datetime.now(timezone.utc).astimezone().tzinfo)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "9d3e523e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2023-01-16 09:25:46.470819-05:00\n" | |
] | |
} | |
], | |
"source": [ | |
"import pytz\n", | |
"from datetime import datetime\n", | |
"print(datetime.now(pytz.timezone(\"America/New_York\")))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "27f7b1c7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CST\n" | |
] | |
} | |
], | |
"source": [ | |
"from datetime import datetime\n", | |
"print(datetime.utcnow().astimezone().tzname())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "86b177af", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"America/Chicago\n" | |
] | |
} | |
], | |
"source": [ | |
"from tzlocal import get_localzone\n", | |
"print(get_localzone())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "6c406784", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Time at 'Europe/London'[GMT]: Monday, January 16, 2023 - 02:25 PM\n" | |
] | |
} | |
], | |
"source": [ | |
"import ssl\n", | |
"import certifi\n", | |
"from timezonefinder import TimezoneFinder\n", | |
"from geopy.geocoders import Nominatim, options\n", | |
"\n", | |
"place = 'London'\n", | |
"options.default_ssl_context = ssl.create_default_context(cafile=certifi.where())\n", | |
"geo_locator = Nominatim(scheme=\"http\", user_agent=\"test/1\", timeout=3)\n", | |
"\n", | |
"tf = TimezoneFinder()\n", | |
"place_tz = geo_locator.geocode(place)\n", | |
"coordinates = place_tz.latitude, place_tz.longitude\n", | |
"located = geo_locator.reverse(coordinates, language='en')\n", | |
"address = located.raw.get('address', {})\n", | |
"city, state = address.get('city'), address.get('state')\n", | |
"time_location = f'{city} {state}'.replace('None', '') if city or state else place\n", | |
"zone = tf.timezone_at(lat=place_tz.latitude, lng=place_tz.longitude)\n", | |
"datetime_zone = datetime.now(pytz.timezone(zone))\n", | |
"dt_string_loc = datetime_zone.strftime(\"%A, %B %d, %Y - %I:%M %p\")\n", | |
"timezone = datetime_zone.tzname()\n", | |
"print(f\"Time at {zone!r}[{timezone}]: {dt_string_loc}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "2be768d0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Time at 'America/Chicago'[CST]: Monday, January 16, 2023 - 08:25 AM\n" | |
] | |
} | |
], | |
"source": [ | |
"from tzlocal import get_localzone_name\n", | |
"\n", | |
"timezone = datetime.utcnow().astimezone().tzname()\n", | |
"dt_string = datetime.now().strftime(\"%A, %B %d, %Y - %I:%M %p\")\n", | |
"print(f\"Time at {get_localzone_name()!r}[{timezone}]: {dt_string}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "256feb77", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "timezone_venv", | |
"language": "python", | |
"name": "timezone_venv" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.16" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment