0

I am currently working with apscheduler with multiple users located in different timezones.

There is no possibility to get the timezone from them automatically, so I need to do this with input data. Using a city is not that obvious to me as some might live in villages or near cities that are within timezones defined by other cities. So I was wondering, is there a possibility to do so using something like pytz module with a difference in hours with the server or is there anything better?

Pytz says the only way possible to do so is like:

amsterdam = timezone('Europe/Amsterdam')

But there is a city-defined timezone, which is not that universal. I want to unload the user as much as I can and free them from unnecessary work of looking up the suitable timezone for each predefined one.

I feel stuck and out of ideas. No success on the Internet so far.

8
  • 1
    Keep in mind that knowing the difference between the current time and utc is not sufficient to know what timezone you're in, since different regions have different daylight saving rules. For example, in the US, Arizona does not observe daylight saving time. As a result, in the summer its time matches PDT time, and in the winter its time matches MST time. Commented Jul 19, 2023 at 17:32
  • The problem is even worse when mixing regions in the northern and southern hemispheres, where daylight saving changes happen in the opposite direction. And even in the same hemisphere, different countries make daylight saving changes on different dates. So in general, you don't have enough information to reliably determine the timezone, unless you can restrict it to a very limited geographic region where everything is in sync. Commented Jul 19, 2023 at 17:35
  • @TomKarzes thanks! a very useful piece of information. Then I can probably ask the user for their utc zone explicitly and assign a fake city for each timezone within which this city is located, then pass the result to pytz. Not the worst solution, but far from being perfect. At least it's what comes immediately to my mind. Commented Jul 19, 2023 at 17:37
  • a city-defined timezone, which is not that universal on the contrary, the IANA timezone database is used by everyone. It's the de-facto (if not the only) standard. 'Europe\Amsterdam doesn't mean you live in Amsterdam but in the timezone where Amsterdam is part of. A country can change its timezone rules at any time, so using the cities instead of eg Western Europe is far more stable and more granular. After all, Spain and the UK are in different timezone for political reasons Commented Jul 19, 2023 at 17:37
  • 1
    I don't think you would be asking your users to do any more work than when they set their timezone in Windows for example. For example, Windows labels a timezone as "(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna". And presumably people outside of those 6 cities don't have trouble figuring out whether it's the right timezone for them. Commented Jul 19, 2023 at 17:41

2 Answers 2

0

Although this is dependent on a timezone from OS (thus, do not adjust timezone based on geolocation):

import datetime
timezone = datetime.datetime.now().astimezone().tzinfo
# datetime.timezone(datetime.timedelta(seconds=32400), 'Tokyo Standard Time')

timezone.tzname(None)
# 'Tokyo Standard Time'

This could work.

(Because this refers to the time from OS, it should consider daylight saving time also)

Sign up to request clarification or add additional context in comments.

Comments

0

Using AsyncIOScheduler, the timezone + hours is calculated as the following:

scheduler = AsyncIOScheduler(timezone=f'Etc/GMT{user_time_zone}')

Where user_time_zone is an str of int numbers from -14 to 9, with positive numbers going with +.

1 Comment

The list of all timezones available can be retrieved with import pytz all_timezones = pytz.all_timezones

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.