RESTful服务最佳实践——(十)

日期/时间处理

如果没有妥善地、一致地处理好日期和时间的话,这将成为一个大麻烦。时区问题总是能轻易发生,并且由于日期在JSON中只是字符串的,所以如果格式是未知的、不一致的、未指定的,那么分析日期便成了一个问题。

在接口内部,这些(时间的)服务应该被存储、运行和缓存,例如UTC或GMT时间戳。这将减轻日期和时间上的问题。

Body内容中的时间序列化

有一个简单的方法可以解决这些问题–在字符串中始终用一些相同的格式,包括时间片(带有时区信息)。ISO8601时间点格式是一种不错的解决的方案,它使用了全面提高版的格式,包括小时、分钟、秒以及秒的小数部分(例如yyyy-MM-dd’T’HH:mm:ss.SSS’Z’)。ISO8601被推荐使用于在REST服务body内容(请求和响应均包括)中呈现的所有日期中。

恰巧,对于那些作为搭载在JAVA上的服务系统,DateAdapterJ库使用它的J日期适配器(DateAdapterJ)、ISO8601时间点适配器和HTTP头时间戳适配器(HttpHeadeTimestampAdapter)实现类,很容易就能分别分析和格式化ISO8601的日期、时间点、HTTP1.1头(RFC1123)格式。这些可以从https://github.com/tfredrich/DateAdapterJ上下载。

对于那些创建的基于浏览器的用户界面来说,ECMAScript5规范一开始便包含了JavaScript分析和创造ISO8601日期的内容,所以它该成为我们所说的主流浏览器所遵从的方式。当然,如果你要支持不能分析这些日期的旧版浏览器,JavaStript库或一个有趣的普遍解释也能够做到。关于可以解析和生成ISO8601时间点的JavaStript库的一些例子:

http://momentjs.com/
http://www.datejs.com/

在HTTP头中的时间序列化

然而以上的建议仅对HTTP的请求或相应内容中的JSON和XML内容有效,HTTP规范对HTTP头使用另一种不同的格式。被RFC1123更替的RFC822中指出,HTTP头格式包括了多种日期、时间和日期-时间格式。然而它总是被推荐使用时间戳格式,在你的请求头中看起来像这样结尾:

Sun, 06 Nov 1994 08:49:37 GMT。

不幸的是,它的格式没有考虑毫秒或者秒的十进制小数。Java 简单日期格式(SimpleDataFormat)的区分串是:“EEE, dd MMM yyyy HH:mm:ss ‘GMT’”


原文如下


Date/Time Handling

Dates and timestamps can be a real headache if not dealt with appropriately and consistently. Timezone issues can crop up easily and since dates are just strings in JSON payloads, parsing is a real issue if the format isn’t known, consistent or specified.

Internally, services should store, process, cache, etc. such timestamps in UTC or GMT time. This alleviates timezone issues with both dates and timestamps.

Date/Time Serialization In Body Content

There’s an easy way around all of this—always use the same format, including the time portion (along with timezone information) in the string. ISO 8601 time point format is a good solution, using the fully-enhanced format that includes hours, minutes, seconds and a decimal fraction of seconds (e.g. yyyy-MM-dd’T’HH:mm:ss.SSS’Z’). It is recommended that ISO 8601 be used for all dates represented in REST service body content (both requests and responses).

Incidentally, for those doing Java-based services, the DateAdapterJ library easily parses and formats ISO8601 dates and time points and HTTP 1.1 header (RFC 1123) formats, with its DateAdapter, Iso8601TimepointAdapter and HttpHeaderTimestampAdapter implementation classes, respectively. It can be downloaded at https://github.com/tfredrich/DateAdapterJ.

For those creating browser-based UIs, the ECMAScript 5 specification includes parsing and creating ISO8601 dates in JavaScript natively, so it should be making its way into all mainstream browsers as we speak. If you’re supporting older browsers that don’t natively parse those dates, a JavaScript library or fancy regular expression is in order. A couple of sample JavaScript libraries that can parse and produce ISO8601 Timepoints are:

http://momentjs.com/
http://www.datejs.com/

Date/Time Serialization In HTTP Headers

While the above recommendation works for JSON and XML content in the content of and HTTP request or response, the HTTP specification utilizes a different format for HTTP headers. Specified in RFC 822 which was updated by RFC 1123, that format includes various date, time and date-time formats. However, it is recommended to always use a timestamp format, which ends up looking like this in your request headers:

Sun, 06 Nov 1994 08:49:37 GMT

Unfortunately, it doesn’t account for a millisecond or decimal fraction of a second in its format. The Java SimpleDateFormat specifier string is: “EEE, dd MMM yyyy HH:mm:ss ‘GMT’”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值