問題描述
如何在 Zing Chart 中將時區設置為浮點數? (How to set time zone as float in Zing Chart?)
我使用 ZingChart 將數據顯示為圖表。在字符中,我顯示了從服務器獲取的時間之後的數據。ZingChart 被設置為默認遵循客戶端時間。
我發現我們只能將圖表中的時區設置為整數值。但是,有些時區是 UTC+10:30, UTC+04:30...
那麼我們如何將時區設置為 ZingChart 顯示正確的時間呢?如果那天有夏令時,我應該如何糾正它。
參考解法
方法 1:
Unfortunately it appears that ZingChart only supports whole‑hour time zone offsets. Not only does this not account for time zones with fixed fractional‑hour offsets, but it also doesn't properly account for time zones that use daylight saving time.
The example in the documentation says:
... For example, to set the timezone to Pacific Time, you would add:
"timezone":‑8
.
This is incorrect, as Pacific time is only at UTC‑8 during standard time. When it's in daylight time, it uses UTC‑7.
This is a common mistake. See "Time Zone != Offset" in the timezone tag wiki. My recommendation to the ZingChart developers would be:
Anywhere you support
timezone:‑8
you should also support fractional hour offsets such astimezone:5.5
ortimezone:8.75
.You should also support named time zone identifiers such as
"America/Los_Angeles"
. To make them work, you'll need to provide a function that the developer can hook into. Don't try to implement the function directly, as there are several libraries already available for this. For example, a developer might combine ZingChart with moment‑timezone by writing something like:zingchart.fnTZOffset = function(timestamp, timeZone) { return moment(timestamp).tz(timeZone).utcOffset() / 60; }
ZingChart would invoke this function when timezone was a string and would apply the resulting offset to the specific data point.
Without support from ZingChart, there's not much you can do to properly support time zones.
方法 2:
One other solution to the Daylight Savings time issue some ZingChart users have mentioned in the past is MomentJS. http://momentjs.com
(by Tri Pham、Matt Johnson‑Pint、Merrily)