iso8601-js 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ language: node_js
2
+ env: TZ=PST+8
3
+ node_js:
4
+ - 0.6
@@ -1,17 +1,23 @@
1
+ ## 0.2.1 (2011-12-29)
2
+
3
+ * Polyfills for **Date.prototype.toJSON** and **Date.now**.
4
+
5
+
1
6
  ## 0.2.0 (2011-12-27)
2
7
 
3
8
  * API changed to match [ECMAScript 5.1][ecmascript] ISO 8601 support for the
4
9
  Date object.
5
10
  * `Date.prototype.toISO8601UTCString` is now `Date.prototype.toISOString`.
6
- * `Date.prototype.toISO8601LocaleString` has been removed. Similar formatting
7
- is available by passing *true* to `Date.prototype.toISO8601String` which
8
- allows other customizations as well.
11
+ * Removed `Date.prototype.toISO8601LocaleString`. Similar formatting is
12
+ available by passing *true* for *localTimezone* to
13
+ `Date.prototype.toISO8601String`.
9
14
  * ISO 8601 UTC formatting falls back to native support if available. Performance
10
15
  improvement on modern browsers for common usages.
11
16
  * Stricter browser test for native ISO 8601 parsing. Ensures timezone offsets
12
17
  are correctly handled. Fixes Firefox issues.
13
18
  * Unit tests. Suite runs via node or browser harness.
14
19
 
20
+
15
21
  ## 0.1.0 (2011-12-23)
16
22
 
17
23
  * First release. Extracted from the Do web client, based on Paul Gallagher's
data/README.md CHANGED
@@ -1,52 +1,50 @@
1
- iso8601.js
2
- ==========
1
+ iso8601.js [![Build Status][buildstatus]][travis]
2
+ =================================================
3
3
 
4
- Partial ECMAScript 5.1 Date object cross-browser polyfill to add
5
- a [ISO 8601][iso8601] support to **Date.parse** and
6
- **Date.prototype.toISOString**. Originally based on Paul Gallagher's
7
- a [rfc3339date.js library][rfc3339date.js].
4
+ Partial cross-browser polyfill to add [ISO 8601][iso8601] support to
5
+ **Date.parse** and **Date.prototype.toISOString** as defined in ECMAScript 5.1.
6
+ Originally based on Paul Gallagher's [rfc3339date.js library][rfc3339date.js].
8
7
 
9
8
  Supports only the subset of ISO 8601 described in
10
- a [ECMAScript 5.1 section 15.9.4.2][ecmascript], [RFC 3339][rfc3339], and the
11
- a [Date and Time Formats W3C NOTE][w3c-note].
9
+ [ECMAScript 5.1 section 15.9.4.2][ecmascript], [RFC 3339][rfc3339], and the
10
+ [Date and Time Formats W3C NOTE][w3c-note].
12
11
 
13
12
 
14
13
  ## Usage
15
14
 
16
- Download [iso8601.js][downloads] and include as usual before other scripts:
15
+ Download [iso8601.js][downloads] and include before other scripts:
17
16
 
18
17
  ```html
19
18
  <script src="iso8601.min.js"></script>
20
19
  ```
21
20
 
22
- If you're using [Sprockets 2][sprockets], you can use the gem version of the
23
- library. Add to your Gemfile:
21
+ Using [Sprockets 2][sprockets]? There's a gem version. Add to your Gemfile:
24
22
 
25
23
  ```ruby
26
24
  gem 'iso8601-js'
27
25
  ```
28
26
 
29
- If using Rails 3.1+, the library is automatically added to your asset paths.
30
- Otherwise add `ISO8601JS.assets_path` to your Sprockets environment:
27
+ For Rails 3.1+, the library is automatically added to your asset paths. For
28
+ other frameworks, add `ISO8601JS.assets_path` to your Sprockets environment:
31
29
 
32
30
  ```ruby
33
31
  env = Sprockets::Environment.new # or however you initialize Sprockets
34
32
  env.append_path ISO8601JS.assets_path
35
33
  ```
36
34
 
37
- Then require it from the appropriate JavaScript or CoffeeScript files with the
38
- `= require` Sprockets directive:
35
+ Include it in your JavaScript or CoffeeScript files with the `= require`
36
+ Sprockets directive:
39
37
 
40
38
  JavaScript:
41
39
 
