Recommended date format for REST GET API

前端 未结 5 1298
一向
一向 2020-12-12 16:13

What\'s the recommended timestamp format for a REST GET API like this:

http://api.example.com/start_date/{timestamp}

I think the actual dat

5条回答
  •  鱼传尺愫
    2020-12-12 16:49

    Every datetime field in input/output needs to be in UNIX/epoch format. This avoids the confusion between developers across different sides of the API.

    Pros:

    • Epoch format does not have a timezone.
    • Epoch has a single format (Unix time is a single signed number).
    • Epoch time is not effected by daylight saving.
    • Most of the Backend frameworks and all native ios/android APIs support epoch conversion.
    • Local time conversion part can be done entirely in application side depends on the timezone setting of user's device/browser.

    Cons:

    • Extra processing for converting to UTC for storing in UTC format in the database.
    • Readability of input/output.
    • Readability of GET URLs.

    Notes:

    • Timezones are a presentation-layer problem! Most of your code shouldn't be dealing with timezones or local time, it should be passing Unix time around.
    • If you want to store a humanly-readable time (e.g. logs), consider storing it along with Unix time, not instead of Unix time.

提交回复
热议问题