使用Python发送短信;考虑时区的时间延迟

发布于 2024-12-04 19:14:27 字数 463 浏览 1 评论 0原文

因此,我有一个代码可以接收移动设备的相关信息,例如电话号码、姓名和消息,通过这些信息可以发送短信。此外,还有延迟短信的功能。因此,用户可以使用下拉菜单选择稍后发送消息。此外,延迟函数还会检查消息发送时间是否在 12:00am 到 8:00am 之间,如果是,则返回默认值 8:00am,这样接收方将在 8:00am 收到消息。最早。然而我面临的问题是,时间延迟是按照山地夏令时间(加拿大/美国)计算的,这意味着居住在 PDT 时区的人们将在上午 7:00 收到消息(太早了),而居住在 EDT 的人们将在上午 7:00 收到消息(太早了)上午 10:00 收到消息(晚了 2 小时)。所以我想知道是否有一些 python 代码可能会检测接收者所在的时区,以便他/她准时收到消息(如果我在内华达州选择上午 8:00,那么加利福尼亚州的接收者将在上午 8:00 PDT),给定接收器所在的区域(例如,给出的代码表明接收器位于加利福尼亚州)。

关于如何解决这个问题有什么想法吗?

谢谢!

So I have a code that receives relevant information for a mobile device, such as phone number, name, and message, through which it can send an SMS message. Also, there is a function which delays the SMS. So a user can choose to send a message at a later time using a drop-down menu. Furthermore the delay function checks whether the time at which the message is to be sent between 12:00am to 8:00am, and if so then the default of 8:00am is returned so the receiver will get the message at 8:00am at the earliest. However the problem I am facing is that the time delay is in terms of Mountain Daylight Time (Canada/US), which means that people living in the PDT timezone will receive the message at 7:00am (hour too early) while people in EDT receive the message at 10:00am (2 hours too late). So I was wondering if there is some python code that would potentially detect which timezone the receiver is located in so that he/she receives the message on time (if I select 8:00am in Nevada then the receiver in California will receive the message at 8:00am PDT), given the area in which the receiver is located (eg. the code is given that the receiver is located in California).

Any idea on how to approach this problem?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

捶死心动 2024-12-11 19:14:27

快速而肮脏:您可以通过让用户说出事件应在多少小时/分钟后发生来避免该问题。那么他们当地的时区是什么并不重要。

更好:如果您能够获取手机上的当地时间以及 UTC 时间(无论是从手机还是从您的服务器),这将告诉您 UTC 偏移量 的电话。大多数时候你都可以逃脱惩罚。但 UTC 偏移量与本地时区不同。在夏令时或夏令时边界附近,当您假设 UTC 偏移量与时区相同时,诸如“明天同一时间”之类的想法就会失效。

最佳:为了正确支持政治时区政策,Olson 数据库无可替代。使用 Olson 意味着您的应用有一个配置项供用户设置时区(例如:America/New_YorkEtc/UTC)。您可以根据电话和手机号码的 UTC 偏移量(使用其他答案中详细介绍的查找表)选择默认值,并且在大多数情况下都是正确的。

在 Python 中, pytz 模块为您提供了一个非常干净的 API,用于使用 Olson 数据库来使用datetime 对象。

编辑:在给定时区对象的情况下,此函数会将 Unix 时间转换为本地化 datetime 对象:

