Python: Email Users at 5Am According to Their Timezone

Think Google calendar but I have a table with a list of reminders to send my users. Since my users are global, I can't email everyone at 5AM EST. I'll have a cronjob that runs every hour. If a record matches the date they've entered and 5AM, then email the user. I haven't figured out the SQL part of it yet, that should be easy (I think), for now I'm using static variables to test the functionality.

I guess my issue is, I want to make sure the script completes in less than a minute or else "5:01" comes around and people will miss their emails. There has to be a better way of doing this.

from datetime import datetime
from pytz import timezone

d_format = "%Y-%m-%d %H:%M"

# This is the value pulled from the database. Format: (Y-m-d 05:00)
record_date = "2013-07-01 05:00"

# Current time in UTC
servertime = datetime.now(timezone('UTC'))

# Let's assume the user lives on the West Coast
user1 = now_utc.astimezone(timezone('US/Pacific'))

if user1.strftime(d_format) == record_date:
    print "Send user an email!"
print ""

# Let's assume the user lives in Singapore
user2 = now_utc.astimezone(timezone('Singapore'))

if user2.strftime(d_format) == record_date:
    print "Send user an email!"
print ""
7
David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article