[Answered ]-Get list of users when it is midnight in their timezone

1👍

Django will filter at the database side, so using datetime.now(F('timezone')) will not work. What you can do is "prepare" the list of timezones with a given offset:

from pytz import all_timezones, timezone
from datetime import datetime, timedelta
from django.utils.timezone import now

now = datetime.utcnow()
day = timedelta(days=1)
target_offset = timedelta(hours=now.hour) % day

timezones = {
    tz
    for tz in all_timezones
    if timezone(tz).utcoffset(now) % day == target_offset
}

This will generate a set timezones that contains the names of all timezones with the given offset. Next we can filter the users for that timezone:

users_to_email = User.objects.filter(
    timezone__in=timezones
)

Leave a comment