简体   繁体   English

javascript 中的 Google Calendar API 时区覆盖

[英]Google Calendar API time zone override in javascript

I have developed a website that's used as a digital display monitor slide to combine several different Google Calendars into one view.我开发了一个网站,用作数字显示监视器幻灯片以将几个不同的 Google 日历组合到一个视图中。 http://www.anokaramsey.edu/events/displays http://www.anokaramsey.edu/events/displays

I am having a problem with the time zone.我的时区有问题。 We are on Central Time.我们在中部时间。 The digital display has a super wonky configuration in order to show this website as a slide.数字显示器具有超级不稳定的配置,以便将本网站显示为幻灯片。 So it's taking periodic screengrabs of the website from a cloud browser on EST, and sending the images to our digital display software.因此,它会定期从 EST 上的云浏览器中抓取网站的屏幕截图,并将图像发送到我们的数字显示软件。 http://webshots.channelshd.com/remoteImages/anokaramsey-1.png http://webshots.channelshd.com/remoteImages/anokaramsey-1.png

So the events are being rendered on the display into EST, but I need them to stay on CST.所以事件在显示器上呈现为 EST,但我需要它们保持在 CST 上。 I'm using moment.js & moment-timezone.js.我正在使用 moment.js 和 moment-timezone.js。

This is my javascript that's trying to set the correct time zone, but to no avail.这是我的 javascript,它试图设置正确的时区,但无济于事。

    ...
                  var request = gapi.client.calendar.events.list({
                      'calendarId': cals[c],
                          'timeMin': (new Date()).toISOString(),
                          'lazyFetching': false,
                          'showDeleted': false,
                          'singleEvents': true,
                          'maxResults': 5,
                          'futureevents': true,
                          'orderBy': 'startTime'
                  });

                  request.execute(function (resp) {
                      for (i = 0; i < resp.items.length; i++) {
                          gevents.push(resp.items[i]); // keep. to monitor original gcal objects.
                          var start = resp.items[i].start.dateTime;
                          if (!start) {
                              start = resp.items[i].start.date;
                          };
                          var end = resp.items[i].end.dateTime;
                          if (!end) {
                              end = resp.items[i].end.date;
                          };

                          events.push({
                              id: resp.items[i].id,
                              title: resp.items[i].summary,
                              cal: resp.items[i].organizer.email,
                              start: start,
                              end: end,
                              url: resp.items[i].htmlLink,
                              location: resp.items[i].location,
                              descr: resp.items[i].description
                          });
                      }
                  });
              }
          }
    ...
          function display(events) {
              if (events.length > 0) {

                  events.sort(function (a, b) {
                      // super simple sort
                      return new Date(a.start) - new Date(b.start);
                  });

                  for (i = 0; i < events.length; i++) {
                      var event = events[i];
                      var when = event.start;
                      var until = event.end;

                      var header = moment(when).format("MMM D, YYYY");

                      if (header != oldHeader) {
                          var id = moment(when);
                          //appendPre('<h3>'+header+'</h3>');
                          appendPre('<ul id="' + id + '">');
                      }

var startTime = moment(when).tz("America/Chicago").format("h:mma");
var endTime = moment(until).tz("America/Chicago").format("h:mma");

    ...

You should be able to see all the interworking files from the top URL, but if more code would help, please let me know.您应该能够从顶部 URL 看到所有互通文件,但如果更多代码有帮助,请告诉我。

If you open the developer console while on your site, the error message there explains the problem:如果您在站点上打开开发者控制台,那里的错误消息说明了问题:

截屏

You are loading moment.js and moment-timezone.js , but you have not populated any time zone data.您正在加载moment.jsmoment-timezone.js ,但尚未填充任何时区数据。

Often one would want to cover all time zones of the world, and thus would use either moment-timezone-all-years.js or moment-timezone-2010-2020.js , depending on the range of data you care about.通常人们会想要覆盖世界上所有的时区,因此会使用moment-timezone-all-years.jsmoment-timezone-2010-2020.js ,具体取决于您关心的数据范围。 You'd load these instead of moment-timezone.js .你会加载这些而不是moment-timezone.js

However, in your case, I think you only need relatively current data for a single time zone, so I would continue to load moment-timezone.js and simply populate that single zone manually in your own script:但是,就您而言,我认为您只需要单个时区的相对最新数据,因此我将继续加载moment-timezone.js并简单地在您自己的脚本中手动填充该单个时区:

moment.tz.add("America/Chicago|CST CDT|60 50|01010101010101010101010|1BQU0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0");

You can read more about data loading in the documentation , and find other specific time zone data by examining the moment-timezone-with-data-2010-2020.js file.您可以在文档中阅读有关数据加载的更多信息,并通过检查moment-timezone-with-data-2010-2020.js文件查找其他特定时区数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM