How to translate UTC to arbitrary local time in Mongo J / S

I am doing map reduction work in Mongo db.

The display function should display (for example, counting) events of a certain nature on days in a certain time zone (card key is a calendar day). The time zone may be different and is an effective input parameter for setting map / reduce.

The time stored in database objects is in UTC format.

Example:

object1: time=78000 object2: time=86420 mapReduce(objects, tz='America/Los_Angeles') would return: [{"1/1/1970" : 2}] 

and

 mapReduce(objects, tz='Europe/London') would return: [{"1/1/1970":1},{"1/2/1970":1}] 

in the same dataset.

The JavaScript Date object can ideally convert any UTC time to local time, but it seems to be limited by what the "current" time zone of the J / S environment is. I cannot find a way to specify the time zone in which I want the conversion to be in.

The conversion should be taken into account in DST and preferably for seconds of jump.

Is there anything I can do for this?

+4
source share
1 answer

I found an answer that will work for me. In the end, the scope was limited to support this support in the mongo server database and only on Linux.

@ AsyaKamsky pointed to the J / S welcome library, timezone-js , which does full and proper timezone support, given that it uses real-time files from IANA. However, loading arbitrary java-script libraries into the Mongo server environment is not so simple. You can only load global function definitions. timezone-js must also be provided with a custom transport mechanism for loading timezone files (I don’t even know if the MongoDB server environment provides files for access), or timezone files must be precompiled into JSON objects and together with the library. I decided that this was too tedious for the approach, and I would have to be responsible for providing a mechanism for updating timezone files when they were changed.

Another alternative I studied is hacking the J / S implementation used in Mongo to add a function that will do the job that I want to do. This is what I did. In the glibc world, everything is just as gloomy as in JavaScript, but there is a library for work, icu .

I did this patch , which adds the static function Date.daytz (), which, took the UTC timestamp and timezone name, will return the string yyyy-mm-dd.

Given the following display / reduction functions:

 fmap = function () { emit(Date.daytz(this.time * 1000, globtz), {count:1}); }; fred = function (k, v) { var r = {count:0}; v.forEach(function (v0) {r.count += v0.count;}); return r; }; 

I get exactly what I wanted using these two map reduction commands:

 { "mapreduce" : "objects", "map" : fmap, "reduce" : fred, "out" : { "inline" : 1 }, "scope" : { "globtz" : "Europe/London" } } 

and

 { "mapreduce" : "objects", "map" : fmap, "reduce" : fred, "out" : { "inline" : 1 }, "scope" : { "globtz" : "America/Los_Angeles" } } 
+4
source

Source: https://habr.com/ru/post/1439038/


All Articles