Internet Explorer 11 computed timezone bug

∥☆過路亽.° 提交于 2021-01-28 07:14:54

问题


I am aware of the origin of the problem and googled like half a day, but still couldn't find any good workarounds or solutions for this.

Our Angular 7+ application use a timezone interceptor, which looks like this:

import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';

@Injectable()
export class TimezoneInterceptor implements HttpInterceptor {

  constructor() {
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

    if (timezone) {
      req = req.clone({
        setHeaders: {
          Timezone: timezone
        }
      });
    } else {
      console.error('Unknown Timezone!');
    }

    return next.handle(req);
  }

}

The Internet Explorer 11 doesn't support the resolvedOptions().timeZone; and I don't know what could I use instead to make it work across all browsers.

Every help is appreciated!


回答1:


Indeed, this isn't supported on IE 11. See the Intl Compatibility Chart, under "DateTimeFormat" then "resolvedOptions().timeZone defaults to the host environment".

If you must support IE 11, then you'll need to resort to older methods of guessing the user's time zone. For a long time, jsTimeZoneDetect, was the only reliable method for this. However, it's not been maintained so I wouldn't recommend it these days.

Later, Moment-Timezone added the moment.tz.guess() function. Since this is still maintained to a certain degree, that's probably your only option.

Both libraries will use the Intl API when available, but then fall back to an algorithm that interrogates the Date object for various offsets at certain key DST transition dates. This is why it's a "guess". It is usually pretty accurate, or at least close, but cannot always identify the exact setting of the user's computer in certain edge cases.

The other drawback is that these libraries have to ship time zone data to the browser, which comes with the overhead of that data size. This is mitigated somewhat by using one of the truncated data files, such as moment-timezone-with-data-10-year-range.min.js.

If you don't have to support IE 11 and older browsers, then you can just use the Intl API directly, as you showed in your question.



来源:https://stackoverflow.com/questions/55914911/internet-explorer-11-computed-timezone-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!