42
40
  ```javascript
43
- //= require iso8601-js
41
+ //= require iso8601
44
42
  ```
45
43
 
46
44
  CoffeeScript:
47
45
 
48
46
  ```coffee-script
49
- #= require iso8601-js
47
+ #= require iso8601
50
48
  ```
51
49
 
52
50
 
@@ -54,19 +52,18 @@ CoffeeScript:
54
52
 
55
53
  Parse ISO 8601 date/time strings two ways:
56
54
 
57
- * **Date.parseISO8601("...")** builds a Date instance from a string containing
58
- an ISO 8610 date.
59
- * **Date.parse("...")** is polyfilled to attempt ISO 8601 parsing on the string
60
- before using the browser's native `Date.parse`. Returns the number of
61
- milliseconds between the Unix epoch and the date.
55
+ * **Date.parseISO8601** creates a Date instance from a ISO 8601 date string.
56
+ * **Date.parse** is polyfilled to attempt ISO 8601 parsing before using the
57
+ browser's native `Date.parse`. Returns the number of milliseconds between the
58
+ Unix epoch and the date. [*EC5 15.9.4.2*][ec15.9.4.2]
62
59
 
63
60
  Examples:
64
61
 
65
62
  ```js
66
- console.log(Date.parseISO8601("2010-07-20T15:00:00Z"))
63
+ Date.parseISO8601("2010-07-20T15:00:00Z")
67
64
  // => Tue Jul 20 2010 08:00:00 GMT-0700 (PDT)
68
65
 
69
- console.log(Date.parse("2010-07-20T15:00:00Z"));
66
+ Date.parse("2010-07-20T15:00:00Z")
70
67
  // => 1307834445456
71
68
  ```
72
69
 
@@ -75,7 +72,11 @@ console.log(Date.parse("2010-07-20T15:00:00Z"));
75
72
 
76
73
  Format ISO 8601 date strings directly from Date instances:
77
74
 
78
- * **Date.prototype.toISOString** generates an ISO 8601 string in UTC.
75
+ * **Date.prototype.toISOString** generates an ISO 8601 UTC string.
76
+ [*EC5 15.9.5.32*][ec15.9.5.43]. Throws `RangeError` for invalid dates.
77
+ * **Date.prototype.toJSON** generates an ISO 8601 UTC string for use by
78
+ JSON.stringify. Returns `null` for invalid dates.
79
+ [*EC5 15.9.5.44*][ec15.9.5.44]
79
80
  * **Date.prototype.toISO8601String** generates an ISO 8601 string with
80
81
  formatting options:
81
82
  * *localTimezone*: Use local timezone with offset or UTC? Defaults to *false*.
@@ -85,16 +86,34 @@ Format ISO 8601 date strings directly from Date instances:
85
86
  Examples:
86
87
 
87
88
  ```js
88
- var date = Date.parseISO8601("2010-07-20T15:00:00Z");
89
+ var date = Date.parseISO8601("2010-07-20T15:00:00Z")
89
90
 
90
- console.log(date.toISOString());
91
+ date.toISOString()
91
92
  // => "2010-07-20T15:00:00.000Z"
92
93
 
93
- console.log(date.toISO8601String(true, true, false));
94
+ date.toJSON()
95
+ // => "2010-07-20T15:00:00.000Z"
96
+
97
+ date.toISO8601String(true, true, false)
94
98
  // => "2010-07-20T08:00:00-07:00"
95
99
  ```
96
100
 
97
101
 
102
+ ## Other
103
+
104
+ Other EC5 Date-related polyfills:
105
+
106
+ * **Date.now** returns the number of milliseconds since the Unix epoch.
107
+ [*EC5 15.9.4.4*][ec15.9.4.4]
108
+
109
+ Examples:
110
+
111
+ ```js
112
+ Date.now()
113
+ // => 1325174662624
114
+ ```
115
+
116
+
98
117
  ## Caveats
99
118
 