def localize_epoch_time(epoch_time, timezone=pytz.UTC):
  u"Given an epoch time return an accurate, timezone-aware datetime object"
  t = localtime(epoch_time)
  epochdt = datetime(*(t[:6] + (int((epoch_time - long(epoch_time)) * 1000000), timezone)
  if hasattr(timezone, 'normalize'):  # pytz tzinfo objects have this
    return timezone.normalize(epochdt)
  else: # tzinfo object not from pytz module
    return epochdt

Quick and Dirty: You could dodge the problem by getting the user to say after how many hours/minutes the event should occur. Then it doesn't matter what their local time zone is.

Better: If you have the ability to get the local time on the phone, as well as the UTC time (either from the phone or from your server), that will tell you the UTC offset of the phone. You can get away with that most of the time. But UTC offset is NOT the same thing as a local time zone. Near a daylight-savings or summer-time boundary, ideas like "the same time tomorrow" break when you assume that the UTC offset is the same as the time zone.

Best: For supporting political time zone policies correctly, there is no substitute for the Olson database. Using Olson would imply that your app has a configuration item for the user to set their timezone (examples: America/New_York and Etc/UTC). You might choose defaults for this based on the UTC offset of the phone and the mobile number (using lookup tables as detailed in the other answers) and be correct most of the time.

In Python the pytz module gives you a very clean API for using the Olson database to work with datetime objects.

EDIT: This function will convert Unix time to a localized datetime object, given a timezone object:

def localize_epoch_time(epoch_time, timezone=pytz.UTC):
  u"Given an epoch time return an accurate, timezone-aware datetime object"
  t = localtime(epoch_time)
  epochdt = datetime(*(t[:6] + (int((epoch_time - long(epoch_time)) * 1000000), timezone)
  if hasattr(timezone, 'normalize'):  # pytz tzinfo objects have this
    return timezone.normalize(epochdt)
  else: # tzinfo object not from pytz module
    return epochdt
堇色安年 2024-12-11 19:14:27

除非您在手机上运行一些代码,否则要准确了解手机所有者的时区是不可能的,因为手机所有者可能会跨时区旅行。

要解决这个问题,您可以执行以下两种操作之一...

最佳猜测:这是使用反向电话查找 API,例如参见 http://www.phonenumber.com/phone/1-214-213-1234 它清楚地告诉您该号码属于德克萨斯州,因此最好的猜测是使用德克萨斯州时区,它并不总是准确的,但大多数时候都是准确的。您需要根据您的使用情况寻找 API。

安全操作:确保您可以在任何时区都不是极端的时间内交付,以免打扰任何人。因此,假设您选择中部时间上午 8:30。太平洋时间为上午 6:30,东部时间为上午 9:30,因此在任何时区都不算太早或太晚。

To accurately know the time zone of the owner of the phone is impossible unless you have some code of yours running on his phone as the owner of the phone could be travelling across time zones.

To address this concern you can do one of two things....

Best Guess: This is, use a reverse phone lookup API, like see http://www.phonenumber.com/phone/1-214-213-1234 It clearly tells you that the number belongs to Texasm hence the best guess would be use the texas time zone, its not always accurate but it is majority of the times. You need to hunt for APIs based on your usage.

Play Safe: Make sure that you may deliver in a time which is not extreme in any time zone, so that no one is disturbed. Hence lets say you pick 8:30 AM central time. Thats 6:30 AM pacific, and 9:30 AM eastern, so its not too early or too late in any time zone.

忆伤 2024-12-11 19:14:27

对于时区,最好是猜测和询问,因为除非您有类似 GPS 的设备,否则您可能会猜错。猜测并不是一件容易的事,但为了改善用户体验是值得的。

这些消息是通过手机短信还是网络安排的?如果通过电话,请使用 @Adithya 建议的电话查找 API。如果来自网络,请尝试使用地理 IP 服务,例如 http://www.hostip.info/ 。然后,您需要找到将地理坐标转换为时区的服务。我不知道有什么,但我相信你可以通过谷歌搜索找到一些。

另请记住,众所周知,手机的 IP 地址很难进行地理定位。如果它是通过电话使用的网络界面,我可能甚至不会理会这条路线。尝试 HTML5 地理位置

With timezones, it's best to both guess and ask, because unless you have a GPS-like device you'll probably guess wrong. And guessing is not an easy task, but worth it to improve your users' experience.

Are the messages being scheduled from a phone via SMS or from the web? If from the phone, use a phone lookup API like @Adithya suggests. If from the web, try a geo-ip service like http://www.hostip.info/ . Then you'll need to find a service that converts geo coordinates to timezones. I don't know of any off the top of my head, but I'm sure you can find some by googling.

Also keep in mind that IP addresses of mobile phones are notoriously difficult to geo-locate. If it's a web interface being used from the phone, I probably wouldn't even bother with that route. Try HTML5 geo-location.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文