os.system set Timezone:based on ip in python code

 To set the system timezone based on the IP address using os.system in Python, you can follow these steps:

  1. Get the location based on the IP address.
  2. Determine the timezone from the location.
  3. Use os.system to update the system timezone.

Here’s the Python code:


Install Required Libraries

bash
pip install requests timezonefinder

Python Code

python
import os import requests from timezonefinder import TimezoneFinder def get_location_from_ip(): """ Get the approximate geographic location (latitude and longitude) based on the public IP. """ try: response = requests.get("https://ipinfo.io/json") if response.status_code == 200: data = response.json() loc = data.get("loc") # Latitude and Longitude return loc else: print("Failed to get location data from IP.") return None except Exception as e: print(f"Error: {e}") return None def get_timezone_from_location(location): """ Get the timezone based on latitude and longitude using TimezoneFinder. """ try: latitude, longitude = map(float, location.split(",")) tz_finder = TimezoneFinder() timezone = tz_finder.timezone_at(lat=latitude, lng=longitude) return timezone except Exception as e: print(f"Error: {e}") return None def set_system_timezone(timezone): """ Set the system timezone using the os.system command. """ try: command = f"sudo timedatectl set-timezone {timezone}" os.system(command) print(f"Timezone set to: {timezone}") except Exception as e: print(f"Failed to set timezone: {e}") def main(): # Step 1: Get location (latitude and longitude) based on IP location = get_location_from_ip() if not location: print("Unable to determine location.") return print(f"Detected Location (Latitude, Longitude): {location}") # Step 2: Get timezone from location timezone = get_timezone_from_location(location) if not timezone: print("Unable to determine timezone.") return print(f"Detected Timezone: {timezone}") # Step 3: Set the system timezone set_system_timezone(timezone) if __name__ == "__main__": main()

How It Works

  1. Fetch IP-based Location:

    • The script uses https://ipinfo.io/json to get the location (latitude and longitude) based on the public IP address.
  2. Determine the Timezone:

    • The timezonefinder library maps the latitude and longitude to a timezone.
  3. Set the System Timezone:

    • The os.system command uses timedatectl to update the system timezone. This requires sudo privileges.

Example Output

plaintext
Detected Location (Latitude, Longitude): 23.8103,90.4125 Detected Timezone: Asia/Dhaka Timezone set to: Asia/Dhaka

Notes

  • Root Privileges: The sudo command requires administrative privileges. Run the script as a user with sudo access.
  • Windows: The timedatectl command is specific to Linux. For Windows, you would need to use PowerShell commands via os.system.

Let me know if you need assistance adapting this for another operating system!



----------------------------------------------

 

Step 2: Python Code to Handle Etc/GMT

You can map the Etc/GMT timezone to Windows-compatible names and set the timezone using tzutil:

python
import os import platform # Mapping of Etc/GMT to Windows-compatible timezones ETC_TO_WINDOWS_TZ = { "Etc/GMT": "UTC", "Etc/GMT+0": "UTC", "Etc/GMT-3": "E. South America Standard Time", "Etc/GMT+5": "Eastern Standard Time", # Equivalent to UTC-5 # Add more mappings as needed } def set_windows_timezone(etc_timezone): """ Map `Etc/GMT` to Windows-compatible timezone and set it on Windows. """ if platform.system() == "Windows": try: # Map Etc/GMT to Windows timezone windows_timezone = ETC_TO_WINDOWS_TZ.get(etc_timezone) if not windows_timezone: raise ValueError(f"Timezone '{etc_timezone}' is not supported or mapped.") # Set the timezone command = f'tzutil /s "{windows_timezone}"' os.system(command) print(f"Timezone set to: {windows_timezone}") except Exception as e: print(f"Error setting timezone: {e}") else: print("This script is for Windows systems only.") # Example usage set_windows_timezone("Etc/GMT+5")

Output

For Etc/GMT+5:

plaintext
Timezone set to: Eastern Standard Time


-------------------------------------------------



