Epoch or iso8601 date format?

前端 未结 2 1779
心在旅途
心在旅途 2020-12-16 18:43

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:

2条回答
  •  一向
    一向 (楼主)
    2020-12-16 19:23

    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 changes 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 since 1970 (as opposed to instant in time)

    - Precision of seconds

    ISO 8601 Time
    + Human readable

    + Represents instant in time, as opposed to seconds since 1970

    + Newer then Unix time format

    + Specifies representation of date, time, date-time, duration and interval!

    + Supports an offset representation

    + Precision of nanoseconds

    - Less compact

    - For any arithmetic actions, reach library is required (like java.time.OffsetDatetime)

提交回复
热议问题