JSON中Duration的良好格式是什么? [英] What is a good format for Duration in JSON?

查看:263
本文介绍了JSON中Duration的良好格式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到JSON没有实际日期格式,并且使用ISO 8601是JavaScript的最佳选择。持续时间如何? JavaScript没有内置格式。

I realise that JSON has no real date format and that using ISO 8601 is a good bet given that's what JavaScript uses. What about a duration? JavaScript has no built in format.


  1. 我遇到了 ISO 8601持续时间例如 P3Y6M4DT12H30M5S 我没见过在野外使用很多。它看起来也很冗长且难以阅读。据我所知,如果需要的话,它也不支持毫秒。

  2. 我正在使用C#,它的 TimeSpan 类型输出 1.02:03:04.0050000 1天,2小时,3分钟,4秒和5毫秒。

  3. 我可以使用该数字以秒或毫秒为单位的整数。这仅是完全机器可读的,如果您使用秒或毫秒而不标记其值,则并不明显。

  1. I came across ISO 8601 Durations e.g. P3Y6M4DT12H30M5S which I haven't seen used much in the wild. It also looks very verbose and difficult to read. As far as I can see it also does not support milliseconds if you needed that.
  2. I'm using C# whose TimeSpan type outputs 1.02:03:04.0050000 for 1 day, 2 hours, 3 minutes, 4 seconds and 5 milliseconds.
  3. I could use the number of seconds or milliseconds as an integer. This is completely machine readable only and it's not obvious if you are using seconds or milliseconds without labelling the value as such.

几乎没有野外见过的第一种格式。第二个对我来说似乎更直观,但是我担心它在.NET之外并不为人所知。第三种格式可能是跨平台最友好的格式,但是完全不可读。

I've hardly ever seen the first format in the wild. The second seems more intuitive to me but I'm worried that it's not as well known outside of .NET. The third format is probably the most cross platform friendly but totally not human readable.

推荐答案

基本上,持续时间是毫秒数或刻度(如果需要更高的精度)。

Basically, duration is a number of milliseconds or ticks (if you need higher precision).


1个刻度是100纳秒。一毫秒有10,000个刻度。

1 tick is 100 nanoseconds. There are 10,000 ticks in a millisecond.


ISO 8601:现有标准


最好的方法是使用您提到的 ISO 8601 通用标准。

什么对您来说更容易阅读和更改?

What is easier to read and change for you?


  • 1036800000 ms

  • P12D

您可以使用ISO 8601持续时间,例如 P1Y2M10DT2H30M 不同的方式:

You can use ISO 8601 duration like P1Y2M10DT2H30M in different ways:

  • Handle by moment.js in JavaScript
  • Pass in JSON format as a string value
  • Handle by NodaTime in C#
  • Store in DB

请记住,在从夏令时切换到夏令时时,ISO 8601也解决了该问题。

Keep in mind that ISO 8601 also resolves the issue when switching from or to Daylight saving time.

PT36H P1DT12H 不同。


Schema.org


schema.org上Internet上的结构化数据有不同的模式。其中一些正在使用持续时间,应以 ISO 8601持续时间格式

这篇关于JSON中Duration的良好格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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