代码之家  ›  专栏  ›  技术社区  ›  Savageman

Microsoft团队:获取用户的时区?

  •  0
  • Savageman  · 技术社区  · 6 年前

    2 回复  |  直到 6 年前
        1
  •  1
  •   Zoe - Save the data dump 张群峰    6 年前

    在发送给用户的每条消息中,都有一个 entities[] 集合,其中之一是用户区域设置的详细信息。例如(复制/粘贴自 here ):

    "entities": [
      { 
        "locale": "en-US",
        "country": "US",
        "platform": "Windows",
        "timezone": "America/Los_Angeles",
        "type": "clientInfo"
      }
    ],
    
        2
  •  1
  •   Savageman    6 年前

    答案是:有一个 localTimestamp 属性,该属性可用于获取时间偏移,这足以满足我的需要。

        3
  •  1
  •   Felipe Plets    4 年前

    从…起 @Savageman answer

    答案是:有一个localTimestamp属性可以用来获取时间偏移量,这对于我所需要的已经足够好了。

    "NOT Receive the timezone" 通过映射 utcOffset 属于 localTimestamp country 在里面 entities timezone .

    我编写了一个javascript代码来获取时区,比如 "Asia/shanghai" 通过使用 "localTimestamp": "2019-08-06T18:23:44.259+08:00" "country": "CN" 从…起 Session 在团队信息中。

    更多详细信息请访问我的github readme.md .

    let moment = require("moment-timezone");
    let ct = require("countries-and-timezones");
    
    let partOfSampleSession = {
        "message": {
            "entities": [
                {
                    "country": "CN",
                    "locale": "zh-CN",
                    "platform": "Web",
                    "type": "clientInfo"
                }
            ],
            "localTimestamp": "2019-08-06T18:23:44.259+08:00"
        }
    }
    
    function getTimezoneFromSession(session) {
        // Get the name of country, such as "CN", "JP", "US"
        let country = session.message.entities[0].country;
    
        // Get the localTimestamp from message in session, such as "2019-08-06T18:23:44.259+08:00"
        let localTimestamp = session.message.localTimestamp;
    
        // Caculate the utfOffset of "localTimestamp", such as "480" by "2019-08-06T18:23:44.259+08:00"
        let utcOffsetOfLocalTime = moment().utcOffset(localTimestamp).utcOffset();
    
        // Mapping country to an object array which contains utcOffsets and it's corresponding timezones
        // One element from mxTimezones is {"utcOffset": "480", "name": "Asia/Shanghai"}
        let mxTimezones = ct.getTimezonesForCountry(country);
    
        // get the same timezone as localtime utcOffset from timezones in a country
        let timezone = "";
        mxTimezones.forEach(mxTimezone => {
            if (mxTimezone.utcOffset == utcOffsetOfLocalTime) {
                timezone = mxTimezone.name;
            }
        });
        return timezone;
    }
    let timezone = getTimezoneFromSession(partOfSampleSession);
    // timezone = "Asia/Shanghai"
    console.log(timezone);
    
    
    // example of ct.getTimezonesForCountry("US")
    // mxTimezones = [
    //      {
    //          "name": "America/New_York",
    //          "utcOffset": "-300",
    //      },
    //      {
    //          "name": "America/Los_Angeles",
    //          "utcOffset": "-480",
    //      }
    //      ...
    //      27 elements
    //      ...
    // ]
    
    推荐文章