【问题标题】:How to load luxon timezone features into mongo shell如何将 luxon 时区功能加载到 mongo shell
【发布时间】:2019-09-20 10:33:40
【问题描述】:

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

DateTime.isInDST()

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

moment.js 

和一个等效的实现。

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

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

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

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

脚本通过 加载(); 声明

但是我得到一个错误

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 按照记录工作。

感谢您的任何建议

【问题讨论】:

    标签: mongodb momentjs luxon


    【解决方案1】:

    一些事情:

    【讨论】:

    • 新的MongoDB Shell支持MapIntl,所以它也可以和Luxon一起使用。到今天为止,新的 shell 仍处于测试阶段,但在不久的将来,这应该会改变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    相关资源
    最近更新 更多