Search code examples
pythonpytz

Python get Start of Week According to Timezone/Locale


I want to know what is the start of the week, but it should be according to a specific time zone or locale.

I can retrieve the datetime of the start of the day like this

now = datetime.now()
weekday = now.weekday()   # 0 if it is Monday
start_of_week = now.replace(day=now.day-weekday)

But if I am in a country where the start of the week is Sunday (for example timezone 'Asia/Tel_Aviv') this won't work.

Is there a way to get the start of the week according to timezone or locale with pytz or any other library?


Solution

  • Not that I am aware of.

    This information is not in pytz database. It would be available in locale database, but your system probably doesn't have all possible locales installed (on linux systems locale -a shows what is in there). You could in theory install them all and build a converter between a timezone and a locale, and check it from there.

    Far too complicated to my taste.

    I would just do this manually. There are only so many countries in the world, and most of them use Monday as the first day of week. Just build a dictionary with about 200 entries, you can import the data for example from here: http://chartsbin.com/view/41671

    Hannu