在客户端-服务器应用程序中处理时间

发布于 2025-01-05 00:50:59 字数 424 浏览 2 评论 0原文

对于我正在开发的 iPhone 聊天应用程序,我想知道如何处理所有时区问题。 我的意思是如何使当 client1 向 client2 发送消息时,他们都可以看到消息旁边的正确时间(日期)。

到目前为止,这就是我认为我需要做的:

  1. 客户端1向服务器发送消息,客户端1使用它的本地时间来显示消息旁边的日期。
  2. 服务器(c#)获取消息并将其保存到数据库中,同时使用 DateTime.UtcNow 将当前时间保存在消息旁边。
  3. 当client2登录时,他从服务器获取带有UTC时间的消息,并将其转换为本地时间。

这是这样做的方法吗? 将日期从服务器发送到客户端的最佳方式是什么?在服务器中,日期作为 DateTime 对象保存到数据库中,我可以将其转换为什么格式以将其发送到客户端?

For an iPhone chat app I'm working on, I want to know how to handle all the timezone issue.
I mean how to make that when client1 sends a message to client2, they both see the correct time (date) next to the message.

So far this is what i think i need to do:

  1. Client1 send message to server, client1 use it's local time to present the date next to the message.
  2. The server(c#) gets the message and save it to the database, also he save the current time next to the message with DateTime.UtcNow.
  3. When client2 login, he get the message from the server with the UTC time, and convert it to its local time.

Is this the way to do that?
What is the best way to send the date from server to client? In the server the date is save to the database as DateTime object, to what format can I convert it to send it to the client?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红玫瑰 2025-01-12 00:50:59

这里有几个选项,具体取决于报告时间需要的准确程度:

  1. 如果需要非常准确,我将只依赖服务器时间。当您从 client1 向服务器发送消息时,让服务器在其响应中返回与该消息关联的 UTC 时间。因此,我发送了消息“hello”并收到 200 OK,其中响应正文或扩展标头或其他内容中包含 UTC 时间。然后client1显示这个时间(转换为本地时间)和client2显示相同的时间(转换为本地时间)。
  2. 如果准确性不那么重要,那么从 client1 发送消息然后使用您本地的当前时间会更容易。您仍然可以在服务器上执行 UTC 操作并让 client1 使用该时间,但这样您就不需要查看对 client1 消息的响应中的任何内容。

关于来回发送日期,请查看 NSDateFormatter。您可以使用可以轻松使用的格式,或者在服务器上执行任何最简单的操作,并使用格式化程序上的自定义 dateFormat 属性来根据需要进行解析。

You have a couple options here depending on how accurate the reported time needs to be:

  1. If it needs to be really accurate, I would rely only on server time. When you sent the message to the server from client1, have the server return the UTC time it associated with the message in its response. So I sent the message "hello" and receive a 200 OK with the UTC time in the response body or in an extended header or something. Then client1 shows this time (converted to local time) and client2 the same time (converted to local time).
  2. If accuracy isn't as important, it's easier to just send the message from client1 and then use your local current time. You can still do the UTC thing on the server and have client1 use that time, but this way you don't need to look at anything in the response to client1's message.

In terms of sending dates back and forth, check out the documentation on NSDateFormatter. You can use a format that it can consume easily or do whatever's easiest on the server and use a custom dateFormat property on the formatter to parse as you please.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文