寻找一种方法来检索当前时间为 x 的时区

Looking for a way to retrieve timezones in which the current time is x

我正在寻找一种方法来获取时区列表,其中当前时间是一个变量(它也必须依赖于 DST)。

例如我需要向当地时间为18:45的用户推送消息。我可以从我们的数据库中按时区获取用户列表。因此,我计划安排一个每 15 分钟唤醒一次的活动,它需要检查当前 18:45 所在的时区,以便能够检索到正确的用户。

我们的环境是node js。 我正在考虑的 方式 最好是一个 Web 服务或一些实用程序,可以为我完成所有工作,包括来自 iana timezones 数据库的更新。但如果没有这样的选择,我可以处理不太方便的方法。

提前致谢。

这是使用 moment-timezone 的一种简单方法(我相信它可以处理夏令时等问题,不是 100% 确定——但肯定会比从单独从头开始.. 查看他们的 github repo 问题——这是一个复杂的主题):

var moment = require('moment-timezone');
var tm = moment();

var toFind = tm.format('h:mm a');

var userZones = ['America/Los_Angeles', 'America/New_York'];
for (var i=0; i<userZones.length; i++) {
  var fmt = tm.tz(userZones[i]).format('h:mm a');
  if (fmt === toFind) console.log('matched zone: ' + userZones[i]);
}