ETC_TO_WINDOWS_TZ = {
# UTC Zones
"Etc/UTC": "UTC",
"Etc/GMT": "UTC",
"Etc/GMT+0": "UTC",
"Etc/GMT-0": "UTC",
"Etc/Universal": "UTC",

# Positive Offsets (GMT-X -> UTC+X)
"Etc/GMT-12": "Dateline Standard Time", # UTC+12
"Etc/GMT-11": "UTC+11", # UTC+11
"Etc/GMT-10": "West Pacific Standard Time", # UTC+10
"Etc/GMT-9": "Tokyo Standard Time", # UTC+9
"Etc/GMT-8": "China Standard Time", # UTC+8
"Etc/GMT-7": "SE Asia Standard Time", # UTC+7
"Etc/GMT-6": "Central Asia Standard Time", # UTC+6
"Etc/GMT-5": "Pakistan Standard Time", # UTC+5
"Etc/GMT-4": "Arabian Standard Time", # UTC+4
"Etc/GMT-3": "Russian Standard Time", # UTC+3
"Etc/GMT-2": "South Africa Standard Time", # UTC+2
"Etc/GMT-1": "W. Europe Standard Time", # UTC+1

# Negative Offsets (GMT+X -> UTC-X)
"Etc/GMT+1": "Cape Verde Standard Time", # UTC-1
"Etc/GMT+2": "Azores Standard Time", # UTC-2
"Etc/GMT+3": "E. South America Standard Time", # UTC-3
"Etc/GMT+4": "Atlantic Standard Time", # UTC-4
"Etc/GMT+5": "Eastern Standard Time", # UTC-5
"Etc/GMT+6": "Central Standard Time", # UTC-6
"Etc/GMT+7": "Mountain Standard Time", # UTC-7
"Etc/GMT+8": "Pacific Standard Time", # UTC-8
"Etc/GMT+9": "Alaska Standard Time", # UTC-9
"Etc/GMT+10": "Hawaii Standard Time", # UTC-10
"Etc/GMT+11": "UTC-11", # UTC-11
"Etc/GMT+12": "Dateline Standard Time", # UTC-12
# North America
"America/New_York": "Eastern Standard Time",
"America/Chicago": "Central Standard Time",
"America/Denver": "Mountain Standard Time",
"America/Los_Angeles": "Pacific Standard Time",
"America/Anchorage": "Alaska Standard Time",
"America/Honolulu": "Hawaiian Standard Time",
"America/Toronto": "Eastern Standard Time",
"America/Vancouver": "Pacific Standard Time",
"America/Phoenix": "US Mountain Standard Time",
"America/Winnipeg": "Central Standard Time",
"America/Edmonton": "Mountain Standard Time",
"America/Bogota": "SA Pacific Standard Time",
"America/Lima": "SA Pacific Standard Time",
"America/La_Paz": "SA Western Standard Time",
"America/Santiago": "Pacific SA Standard Time",
"America/Montevideo": "Montevideo Standard Time",
"America/Asuncion": "Paraguay Standard Time",
"America/Argentina/Buenos_Aires": "Argentina Standard Time",
"America/Barbados": "Atlantic Standard Time",
"America/St_Johns": "Newfoundland Standard Time",
"America/Aruba": "SA Western Standard Time",
"America/Belize": "Central America Standard Time",
"America/Panama": "SA Pacific Standard Time",
"America/Costa_Rica": "Central America Standard Time",
"America/El_Salvador": "Central America Standard Time",
"America/Havana": "Cuba Standard Time",
# Europe
"Europe/London": "GMT Standard Time",
"Europe/Berlin": "W. Europe Standard Time",
"Europe/Paris": "Romance Standard Time",
"Europe/Madrid": "Romance Standard Time",
"Europe/Rome": "W. Europe Standard Time",
"Europe/Amsterdam": "W. Europe Standard Time",
"Europe/Oslo": "W. Europe Standard Time",
"Europe/Stockholm": "W. Europe Standard Time",
"Europe/Bucharest": "GTB Standard Time",
"Europe/Istanbul": "Turkey Standard Time",
"Europe/Kiev": "FLE Standard Time",
"Europe/Athens": "GTB Standard Time",
"Europe/Lisbon": "GMT Standard Time",
"Europe/Dublin": "GMT Standard Time",
"Europe/Brussels": "Romance Standard Time",
"Europe/Copenhagen": "Romance Standard Time",
"Europe/Warsaw": "Central Europe Standard Time",
"Europe/Prague": "Central Europe Standard Time",
"Europe/Vienna": "Central Europe Standard Time",
"Europe/Helsinki": "FLE Standard Time",
"Europe/Riga": "FLE Standard Time",
"Europe/Vilnius": "FLE Standard Time",
# Asia
"Asia/Kolkata": "India Standard Time",
"Asia/Shanghai": "China Standard Time",
"Asia/Tokyo": "Tokyo Standard Time",
"Asia/Dubai": "Arabian Standard Time",
"Asia/Singapore": "Singapore Standard Time",
"Asia/Bangkok": "SE Asia Standard Time",
"Asia/Colombo": "Sri Lanka Standard Time",
"Asia/Kathmandu": "Nepal Standard Time",
"Asia/Manila": "Singapore Standard Time",
"Asia/Riyadh": "Arab Standard Time",
"Asia/Karachi": "Pakistan Standard Time",
"Asia/Dhaka": "Bangladesh Standard Time",
"Asia/Yangon": "Myanmar Standard Time",
"Asia/Tehran": "Iran Standard Time",
"Asia/Tbilisi": "Georgian Standard Time",
"Asia/Novosibirsk": "N. Central Asia Standard Time",
"Asia/Krasnoyarsk": "North Asia Standard Time",
"Asia/Irkutsk": "North Asia East Standard Time",
"Asia/Yakutsk": "Yakutsk Standard Time",
"Asia/Vladivostok": "Vladivostok Standard Time",
"Asia/Magadan": "Magadan Standard Time",
"Asia/Sakhalin": "Magadan Standard Time",
"Asia/Kamchatka": "Kamchatka Standard Time",
"Asia/Ulaanbaatar": "Ulaanbaatar Standard Time",
"Asia/Hong_Kong": "China Standard Time",
"Asia/Taipei": "Taipei Standard Time",
# Africa
"Africa/Lagos": "W. Central Africa Standard Time",
"Africa/Nairobi": "E. Africa Standard Time",
"Africa/Casablanca": "Morocco Standard Time",
"Africa/Accra": "Greenwich Standard Time",
"Africa/Algiers": "Central Europe Standard Time",
"Africa/Windhoek": "Namibia Standard Time",
"Africa/Tunis": "Central Europe Standard Time",
"Africa/Dakar": "Greenwich Standard Time",
"Africa/Abidjan": "Greenwich Standard Time",
"Africa/Luanda": "W. Central Africa Standard Time",
"Africa/Harare": "South Africa Standard Time",
"Africa/Blantyre": "South Africa Standard Time",
"Africa/Addis_Ababa": "E. Africa Standard Time",
"Africa/Kigali": "South Africa Standard Time",
# Australia and Oceania
"Australia/Sydney": "AUS Eastern Standard Time",
"Australia/Perth": "W. Australia Standard Time",
"Australia/Melbourne": "AUS Eastern Standard Time",
"Australia/Brisbane": "E. Australia Standard Time",
"Australia/Adelaide": "Cen. Australia Standard Time",
"Australia/Darwin": "AUS Central Standard Time",
"Pacific/Port_Moresby": "West Pacific Standard Time",
"Pacific/Tahiti": "Hawaiian Standard Time",
"Pacific/Guam": "West Pacific Standard Time",
"Pacific/Chatham": "Chatham Islands Standard Time",
"Pacific/Guadalcanal": "Central Pacific Standard Time",
"Pacific/Tarawa": "UTC+12",
"Pacific/Wallis": "UTC+12",
"Pacific/Samoa": "Samoa Standard Time",
"Pacific/Palau": "Tokyo Standard Time",
"Pacific/Nauru": "UTC+12",
"Pacific/Efate": "Central Pacific Standard Time",
"Pacific/Niue": "UTC-11",
# Middle East
"Asia/Jerusalem": "Israel Standard Time",
"Asia/Baghdad": "Arabic Standard Time",
"Asia/Damascus": "Syria Standard Time",
"Asia/Amman": "Jordan Standard Time",
"Asia/Kuwait": "Arab Standard Time",
"Asia/Qatar": "Arab Standard Time",
"Asia/Aden": "Arab Standard Time",
"Asia/Muscat": "Arabian Standard Time",
"Asia/Bahrain": "Arab Standard Time",
# Arctic and Antarctic Zones
"Antarctica/Palmer": "SA Eastern Standard Time",
"Antarctica/McMurdo": "New Zealand Standard Time",
"Antarctica/Casey": "W. Australia Standard Time",
"Europe/Sofia": "GTB Standard Time",
"Europe/Ljubljana": "Central Europe Standard Time",
"Europe/Skopje": "Central Europe Standard Time",
"Europe/Zagreb": "Central Europe Standard Time",
"Europe/Chisinau": "Moldova Standard Time",
"Europe/Belgrade": "Central European Standard Time",
"Europe/Podgorica": "Central European Standard Time",
"Europe/Monaco": "Central European Standard Time",
# Asia (Continued)
"Asia/Chongqing": "China Standard Time",
"Asia/Seoul": "Korea Standard Time",
"Asia/Beirut": "Middle East Standard Time",
"Asia/Kuala_Lumpur": "Malaysia Standard Time",
"Asia/Jakarta": "SE Asia Standard Time",
"Asia/Hanoi": "SE Asia Standard Time",
# Africa (Continued)
"Africa/Cairo": "Egypt Standard Time",
"Africa/Mogadishu": "East Africa Time",
"Africa/Abuja": "West Africa Standard Time",
"Africa/Dar_es_Salaam": "East Africa Time",
# Australia and Oceania (Continued)
"Australia/Canberra": "AUS Eastern Standard Time",
"Australia/Hobart": "AUS Eastern Standard Time",
"Pacific/Auckland": "New Zealand Standard Time",
"Pacific/Fiji": "Fiji Standard Time",
# South America (Continued)
"America/Caracas": "Venezuela Standard Time",
"America/Georgetown": "Guyana Time",
"America/Paramaribo": "Suriname Time",
"America/Boa_Vista": "Brazil Standard Time",
"America/Cuiaba": "Amazon Standard Time",
# Middle East (Continued)
"Asia/Tashkent": "Uzbekistan Standard Time",
"Asia/Baku": "Azerbaijan Standard Time",
# Caribbean (Continued)
"America/Port_of_Spain": "Atlantic Standard Time",
"America/Kingston": "Jamaica Standard Time",
# Miscellaneous
"Indian/Reunion": "Reunion Standard Time",
"Indian/Comoro": "E. Africa Time",
"Indian/Kerguelen": "Indian/Kerguelen Time",
"Indian/Maldives": "Sri Lanka Time",
"Indian/Chagos": "Indian Time",
"Indian/Cocos": "Cocos Islands Time",
# Arctic and Antarctic Zones
"Antarctica/Rothera": "SA Eastern Standard Time",
"Antarctica/Davis": "SE Asia Standard Time",
"Antarctica/Mawson": "Central Asia Standard Time",
"Atlantic/Reykjavik": "Greenwich Standard Time",
"Atlantic/Azores": "Azores Standard Time",
"Atlantic/Bermuda": "Atlantic Standard Time",
"Europe/Moscow": "Russian Standard Time",
"Africa/Johannesburg": "South Africa Standard Time",
"America/Sao_Paulo": "E. South America Standard Time",
"America/Mexico_City": "Central Standard Time (Mexico)",
}


chatgpt

Post a Comment

Previous Post Next Post