大头
Table_bottom

标签云
Table_bottom

分类
Table_bottom

日历
二月
28293031123
45678910
11121314151617
18192021222324
252627282912
Table_bottom

评论
Table_bottom

留言
Table_bottom

微博
Table_bottom

热门文章
Table_bottom

随机文章
Table_bottom

豆瓣上谁关注这里
Table_bottom

链接
Table_bottom

搜索栏
Table_bottom

RSS
RSS Link
Table_bottom

功能
Table_bottom

页面
Table_bottom

计数器
464552
Table_bottom

访客统计
Table_bottom

存档
Table_bottom

TimeZone里的daylight savings

loveisbug posted @ 2013年6月19日 14:11 in 工作 with tags Android , 1999 阅读

被一个夏令时的问题折腾很久。场景是这样的,欧洲数字电视应用(DVB-T标准)可以从空中实时信号中获得当前UTC时间,节目预告的UTC时间, 和所谓的offset值。这个offset是包含夏令时的,也就是说如果在UTC+1时区的夏令时期间,空中信号送来的offset是2,非夏令时期 间,offset是1。
欧洲的客户报告说我们显示的节目预告时间比当地时间提前了一小时。
我们做了测试版本,显示UTC时间和信号里的offset值,结果显示是正确的。
无奈只能请客户录把空中信号录一段码流给我们,可是在我们这边还是没能复现问题。耗时很久没能检查出代码里的问题。只好搁置。

两周后,我们想到了第一个教训。用客户录制的码流测试,和客户场测唯一的差别是手机的时区设置。我们测试用的手机都是设置为“北京时间”(UTC+8)。客户的手机设置是“欧洲中部时间,夏令时”(UTC+2)。
如此设置后果然可以复现出该问题。我们App里显示的时间比手机上的“本地时间”多一小时。

原因在于我们想当然的编码思路。
我们在程序运行起来获得空中offset值后,用它设置了进程default的TimeZone。

TimeZone tz = TimeZone.getDefault();
tz.setRawOffset(stream_offset * 1000);
TimeZone.setDefault(tz);

然后,在需要显示时间的地方,创建Calendar对象时,不传TimeZone,期望的是使用上面setDefault()的那个TimeZone。

Calendar local_cal_start = new GregorianCalendar();
local_cal_start.setTimeInMillis(epg_start_time);

这里的epg_start_time是“the date in milliseconds since January 1, 1970 00:00:00 UTC”。
这样做,当手机的时区设置为北京时间(“UTC+8”)时(没有夏令时),时间显示正确;当手机的时区设置为欧洲中部时间(“UTC+2”,夏令时)时,多加了1小时。
在这里加打印观察,创建Calendar如果set一个夏天的日期,local_cal_start.get(Calendar.DST_OFFSET)返回3600000,即1小时夏令时偏移;如果set一个冬天的日期,返回0。
这里,显示的时间被多加了一次夏令时偏移。

前面说的那个测试版本显示UTC时间正确,在那个版本里是这样创建Calendar的。

Calendar local_cal_start = new GregorianCalendar(TimeZone.getTimeZone("GMT"));

仔细看看API文档。
TimeZone.setRawOffset(), Sets the offset in milliseconds from UTC of this time zone's standard time. 这里是standard time,也就是raw offset,没有夏令时。
TimeZone.getTimeZone("GMT+5"), return an object with a raw offset of +5 hours from UTC, and which does not use daylight savings. 这里跟上面的一样,也是raw offset的standard time,没有夏令时。
TimeZone.getDSTSavings(), 看上去它的作用等同于Calendar的get(Calendar.DST_OFFSET)。returns 3600000 (1 hour) for time zones that use daylight savings time and 0 for timezones that do not.

就是说,我们把来自空中信号的包含了夏令时偏移的offset当做raw offset设置给default的当前进程TimeZone,然后基于这个TimeZone创建Calendar时又加了夏令时偏移,于是多加了1小时。

解决方案是把getDefault()/setRawOffset()/setDefault()代码段改为如下。

timeZone = TimeZone.getTimeZone("GMT+" + (stream_offset / 3600));

之后,需要创建Calendar的时候,

Calendar local_cal_start =  new GregorianCalendar(MyBlaBla.timeZone);

再总结一下,调用TimeZone.getDefault(),获取了系统时区设置,如果该时区有夏令时,Calendar会内部、自动地处理夏令时。
我们要做的事情是忽略手机的系统时区设置,完全根据从空中信号中获得的offset时间来设置TimeZone和创建Calendar。

补充一件事,DateFormat类拥有一个protected的Calendar,用DateFormat格式化日期时间的时候,注意要不要给这个Calendar设置TimeZone。

CBSE Class 2 Urdu Qu 说:
2022年9月15日 12:28

Download the complete chapter-by-chapter solutions question bank for Class 2 Ibtedai Sample Paper 2022 from the CBSE Board. Students in Urdu-medium classes in Class II may download the Ibtedai solved question bank based on the revised exam format. CBSE Class 2 Urdu Question Paper Download and practise the Ibtedai solved question bank that is recommended by Urdu topic experts to improve your performance in all class 2 exams.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter