首页 > 解决方案 > 如何将 luxon 时区功能加载到 mongo shell

问题描述

我想在我在 mongodb shell 中使用的 shell 脚本中使用 luxon.js 夏令时和时区功能。这是

DateTime.isInDST()

我追求的功能 - 失败了我会接受

moment.js 

和一个等效的实现。

运行时环境是在 linux VM VirtualBOX 上的 docker 容器中运行的 Mongo 3.6 从命令行提交的脚本(从 cygwin csh 内部)

我已经提取了一个本地副本

https://moment.github.io/luxon/global-filled/luxon.js  

其中(我认为)应该包含所有依赖项。

脚本通过 load() 访问此信息;陈述

但是我收到一个错误

TypeError: "_cache.has is not a function"

查看源文件,我在这里看到了有问题的行 - 2008 行。

if (typeof _cache !== "undefined") {
   if (_cache.has(Class)) return _cache.get(Class);
      _cache.set(Class, Wrapper);
    }
   ....
}

变量 _cache 在这里早些时候声明了几行 - 1998 行。

function _wrapNativeSuper(Class) {
  var _cache = typeof Map === "function" ? new Map() : undefined;
  ...
}

我的猜测是“地图”是先前定义的。所以我破解了代码

var _cache = undefined;  //I read somewhere that it doesn't need to be pre-defined
....

我还检查了我所有的代码,找不到任何定义为“地图”的东西——所以我认为没有本地名称冲突。

在这一点上,我似乎一瘸一拐。我可以创建实例并访问该功能

DateTime.local();  and 
DateTime.toString();  

并获得正确的 UTC 值。

但是请注意,我发现我不得不使用“DateTime”作为全局变量(如文档所述),而不是使用

luxon.DateTime

(因为“luxon”是源中声明的变量)

但是当我编码时

var now = luxon.DateTime.local();
var now_tz = now.setZone( "America/Los_Angeles" ); 

trace("time after tz:"+ now_tz.toString()+" reason:" +now_tz.invalidReason+ " explanation:"+now_tz.invalidExplanation);
trace("zone support is:"+luxon.Info.features().zones);

我明白了

time after tz:Invalid DateTime reason:unsupported zone explanation:the zone "America/Los_Angeles" is not supported
zone support is:false

哪种暗示 Mongodb shell 没有区域支持(除非早期的黑客以某种方式使该区域支持无效)。

我的问题:如何添加区域支持。以及为什么通过 luxon.js 进行的实例化没有按记录工作。

感谢您的任何建议

标签: mongodbmomentjsluxon

解决方案


一些东西:

  • 对于您的地图问题,请在此处查看具有相同症状的人:https ://github.com/moment/luxon/issues/459 。很确定您(或者可能是 Mongo)必须在Map某个地方定义。那件事还没有has定义。
  • Luxon 的全球构建出口luxon,因此在luxon.DateTime意料之中。见,例如这里:https ://moment.github.io/luxon/
  • 环境必须通过 Intl API 提供时区支持。请参阅 Luxon 的文档:https ://moment.github.io/luxon/docs/manual/zones.html#iana-support 。如果 Mongo 的 shell 没有 Intl 或足够新的 Intl 版本,那么您需要使用https://github.com/yahoo/date-time-format-timezone来填充它

推荐阅读