100
119
  The Date constructor is not polyfilled to support ISO 8601. Avoid using `new
@@ -112,6 +131,8 @@ Patches and bug reports are always welcome. Just send a
112
131
 
113
132
 
114
133
 
134
+ [buildstatus]: https://secure.travis-ci.org/Do/iso8601.js.png?branch=master "Build status"
135
+ [travis]: http://travis-ci.org/Do/iso8601.js
115
136
  [iso8601]: http://en.wikipedia.org/wiki/ISO_8601
116
137
  [rfc3339date.js]: https://github.com/tardate/rfc3339date.js
117
138
  [ecmascript]: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
@@ -119,6 +140,10 @@ Patches and bug reports are always welcome. Just send a
119
140
  [w3c-note]: http://www.w3.org/TR/NOTE-datetime
120
141
  [downloads]: https://github.com/Do/iso8601.js/downloads
121
142
  [sprockets]: https://github.com/sstephenson/sprockets
143
+ [ec15.9.4.2]: http://es5.github.com/#x15.9.4.2
144
+ [ec15.9.5.43]: http://es5.github.com/#x15.9.5.43
145
+ [ec15.9.5.44]: http://es5.github.com/#x15.9.5.44
146
+ [ec15.9.4.4]: http://es5.github.com/#x15.9.4.4
122
147
  [coffeescript]: http://coffeescript.org/
123
148
  [pullrequests]: https://github.com/Do/iso8601.js/pulls
124
149
  [issues]: https://github.com/Do/iso8601.js/issues
@@ -1,3 +1,3 @@
1
1
  module ISO8601JS
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -47,9 +47,8 @@ pad = (number, length = 2) ->
47
47
  result = '0' + result
48
48
  result
49
49
 
50
- # Unit tests to check native ISO 8601 support
50
+ # Unit test to check native ISO 8601 parsing support
51
51
  supportsISOParsing = Date.parse?('2011-06-11T23:20:45.456-0700') is 1307859645456
52
- supportsISOFormatting = Date::toISOString?
53
52
 
54
53
 
55
54
 
@@ -119,19 +118,38 @@ Date::toISO8601String = (localTimezone = false, separators = true, milliseconds
119
118
  #
120
119
  # Format the date in UTC ISO 8601 / RFC 3339.
121
120
  #
122
- # Defined in ECMAScript 5.1 section 15.9.5.43. An implementation is set only if
123
- # the browser lacks native support.
121
+ # Defined in ECMAScript 5.1 15.9.5.43. An implementation is set only if the
122
+ # browser lacks native support.
124
123
  #
125
124
  # Examples
126
125
  #
127
126
  # new Date().toISOString()
128
127
  # // => "2010-07-25T11:51:31.427Z"
129
128
  #
130
- unless supportsISOFormatting
129
+ unless Date::toISOString
131
130
  Date::toISOString = Date::toISO8601String
132
131
 
133
132
 
134
133
 
134
+ # Date.prototype.toJSON
135
+ #
136
+ # Format the date in ISO 8601 UTC suitable for use by JSON.stringify. Returns
137
+ # null for invalid dates. See ECMAScript 5.1 15.9.5.44.
138
+ #
139
+ # Examples
140
+ #
141
+ # new Date().toJSON()
142
+ # // => "2010-07-25T11:51:31.427Z"
143
+ #
144
+ unless Date::toJSON
145
+ Date::toJSON = (key) ->
146
+ if isFinite(@valueOf())
147
+ @toISOString()
148
+ else
149
+ null # Spec requires returning null for non-finite values
150
+
151
+
152
+
135
153
  # Date.parseISO8601
136
154
  #
137
155
  # Parses ISO8601 / RFC 3339 date strings to a Date object. Uses native browser
@@ -234,3 +252,18 @@ else if not supportsISOParsing
234
252
  result = parseISO8601(input)?.getTime()
235
253
  result = oldDateParse(input) if not result and oldDateParse
236
254
  result
255
+
256
+
257
+
258
+ # Date.now
259
+ #
260
+ # Returns the Number value (milliseconds since the Unix epoch) of the current
261
+ # time. See ECMAScript 5.1 15.9.4.4.
262
+ #
263
+ # Examples
264
+ #
265
+ # Date.now()
266
+ # // => 1325174662624
267
+ #
268
+ unless Date.now
269
+ Date.now = -> new Date().getTime()
@@ -36,5 +36,8 @@
36
36
  "uglify-js": "1.2.2",
37
37
  "mocha": "0.7.1",
38
38
  "chai": "0.1.4"
39
+ },
40
+ "scripts": {
41
+ "test": "mocha --reporter spec"
39
42
  }
40
43
  }
@@ -1,9 +1,12 @@
1
- # Tests for the polyfill parsing and formatting functions. Browser native
2
- # support detection and usage untested.
1
+ # Tests for the iso8601.js library
3
2
  #
4
3
  # For proper testing of timezone offsets, the suite must be run in the PST (non
5
4
  # daylight savings) timezone environment, e.g. TZ='PST+8'.
6
5
  #
6
+ # Functions are tested directly where possible. Some of these tests won't test
7
+ # the polyfill functions unless run in a non-ECMAScript 5.1 environment -
8
+ # they'll just test native versions. Be sure to run the suite in older engines.
9
+ #
7
10
 
8
11
  chai = @chai || require('chai')
9
12
  assert = chai.assert
@@ -12,8 +15,10 @@ require('../lib/iso8601') if require?
12
15
 
13
16
  describe 'Date', ->
14
17
 
15
- # Date fixture: Sat Jun 18, 2011 02:20:45.678 PST
16
- date = new Date(1308392445678)
18
+ # Date fixtures
19
+ date = new Date(1308392445678) # Sat Jun 18, 2011 02:20:45.678 PST
20
+ invalidDate = new Date('not a date') # Doesn't throw exception on creation
21
+
17
22
 
18
23
  describe 'toISO8601String', ->
19
24
 
@@ -36,12 +41,23 @@ describe 'Date', ->
36
41
  assert.equal '2011-06-18T02:20:45.678-08:00', date.toISO8601String(true, true, true)
37
42
 
38
43
  it 'throws RangeError for Invalid Dates', ->
39
- fn = -> new Date('not a date').toISO8601String()
44
+ fn = -> invalidDate.toISO8601String() # Should throw exception when called
40
45
  expect(fn).to.throw RangeError
41
46
 
42
47
 
48
+ describe 'toJSON', ->
49
+
50
+ it 'returns the UTC ISO 8601 string for valid dates', ->
51
+ assert.equal '2011-06-18T10:20:45.678Z', date.toJSON()
52
+
53
+ it 'returns null for invalid dates', ->
54
+ assert.equal null, invalidDate.toJSON()
55
+
56
+
43
57
  describe 'parseISO8601', ->
44
58
 
59
+ # Using Date.parseISO8601(..., false) to parse without browser fallback.
60
+
45
61
  it 'parses UTC ISO 8601 strings', ->
46
62
  assert.equal date.getTime(), Date.parseISO8601('2011-06-18T10:20:45.678Z', false).getTime()
47
63
 
@@ -69,3 +85,8 @@ describe 'Date', ->
69
85
 
70
86
  it 'falls back to native Date.parse for non ISO strings', ->
71
87
  assert.equal 807926400000, Date.parse('Wed, 09 Aug 1995 00:00:00 GMT')
88
+
89
+
90
+ describe 'now', ->
91
+ it 'returns the current time in milliseconds since epoch', ->
92
+ assert.equal new Date().getTime(), Date.now()
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iso8601-js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,11 +11,11 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2011-12-27 00:00:00.000000000Z
14
+ date: 2011-12-29 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: sprockets
18
- requirement: &70341578450300 !ruby/object:Gem::Requirement
18
+ requirement: &70233797964100 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ~>
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: '2.0'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *70341578450300
26
+ version_requirements: *70233797964100
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: coffee-script
29
- requirement: &70341578449500 !ruby/object:Gem::Requirement
29
+ requirement: &70233797962980 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ~>
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: '2.2'
35
35
  type: :runtime
36
36
  prerelease: false
37
- version_requirements: *70341578449500
37
+ version_requirements: *70233797962980
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rake
40
- requirement: &70341578448740 !ruby/object:Gem::Requirement
40
+ requirement: &70233797961920 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ! '>='
@@ -45,7 +45,7 @@ dependencies:
45
45
  version: '0'
46
46
  type: :development
47
47
  prerelease: false
48
- version_requirements: *70341578448740
48
+ version_requirements: *70233797961920
49
49
  description: Adds cross-browser ISO 8601 support to the JavaScript Date object based
50
50
  on EC5.1. This is a Sprockets gem to simplify inclusion of the client-side library.
51
51
  email:
@@ -57,6 +57,7 @@ extensions: []
57
57
  extra_rdoc_files: []
58
58
  files:
59
59
  - .gitignore
60
+ - .travis.yml
60
61
  - CHANGELOG.md
61
62
  - Gemfile
62
63
  - LICENSE