关于在 DB 中保存日期时间和时区信息有很多问题,但更多的是在整体层面上。在这里,我想谈谈一个具体的案例。
系统规格
数据库中需要包含业务规则
ORDR-13432-Year-Month-Day
)。精确的计算目前并不重要,重要的是它取决于租户的本地日期时间我们最初的想法
进场1
为每个租户保存本地租户的日期时间会很好,但是我们会遇到以下查询问题:
SELECT * FROM ORDERS WHERE OrderDateTime BETWEEN UTCDateTime1 AND UTCDateTime2
It's problematic because OrderDateTime
in this query means different moment in time, based on tenant. Of course, this query might include join to Tenants
table to get local datetime offset which would then calculate OrderDateTime
on the fly to make adjustments. It's possible, but not sure if it's a good way to do it?
Approach 2
Let's take extreme example; let's say tenant is 6 hours ahead of UTC and his local datetime is 2017-01-01 02:00
.
UTC would be 2016-12-31 20:00
. Order placed at that moment should get OrderNumber 'ORDR-13432-2017-1-1'
but if saving UTC it would get ORDR-13432-2016-12-31
.
In this case, at the moment of creating Order in DB, we should get UTC datetime, tenants offset and compile OrderNumber based on recalculated tenants localtime but still save DateTime column in UTC.
Questions
[UPDATE]
Based on comments from Gerard Ashton and Hugo:
Initial question was not clear with respect to the detail if tenant can change timezone or not and what happens if political authority changes the timezone properties or some terirory's timezone. Of course that is of an extreme importance, but it is not in the center of this question . We might address that in a separate question.
For the sake of this question, lets assume tenant will not change location. Timezone properties or timezone itself for that location might change and those changes will be handled in the system separately from this question.