测试正确的时区处理 [英] Testing correct timezone handling

查看:32
本文介绍了测试正确的时区处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在处理大量数据,所有数据均以 UTC(Java)标记.在读取这些数据、将其存储在数据库中并再次将其取出之间,发生了一些数据在夏令时关闭一小时的情况.由于 UTC 没有夏令时的概念,这显然是软件中的一个错误.一旦知道,就很容易修复.

We are working with large amounts of data, all tagged in UTC (in Java). Between reading this data, storing it in a database and getting it out again, it happened, that some data was off one hour during daylight saving time. As UTC has no concept of daylight saving time this was clearly a bug within the software. Once known, it's easy to fix.

但是,最好有一些单元/集成测试,不管当前的时差如何 - 例如我想更改本地时区并在这些不同的时区中一遍又一遍地运行一些方法,以确保正确处理 UTC.

However, it'd be nice to have some unit/integration tests that work regardless of the current time difference - e.g. I'd like to change the local time zone and run some methods over and over again within these different time zones to make sure UTC is handled correctly.

由于测试应该自动运行,而且最好是在一个 Testsuite 中运行,我想知道如何最好地测试正确的行为.在重新启动 JVM 时更改时区等本地设置很容易,但在测试套件中运行它并不容易.

As the tests should run automatically and - preferably - within one Testsuite, I'm wondering how to best test for correct behaviour. It'd be easy to change local settings like time zone upon restarting the JVM, but running this within a test suite is not that easy.

有人知道支持这种情况的测试环境、库或模式吗?我们通常使用 JUnit,但如果它有助于解决此类问题,我们愿意添加另一种环境/技术.我想这与其说是单元测试,不如说是集成.

Does anybody know of a test environment, library or pattern supporting this scenario? We're usually working with JUnit, but are open for adding another environment/technique if it helps getting rid of problems like this. I suppose that it's rather an integration- than unit test.

编辑:已经有两个非常有用的答案,但我想肯定还有更多的技巧.有没有人有关于何时/多久调用 TimeZone.getDefault 的权威信息(请参阅 Jon Skeets 答案的评论)?

Edit: There are already two very helpful answers, but I guess there must be more techniques out there. Does anybody have authoritative information about when/how often TimeZone.getDefault will be called (see the comments for Jon Skeets answers)?

注意:尽管这个问题有一个可接受的答案,但我并不完全确定该接受哪个答案.即使有这种接受,我也希望看到更多的想法和技术.

Note: Even though this question has an accepted answer, I was not completely sure, which answer to accept. Even with this acceptance I'd like to see more ideas and techniques.

感谢您的意见!

推荐答案

我建议您查看 JodaTime 提供了一些糖来帮助更清晰地管理代码中的日期/时间/时区类型问题.

I would recommend you check out JodaTime which provides some sugar to help manage Date / Time / TimeZone type issues more legibly in your code.

我们在整个测试和生产过程中都使用这些,因为它如何为日期/时间问题增强本机 Java API 是无与伦比的.在测试中使用这些在 JUnit 中工作正常

We use these throughout test and production since how it boosts the native Java API for Date/Time issues is unparalleled. Using these in tests works fine within JUnit

这篇关于测试正确的时区处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