Epoch or iso8601 date format?

我只是一个虾纸丫 提交于 2019-12-01 06:29:21

问题


For passing times in JSON to/from a web API, why would I choose to use an ISO8601 string instead of simply the UTC epoch value? For example, both of these are the same:

Epoch = 1511324473
iso8601 =   2017-11-22T04:21:13Z

The epoch value is obviously shorter in length, which is always good for mobile data usage, and it's pretty simple to convert between epoch values and the language's local Date type variable.

I'm just not seeing the benefit to using an ISO string value.


回答1:


Both are unambiguous and easy to parse in programs. The benefit of epoch like you have mentioned is that it is smaller and will be faster to process in your program. The downside is it means nothing to humans.

iso8901 dates are easy to read on their own and don't require the user to translate a number in to a recognizable date. The size increase in iso8601 is unnoticeable when compared to much much larger things like images.

Personally I would pick ease of reading over speed for an API as it will cut down on debugging time while inspecting values sent and received. In another situation such as passing times around internally you may wish to choose the speed of an integer over text so it depends which you think will be more useful.




回答2:


Unix/Epoch Time
+ Compact
+ Easy to do arithmetic actions without any libraries, i.e. var tomorrow=now()+60*60*24
- Not human readable
- Cannot represent dates before 1 January 1970
- Cannot represent dates after 19 January 2038 (if using Int32)
- Timezone and offset are "external" info, there is ambiguity if the value is UTC or any other offset.
- Officially the spec supports only seconds.
- When someone change the value to milliseconds for better resolution, there is an ambiguity if the value is seconds or milliseconds.
- Older then ISO 8601 format
- Represents seconds/milliseconds since 1970 (as opposed to instant in time)

ISO 8601 Time
+ Human readable + Represents instant in time (as opposed to seconds/milliseconds since 1970)
+ Newer then Unix time format
+ Specifies representation of date, time, date-time, duration and interval!
+ Supports an offset representation
- Less compact
- For any arithmetic action, reach library is required (like java.time.OffsetDatetime)



来源:https://stackoverflow.com/questions/47426786/epoch-or-iso8601-date-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!