I am working on a Django application where I have used twilio to send sms and whatsapp messages and the sendgrid api for sending the emails. The problem occurs in scheduled messages in all three. For example, if I have schedule an email to be sent at 06:24 PM(scheduled time is 06:24 PM), then I am receiving the email at 11:54 PM, hence the difference is of 5 hours and 30 minutes. The same problem also arises in sms and whatsapp.
I think the problem might be due to the timezone. I am scheduling the messages from India and twilio account is in USA. And, the time difference of 5 hours 30 mins is due to the UTC format as India is 5 hours 30 mins ahead of UTC. But I am not getting any solution for this.
Also the time is specified as the UNIX time. I am getting a datetime object whose value is like: "2022-09-13 11:44:00". I am converting it to unix time with the "time module" of python. Hence the code looks like:
message = MAIL(...)
finalScheduleTime = int(time.mktime(scheduleTime.timetupple()))
message.send_at = finalScheduleTime
In the above code, scheduleTime is the datetime object.
So, is there any specific way to solve this problem ?
As per the answer of philnash, the sendgrid api and twilio is scheduling the messages in UTC and we are generating it in IST without timezone attached. Hence first of all we need to attach the timezone and then convert it in the UNIX Timestamp. Refer to the following code:
scheduleTime = scheduleTime.astimezone()
finalScheduleTime = int(time.mktime(scheduleTime.timetupple()))
message.send_at = finalScheduleTime
Hence, this will solve the problem.