简体   繁体   English

如果我以 UTC +0 保存所有日期,如何在不同时区获得一致的数据

[英]How do I get consistent data across different timezones if I am saving all the dates in UTC +0

I have a system where users from Washington DC can create a post .我有一个系统,来自华盛顿特区的用户可以在其中创建post This post is saved in my system in UTC +0 time.这篇文章在 UTC +0 时间保存在我的系统中。 Then, I can use a reporting system which will give me info about every created post in a certain date range.然后,我可以使用一个报告系统,它会为我提供有关特定日期范围内创建的每个帖子的信息。 Lets say I select a date range from March 21st 00:00:00 to March 28th 23:59:59 but in my system someone created a post on March 28th 22:30:00 Washington DC time.假设我 select 的日期范围从March 21st 00:00:00March 28th 23:59:59 ,但在我的系统中,有人在华盛顿特区时间March 28th 22:30:00创建了一个帖子。 Washington DC is several hours behind UTC, so this post would be saved at around March 29th 02:30:00 , and so when I generate the report for March 21st to March 28th, I will not get the correct result because there is 1 post that has been created on March 28th Washington time, but that is March 29th UTC +0 time.华盛顿特区比 UTC 晚几个小时,所以这篇文章会在March 29th 02:30:00左右保存,所以当我生成 3 月 21 日至 3 月 28 日的报告时,我不会得到正确的结果,因为有 1 篇文章它是在华盛顿时间 3 月 28 日创建的,但那是 3 月 29 日 UTC +0 时间。

I first solved this by obtaining the UTC offset of the client and sending it to the server, and so adding that offset to my date range:我首先通过获取客户端的 UTC 偏移量并将其发送到服务器来解决这个问题,然后将该偏移量添加到我的日期范围:

// JavaScript
"offsetHours" : parseInt(new Date().getTimezoneOffset() / -60)
"offsetMinutes" : (new Date().getTimezoneOffset() / -60) % 1 * 60
// Python
_range["from"] = strToDate(_range["from"]) - datetime.timedelta(hours = int(request.headers["offsetHours"]), minutes=int(request.headers["offsetMinutes"]))

This solved the issue, but it raised another.这解决了这个问题,但它提出了另一个问题。 Now if I generate a report for the same time range (from 21st to 28th March) from 2 different timezones, I will get different results.现在,如果我从 2 个不同的时区生成相同时间范围(从 3 月 21 日到 28 日)的报告,我会得到不同的结果。 This is due to the fact that the 2 users have different offsets and so they affect the from range in different intervals.这是因为 2 个用户具有不同的偏移量,因此它们会在不同的时间间隔内影响起始from

Is there any solution to this problem?这个问题有什么解决办法吗?

You're not necessarily describing a problem , but rather a side effect of how local times around the world work.您不一定要描述问题,而是世界各地当地时间如何运作的副作用。

At any given time, there is usually more than one "date" in effect somewhere in the world.在任何给定时间,世界上的某个地方通常有不止一个“日期”生效。 If you are saving the timestamp of an event that took place, and you have customers around the world, you're not necessarily saving it with the same date that the user thought it was in their own time zone.如果您要保存已发生事件的时间戳,并且您的客户遍布世界各地,那么您不一定要使用用户认为它在他们自己的时区中的相同日期来保存它。 This is true whether you align the timestamps to UTC or to a specific time zone.无论您将时间戳与 UTC 还是与特定时区对齐,都是如此。

Therefore, you must make a business decision about how your application is intended to work.因此,您必须就应用程序的工作方式做出业务决策。 Do you want your daily reports to reflect posts that were made within a UTC day, or within the day according to the time zone of your business's headquarters?您是否希望您的每日报告反映在 UTC 日内发布的帖子,或根据您的企业总部的时区在一天内发布的帖子? Then store the timestamp in UTC and (optionally) adjust to your business's time zone before or during reporting.然后将时间戳存储在 UTC 中,并(可选)在报告之前或期间调整到您企业的时区。

If however you want the daily reports to reflect the date in the user's time zone, then you might want to also store the user's time zone ID (such as America/New_York - not a numeric offset) so that you could convert to that.但是,如果您希望每日报告反映用户时区中的日期,那么您可能还希望存储用户的时区 ID(例如America/New_York - 不是数字偏移量),以便您可以转换为那个。 Keep in mind that if user's are in different time zones, your reports might look strange when examined from a single time zone's perspective.请记住,如果用户位于不同的时区,则从单个时区的角度来看,您的报告可能看起来很奇怪。

Another technique that is often used (primarily for performance reasons, but also for clarity of logic), is to keep both a UTC-based timestamp and a separate field for the "business date" that applies.另一种经常使用的技术(主要是出于性能原因,但也是为了逻辑清晰)是保留基于 UTC 的时间戳适用的“业务日期”的单独字段。 Usually such a field is just a date field, storing a value such as 2021-03-29 without any time or time zone.通常这样的字段只是一个日期字段,存储一个诸如2021-03-29类的值,没有任何时间或时区。 This field can be pre-converted to a time zone according to whatever rules you decide are applicable for your business.该字段可以根据您决定适用于您的业务的任何规则预先转换为时区。 It then becomes a great candidate for an index and works well for range queries for daily reports.然后,它成为索引的绝佳候选者,并且适用于每日报告的范围查询。

In the end - there is no one "right" way to do it.最后 - 没有一种“正确”的方法可以做到这一点。 You have to decide what works best for your use case.您必须决定什么最适合您的用例。 If you are working for a larger company and unsure of the business requirements, then ask someone who might already perform a similar activity manually.如果您在一家较大的公司工作并且不确定业务需求,请询问可能已经手动执行类似活动的人。 (Often this is an accounting or sales person in a larger organization.) (通常是较大组织中的会计或销售人员。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使各种浏览器的toLowerCase()和toUpperCase()保持一致 - How do I make toLowerCase() and toUpperCase() consistent across browsers 从UTC偏移量获取所有可能的时区 - Get all possible timezones from UTC offset 我如何使一个div在该div中具有相同的图像大小? 但整个网站上所有图片的大小仍然一致吗? - How do i make a div to have the same image size in that div? but still have a consistent size for all images across the website? 我如何从 windows 时区获取当前日期和时间 - how do i get the current date and time from windows timezones 如何在JavaScript中跨时区比较日期? - How to compare dates across timezones in JavaScript? 如何在 JavaScript 中获取 UTC 时间戳? - How do I get a UTC Timestamp in JavaScript? 如何使用时刻获取不同时区的日期之间的差异? - How to get difference between dates in different timezones using moment? 如何将这些字符串解析为UTC日期? - How can I parse these strings into UTC dates? 如何在不同的屏幕尺寸上保持一致的字体大小? - How can I have consistent font size across different screen sizes? 我怎样才能每周洗牌并在所有设备上保持一致? - How can I shuffle a list weekly and have it consistent across all devices?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM