date-format 1.2.0 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
package/.eslintrc CHANGED
@@ -1,24 +1,12 @@
1
1
  {
2
+ "extends": [
3
+ "eslint:recommended"
4
+ ],
2
5
  "env": {
3
6
  "node": true,
4
7
  "mocha": true
5
8
  },
6
9
  "plugins": [
7
- "mocha",
8
- "import"
9
- ],
10
- "rules": {
11
- "comma-dangle": 0,
12
- "indent": 2,
13
- "func-names": 0,
14
- "max-len": [1, 120, 2],
15
- "no-use-before-define": 1,
16
- "no-param-reassign": 0,
17
- "strict": 0,
18
- "import/no-extraneous-dependencies": ["error", {"devDependencies": ["test/*.js"]}],
19
- "mocha/no-exclusive-tests": "error"
20
- },
21
- "parserOptions": {
22
- "ecmaVersion": 5
23
- }
10
+ "mocha"
11
+ ]
24
12
  }
@@ -0,0 +1,70 @@
1
+ # For most projects, this workflow file will not need changing; you simply need
2
+ # to commit it to your repository.
3
+ #
4
+ # You may wish to alter this file to override the set of languages analyzed,
5
+ # or to provide custom queries or build logic.
6
+ #
7
+ # ******** NOTE ********
8
+ # We have attempted to detect the languages in your repository. Please check
9
+ # the `language` matrix defined below to confirm you have the correct set of
10
+ # supported CodeQL languages.
11
+ #
12
+ name: "CodeQL"
13
+
14
+ on:
15
+ push:
16
+ branches: [ master ]
17
+ pull_request:
18
+ # The branches below must be a subset of the branches above
19
+ branches: [ master ]
20
+ schedule:
21
+ - cron: '34 6 * * 1'
22
+
23
+ jobs:
24
+ analyze:
25
+ name: Analyze
26
+ runs-on: ubuntu-latest
27
+ permissions:
28
+ actions: read
29
+ contents: read
30
+ security-events: write
31
+
32
+ strategy:
33
+ fail-fast: false
34
+ matrix:
35
+ language: [ 'javascript' ]
36
+ # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37
+ # Learn more about CodeQL language support at https://git.io/codeql-language-support
38
+
39
+ steps:
40
+ - name: Checkout repository
41
+ uses: actions/checkout@v2
42
+
43
+ # Initializes the CodeQL tools for scanning.
44
+ - name: Initialize CodeQL
45
+ uses: github/codeql-action/init@v1
46
+ with:
47
+ languages: ${{ matrix.language }}
48
+ # If you wish to specify custom queries, you can do so here or in a config file.
49
+ # By default, queries listed here will override any specified in a config file.
50
+ # Prefix the list here with "+" to use these queries and those in the config file.
51
+ # queries: ./path/to/local/query, your-org/your-repo/queries@main
52
+
53
+ # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
54
+ # If this step fails, then you should remove it and run the build manually (see below)
55
+ - name: Autobuild
56
+ uses: github/codeql-action/autobuild@v1
57
+
58
+ # ℹ️ Command-line programs to run using the OS shell.
59
+ # 📚 https://git.io/JvXDl
60
+
61
+ # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
62
+ # and modify them (or add more) to build your code if your project
63
+ # uses a compiled language
64
+
65
+ #- run: |
66
+ # make bootstrap
67
+ # make release
68
+
69
+ - name: Perform CodeQL Analysis
70
+ uses: github/codeql-action/analyze@v1
@@ -0,0 +1,31 @@
1
+ # This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
+ # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
+
4
+ name: Node.js CI
5
+
6
+ on:
7
+ push:
8
+ branches: [ master ]
9
+ pull_request:
10
+ branches: [ master ]
11
+
12
+ jobs:
13
+ build:
14
+
15
+ runs-on: ubuntu-latest
16
+
17
+ strategy:
18
+ matrix:
19
+ node-version: [12.x, 14.x, 16.x]
20
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21
+
22
+ steps:
23
+ - uses: actions/checkout@v2
24
+ - name: Use Node.js ${{ matrix.node-version }}
25
+ uses: actions/setup-node@v2
26
+ with:
27
+ node-version: ${{ matrix.node-version }}
28
+ cache: 'npm'
29
+ - run: npm ci
30
+ - run: npm run build --if-present
31
+ - run: npm test
@@ -0,0 +1,38 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a milestone is closed
2
+ # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3
+
4
+ name: Node.js Package
5
+
6
+ on:
7
+ milestone:
8
+ types: [closed]
9
+
10
+ jobs:
11
+ build:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - uses: actions/setup-node@v2
16
+ with:
17
+ node-version: 16
18
+ - run: npm ci
19
+ - run: npm test
20
+
21
+ publish-npm:
22
+ needs: build
23
+ runs-on: ubuntu-latest
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - uses: actions/setup-node@v2
27
+ with:
28
+ node-version: 16
29
+ registry-url: https://registry.npmjs.org/
30
+ - run: npm ci
31
+ - run: |
32
+ git config user.name github-actions
33
+ git config user.email github-actions@github.com
34
+ - run: npm version ${{ github.event.milestone.title }}
35
+ - run: git push && git push --tags
36
+ - run: npm publish
37
+ env:
38
+ NODE_AUTH_TOKEN: ${{secrets.npm_token}}
package/.travis.yml CHANGED
@@ -1,7 +1,6 @@
1
1
  language: node_js
2
2
  sudo: false
3
3
  node_js:
4
- - "7"
4
+ - "10"
5
+ - "8"
5
6
  - "6"
6
- - "5"
7
- - "4"
package/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # log4js-node changelog
2
+
3
+ ## 4.0.0
4
+
5
+ - [Fix timezone format to include colon separator](https://github.com/nomiddlename/date-format/pull/27) - thanks [@peteriman](https://github.com/peteriman)
6
+ - [test: have a test case for timezone with colon](https://github.com/nomiddlename/date-format/pull/32) - thanks [@peteriman](https://github.com/peteriman)
7
+ - [Docs: Updated README.md with more examples and expected output](https://github.com/nomiddlename/date-format/pull/33) - thanks [@peteriman](https://github.com/peteriman)
8
+ - Updated dependencies
9
+ - [should-util from 1.0.0 to 1.0.1](https://github.com/nomiddlename/date-format/pull/31)
10
+ - [eslint from 5.16.0 to 8.6.0 and mocha from 5.2.0 to 9.1.3](https://github.com/nomiddlename/date-format/pull/30)
11
+ - [acorn from 6.2.0 to 6.4.2](https://github.com/nomiddlename/date-format/pull/29)
12
+ - [lodash from 4.17.14 to 4.17.21](https://github.com/nomiddlename/date-format/pull/26)
13
+
14
+ ## Previous versions
15
+
16
+ Change information for older versions can be found by looking at the milestones in github.
package/README.md CHANGED
@@ -10,20 +10,33 @@ npm install date-format
10
10
  usage
11
11
  =====
12
12
 
13
- ```js
13
+ Formatting dates as strings
14
+ ----
15
+
16
+ ```javascript
14
17
  var format = require('date-format');
15
- format.asString(); //defaults to ISO8601 format and current date.
16
- format.asString(new Date()); //defaults to ISO8601 format
17
- format.asString('hh:mm:ss.SSS', new Date()); //just the time
18
+ format.asString(); // defaults to ISO8601 format and current date
19
+ format.asString(new Date()); // defaults to ISO8601 format
20
+ format.asString('hh:mm:ss.SSS', new Date()); // just the time
21
+ format.asString(format.ISO8601_WITH_TZ_OFFSET_FORMAT, new Date()); // in ISO8601 with timezone
18
22
  ```
19
23
 
20
24
  or
21
25
 
22
- ```js
26
+ ```javascript
23
27
  var format = require('date-format');
24
- format(); //defaults to ISO8601 format and current date.
25
- format(new Date());
26
- format('hh:mm:ss.SSS', new Date());
28
+ format(); // defaults to ISO8601 format and current date
29
+ format(new Date()); // defaults to ISO8601 format
30
+ format('hh:mm:ss.SSS', new Date()); // just the time
31
+ format(format.ISO8601_WITH_TZ_OFFSET_FORMAT, new Date()); // in ISO8601 with timezone
32
+ ```
33
+
34
+ **output:**
35
+ ```javascript
36
+ 2017-03-14T14:10:20.391
37
+ 2017-03-14T14:10:20.391
38
+ 14:10:20.391
39
+ 2017-03-14T14:10:20.391+11:00
27
40
  ```
28
41
 
29
42
  Format string can be anything, but the following letters will be replaced (and leading zeroes added if necessary):
@@ -35,6 +48,24 @@ Format string can be anything, but the following letters will be replaced (and l
35
48
  * mm - `date.getMinutes()`
36
49
  * ss - `date.getSeconds()`
37
50
  * SSS - `date.getMilliseconds()`
38
- * O - timezone offset in +hm format
51
+ * O - timezone offset in ±hh:mm format (note that time will still be local if displaying offset)
52
+
53
+ Built-in formats:
54
+ * `format.ISO8601_FORMAT` - `2017-03-14T14:10:20.391` (local time used)
55
+ * `format.ISO8601_WITH_TZ_OFFSET_FORMAT` - `2017-03-14T14:10:20.391+11:00` (local + TZ used)
56
+ * `format.DATETIME_FORMAT` - `14 03 2017 14:10:20.391` (local time used)
57
+ * `format.ABSOLUTETIME_FORMAT` - `14:10:20.391` (local time used)
39
58
 
40
- That's it.
59
+ Parsing strings as dates
60
+ ----
61
+ The date format library has limited ability to parse strings into dates. It can convert strings created using date format patterns (as above), but if you're looking for anything more sophisticated than that you should probably look for a better library ([momentjs](https://momentjs.com) does pretty much everything).
62
+
63
+ ```javascript
64
+ var format = require('date-format');
65
+ // pass in the format of the string as first argument
66
+ format.parse(format.ISO8601_FORMAT, '2017-03-14T14:10:20.391');
67
+ format.parse(format.ISO8601_WITH_TZ_OFFSET_FORMAT, '2017-03-14T14:10:20.391+1100');
68
+ format.parse(format.ISO8601_WITH_TZ_OFFSET_FORMAT, '2017-03-14T14:10:20.391+11:00');
69
+ format.parse(format.ISO8601_WITH_TZ_OFFSET_FORMAT, '2017-03-14T03:10:20.391Z');
70
+ // returns Date
71
+ ```
package/lib/index.js CHANGED
@@ -1,15 +1,15 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  function padWithZeros(vNumber, width) {
4
- var numAsString = vNumber.toString();
5
- while (numAsString.length < width) {
6
- numAsString = '0' + numAsString;
7
- }
8
- return numAsString;
4
+ var numAsString = vNumber.toString();
5
+ while (numAsString.length < width) {
6
+ numAsString = "0" + numAsString;
7
+ }
8
+ return numAsString;
9
9
  }
10
10
 
11
11
  function addZero(vNumber) {
12
- return padWithZeros(vNumber, 2);
12
+ return padWithZeros(vNumber, 2);
13
13
  }
14
14
 
15
15
  /**
@@ -18,46 +18,42 @@ function addZero(vNumber) {
18
18
  * @private
19
19
  */
20
20
  function offset(timezoneOffset) {
21
- // Difference to Greenwich time (GMT) in hours
22
- var os = Math.abs(timezoneOffset);
23
- var h = String(Math.floor(os / 60));
24
- var m = String(os % 60);
25
- if (h.length === 1) {
26
- h = '0' + h;
27
- }
28
- if (m.length === 1) {
29
- m = '0' + m;
30
- }
31
- return timezoneOffset < 0 ? '+' + h + m : '-' + h + m;
21
+ var os = Math.abs(timezoneOffset);
22
+ var h = String(Math.floor(os / 60));
23
+ var m = String(os % 60);
24
+ if (h.length === 1) {
25
+ h = "0" + h;
26
+ }
27
+ if (m.length === 1) {
28
+ m = "0" + m;
29
+ }
30
+ return timezoneOffset === 0 ? "Z" : (timezoneOffset < 0 ? "+" : "-") + h + ":" + m;
32
31
  }
33
32
 
34
- function asString(format, date, timezoneOffset) {
35
- if (typeof format !== 'string') {
36
- timezoneOffset = date;
37
- date = format;
38
- format = module.exports.ISO8601_FORMAT;
39
- }
40
- if (!date) {
41
- date = new Date();
42
- }
43
- // make the date independent of the system timezone by working with UTC
44
- if (timezoneOffset === undefined) {
45
- timezoneOffset = date.getTimezoneOffset();
46
- }
33
+ function asString(format, date) {
34
+ if (typeof format !== "string") {
35
+ date = format;
36
+ format = module.exports.ISO8601_FORMAT;
37
+ }
38
+ if (!date) {
39
+ date = module.exports.now();
40
+ }
47
41
 
48
- date.setUTCMinutes(date.getUTCMinutes() - timezoneOffset);
49
- var vDay = addZero(date.getUTCDate());
50
- var vMonth = addZero(date.getUTCMonth() + 1);
51
- var vYearLong = addZero(date.getUTCFullYear());
52
- var vYearShort = addZero(date.getUTCFullYear().toString().substring(2, 4));
53
- var vYear = (format.indexOf('yyyy') > -1 ? vYearLong : vYearShort);
54
- var vHour = addZero(date.getUTCHours());
55
- var vMinute = addZero(date.getUTCMinutes());
56
- var vSecond = addZero(date.getUTCSeconds());
57
- var vMillisecond = padWithZeros(date.getUTCMilliseconds(), 3);
58
- var vTimeZone = offset(timezoneOffset);
59
- date.setUTCMinutes(date.getUTCMinutes() + timezoneOffset);
60
- var formatted = format
42
+ // Issue # 14 - Per ISO8601 standard, the time string should be local time
43
+ // with timezone info.
44
+ // See https://en.wikipedia.org/wiki/ISO_8601 section "Time offsets from UTC"
45
+
46
+ var vDay = addZero(date.getDate());
47
+ var vMonth = addZero(date.getMonth() + 1);
48
+ var vYearLong = addZero(date.getFullYear());
49
+ var vYearShort = addZero(vYearLong.substring(2, 4));
50
+ var vYear = format.indexOf("yyyy") > -1 ? vYearLong : vYearShort;
51
+ var vHour = addZero(date.getHours());
52
+ var vMinute = addZero(date.getMinutes());
53
+ var vSecond = addZero(date.getSeconds());
54
+ var vMillisecond = padWithZeros(date.getMilliseconds(), 3);
55
+ var vTimeZone = offset(date.getTimezoneOffset());
56
+ var formatted = format
61
57
  .replace(/dd/g, vDay)
62
58
  .replace(/MM/g, vMonth)
63
59
  .replace(/y{1,4}/g, vYear)
@@ -66,12 +62,161 @@ function asString(format, date, timezoneOffset) {
66
62
  .replace(/ss/g, vSecond)
67
63
  .replace(/SSS/g, vMillisecond)
68
64
  .replace(/O/g, vTimeZone);
69
- return formatted;
65
+ return formatted;
66
+ }
67
+
68
+ function setDatePart(date, part, value, local) {
69
+ date['set' + (local ? '' : 'UTC') + part](value);
70
+ }
71
+
72
+ function extractDateParts(pattern, str, missingValuesDate) {
73
+ // Javascript Date object doesn't support custom timezone. Sets all felds as
74
+ // GMT based to begin with. If the timezone offset is provided, then adjust
75
+ // it using provided timezone, otherwise, adjust it with the system timezone.
76
+ var local = pattern.indexOf('O') < 0;
77
+ var matchers = [
78
+ {
79
+ pattern: /y{1,4}/,
80
+ regexp: "\\d{1,4}",
81
+ fn: function(date, value) {
82
+ setDatePart(date, 'FullYear', value, local);
83
+ }
84
+ },
85
+ {
86
+ pattern: /MM/,
87
+ regexp: "\\d{1,2}",
88
+ fn: function(date, value) {
89
+ setDatePart(date, 'Month', (value - 1), local);
90
+ }
91
+ },
92
+ {
93
+ pattern: /dd/,
94
+ regexp: "\\d{1,2}",
95
+ fn: function(date, value) {
96
+ setDatePart(date, 'Date', value, local);
97
+ }
98
+ },
99
+ {
100
+ pattern: /hh/,
101
+ regexp: "\\d{1,2}",
102
+ fn: function(date, value) {
103
+ setDatePart(date, 'Hours', value, local);
104
+ }
105
+ },
106
+ {
107
+ pattern: /mm/,
108
+ regexp: "\\d\\d",
109
+ fn: function(date, value) {
110
+ setDatePart(date, 'Minutes', value, local);
111
+ }
112
+ },
113
+ {
114
+ pattern: /ss/,
115
+ regexp: "\\d\\d",
116
+ fn: function(date, value) {
117
+ setDatePart(date, 'Seconds', value, local);
118
+ }
119
+ },
120
+ {
121
+ pattern: /SSS/,
122
+ regexp: "\\d\\d\\d",
123
+ fn: function(date, value) {
124
+ setDatePart(date, 'Milliseconds', value, local);
125
+ }
126
+ },
127
+ {
128
+ pattern: /O/,
129
+ regexp: "[+-]\\d{1,2}:?\\d{2}?|Z",
130
+ fn: function(date, value) {
131
+ if (value === "Z") {
132
+ value = 0;
133
+ }
134
+ else {
135
+ value = value.replace(":", "");
136
+ }
137
+ var offset = Math.abs(value);
138
+ var timezoneOffset = (value > 0 ? -1 : 1 ) * ((offset % 100) + Math.floor(offset / 100) * 60);
139
+ // Per ISO8601 standard: UTC = local time - offset
140
+ //
141
+ // For example, 2000-01-01T01:00:00-0700
142
+ // local time: 2000-01-01T01:00:00
143
+ // ==> UTC : 2000-01-01T08:00:00 ( 01 - (-7) = 8 )
144
+ //
145
+ // To make it even more confusing, the date.getTimezoneOffset() is
146
+ // opposite sign of offset string in the ISO8601 standard. So if offset
147
+ // is '-0700' the getTimezoneOffset() would be (+)420. The line above
148
+ // calculates timezoneOffset to matche Javascript's behavior.
149
+ //
150
+ // The date/time of the input is actually the local time, so the date
151
+ // object that was constructed is actually local time even thought the
152
+ // UTC setters are used. This means the date object's internal UTC
153
+ // representation was wrong. It needs to be fixed by substracting the
154
+ // offset (or adding the offset minutes as they are opposite sign).
155
+ //
156
+ // Note: the time zone has to be processed after all other fileds are
157
+ // set. The result would be incorrect if the offset was calculated
158
+ // first then overriden by the other filed setters.
159
+ date.setUTCMinutes(date.getUTCMinutes() + timezoneOffset);
160
+ }
161
+ }
162
+ ];
163
+
164
+ var parsedPattern = matchers.reduce(
165
+ function(p, m) {
166
+ if (m.pattern.test(p.regexp)) {
167
+ m.index = p.regexp.match(m.pattern).index;
168
+ p.regexp = p.regexp.replace(m.pattern, "(" + m.regexp + ")");
169
+ } else {
170
+ m.index = -1;
171
+ }
172
+ return p;
173
+ },
174
+ { regexp: pattern, index: [] }
175
+ );
176
+
177
+ var dateFns = matchers.filter(function(m) {
178
+ return m.index > -1;
179
+ });
180
+ dateFns.sort(function(a, b) {
181
+ return a.index - b.index;
182
+ });
183
+
184
+ var matcher = new RegExp(parsedPattern.regexp);
185
+ var matches = matcher.exec(str);
186
+ if (matches) {
187
+ var date = missingValuesDate || module.exports.now();
188
+ dateFns.forEach(function(f, i) {
189
+ f.fn(date, matches[i + 1]);
190
+ });
191
+
192
+ return date;
193
+ }
194
+
195
+ throw new Error(
196
+ "String '" + str + "' could not be parsed as '" + pattern + "'"
197
+ );
198
+ }
199
+
200
+ function parse(pattern, str, missingValuesDate) {
201
+ if (!pattern) {
202
+ throw new Error("pattern must be supplied");
203
+ }
204
+
205
+ return extractDateParts(pattern, str, missingValuesDate);
206
+ }
207
+
208
+ /**
209
+ * Used for testing - replace this function with a fixed date.
210
+ */
211
+ function now() {
212
+ return new Date();
70
213
  }
71
214
 
72
215
  module.exports = asString;
73
216
  module.exports.asString = asString;
74
- module.exports.ISO8601_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSS';
75
- module.exports.ISO8601_WITH_TZ_OFFSET_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSSO';
76
- module.exports.DATETIME_FORMAT = 'dd MM yyyy hh:mm:ss.SSS';
77
- module.exports.ABSOLUTETIME_FORMAT = 'hh:mm:ss.SSS';
217
+ module.exports.parse = parse;
218
+ module.exports.now = now;
219
+ module.exports.ISO8601_FORMAT = "yyyy-MM-ddThh:mm:ss.SSS";
220
+ module.exports.ISO8601_WITH_TZ_OFFSET_FORMAT = "yyyy-MM-ddThh:mm:ss.SSSO";
221
+ module.exports.DATETIME_FORMAT = "dd MM yyyy hh:mm:ss.SSS";
222
+ module.exports.ABSOLUTETIME_FORMAT = "hh:mm:ss.SSS";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "date-format",
3
- "version": "1.2.0",
3
+ "version": "4.0.1",
4
4
  "description": "Formatting Date objects as strings since 2013",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
@@ -25,10 +25,9 @@
25
25
  "readmeFilename": "README.md",
26
26
  "gitHead": "bf59015ab6c9e86454b179374f29debbdb403522",
27
27
  "devDependencies": {
28
- "eslint": "^3.12.0",
29
- "eslint-plugin-import": "^2.2.0",
30
- "eslint-plugin-mocha": "^4.8.0",
31
- "mocha": "^3.2.0",
32
- "should": "^11.1.2"
28
+ "eslint": "^8.6.0",
29
+ "eslint-plugin-mocha": "^5.3.0",
30
+ "mocha": "^9.1.3",
31
+ "should": "^13.2.3"
33
32
  }
34
33
  }
@@ -29,22 +29,21 @@ describe('date_format', function() {
29
29
 
30
30
  it('should provide a ISO8601 with timezone offset format', function() {
31
31
  var tzDate = createFixedDate();
32
- tzDate.setMinutes(tzDate.getMinutes() - tzDate.getTimezoneOffset() - 660);
33
32
  tzDate.getTimezoneOffset = function () {
34
33
  return -660;
35
34
  };
36
35
 
36
+ // when tz offset is in the pattern, the date should be in local time
37
37
  dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
38
- .should.eql('2010-01-11T14:31:30.005+1100');
38
+ .should.eql('2010-01-11T14:31:30.005+11:00');
39
39
 
40
40
  tzDate = createFixedDate();
41
- tzDate.setMinutes((tzDate.getMinutes() - tzDate.getTimezoneOffset()) + 120);
42
41
  tzDate.getTimezoneOffset = function () {
43
42
  return 120;
44
43
  };
45
44
 
46
45
  dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
47
- .should.eql('2010-01-11T14:31:30.005-0200');
46
+ .should.eql('2010-01-11T14:31:30.005-02:00');
48
47
  });
49
48
 
50
49
  it('should provide a just-the-time format', function() {
@@ -53,11 +52,10 @@ describe('date_format', function() {
53
52
 
54
53
  it('should provide a custom format', function() {
55
54
  var customDate = createFixedDate();
56
- customDate.setMinutes((customDate.getMinutes() - customDate.getTimezoneOffset()) + 120);
57
55
  customDate.getTimezoneOffset = function () {
58
56
  return 120;
59
57
  };
60
58
 
61
- dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.14.11.01.10');
59
+ dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-02:00.005.30.31.14.11.01.10');
62
60
  });
63
61
  });
@@ -0,0 +1,220 @@
1
+ "use strict";
2
+
3
+ require("should");
4
+ var dateFormat = require("../lib");
5
+
6
+ describe("dateFormat.parse", function() {
7
+ it("should require a pattern", function() {
8
+ (function() {
9
+ dateFormat.parse();
10
+ }.should.throw(/pattern must be supplied/));
11
+ (function() {
12
+ dateFormat.parse(null);
13
+ }.should.throw(/pattern must be supplied/));
14
+ (function() {
15
+ dateFormat.parse("");
16
+ }.should.throw(/pattern must be supplied/));
17
+ });
18
+
19
+ describe("with a pattern that has no replacements", function() {
20
+ it("should return a new date when the string matches", function() {
21
+ dateFormat.parse("cheese", "cheese").should.be.a.Date();
22
+ });
23
+
24
+ it("should throw if the string does not match", function() {
25
+ (function() {
26
+ dateFormat.parse("cheese", "biscuits");
27
+ }.should.throw(/String 'biscuits' could not be parsed as 'cheese'/));
28
+ });
29
+ });
30
+
31
+ describe("with a full pattern", function() {
32
+ var pattern = "yyyy-MM-dd hh:mm:ss.SSSO";
33
+
34
+ it("should return the correct date if the string matches", function() {
35
+ var testDate = new Date();
36
+ testDate.setUTCFullYear(2018);
37
+ testDate.setUTCMonth(8);
38
+ testDate.setUTCDate(13);
39
+ testDate.setUTCHours(18);
40
+ testDate.setUTCMinutes(10);
41
+ testDate.setUTCSeconds(12);
42
+ testDate.setUTCMilliseconds(392);
43
+
44
+ dateFormat
45
+ .parse(pattern, "2018-09-14 04:10:12.392+1000")
46
+ .getTime()
47
+ .should.eql(testDate.getTime())
48
+ ;
49
+ });
50
+
51
+ it("should throw if the string does not match", function() {
52
+ (function() {
53
+ dateFormat.parse(pattern, "biscuits");
54
+ }.should.throw(
55
+ /String 'biscuits' could not be parsed as 'yyyy-MM-dd hh:mm:ss.SSSO'/
56
+ ));
57
+ });
58
+ });
59
+
60
+ describe("with a partial pattern", function() {
61
+ var testDate = new Date();
62
+ dateFormat.now = function() {
63
+ return testDate;
64
+ };
65
+
66
+ /**
67
+ * If there's no timezone in the format, then we verify against the local date
68
+ */
69
+ function verifyLocalDate(actual, expected) {
70
+ actual.getFullYear().should.eql(expected.year || testDate.getFullYear());
71
+ actual.getMonth().should.eql(expected.month || testDate.getMonth());
72
+ actual.getDate().should.eql(expected.day || testDate.getDate());
73
+ actual.getHours().should.eql(expected.hours || testDate.getHours());
74
+ actual.getMinutes().should.eql(expected.minutes || testDate.getMinutes());
75
+ actual.getSeconds().should.eql(expected.seconds || testDate.getSeconds());
76
+ actual
77
+ .getMilliseconds()
78
+ .should.eql(expected.milliseconds || testDate.getMilliseconds());
79
+ }
80
+
81
+ /**
82
+ * If a timezone is specified, let's verify against the UTC time it is supposed to be
83
+ */
84
+ function verifyDate(actual, expected) {
85
+ actual.getUTCFullYear().should.eql(expected.year || testDate.getUTCFullYear());
86
+ actual.getUTCMonth().should.eql(expected.month || testDate.getUTCMonth());
87
+ actual.getUTCDate().should.eql(expected.day || testDate.getUTCDate());
88
+ actual.getUTCHours().should.eql(expected.hours || testDate.getUTCHours());
89
+ actual.getUTCMinutes().should.eql(expected.minutes || testDate.getUTCMinutes());
90
+ actual.getUTCSeconds().should.eql(expected.seconds || testDate.getUTCSeconds());
91
+ actual
92
+ .getMilliseconds()
93
+ .should.eql(expected.milliseconds || testDate.getMilliseconds());
94
+ }
95
+
96
+ it("should return a date with missing values defaulting to current time", function() {
97
+ var date = dateFormat.parse("yyyy-MM", "2015-09");
98
+ verifyLocalDate(date, { year: 2015, month: 8 });
99
+ });
100
+
101
+ it("should use a passed in date for missing values", function() {
102
+ var missingValueDate = new Date(2010, 1, 8, 22, 30, 12, 100);
103
+ var date = dateFormat.parse("yyyy-MM", "2015-09", missingValueDate);
104
+ verifyLocalDate(date, {
105
+ year: 2015,
106
+ month: 8,
107
+ day: 8,
108
+ hours: 22,
109
+ minutes: 30,
110
+ seconds: 12,
111
+ milliseconds: 100
112
+ });
113
+ });
114
+
115
+ it("should handle variations on the same pattern", function() {
116
+ var date = dateFormat.parse("MM-yyyy", "09-2015");
117
+ verifyLocalDate(date, { year: 2015, month: 8 });
118
+
119
+ date = dateFormat.parse("yyyy MM", "2015 09");
120
+ verifyLocalDate(date, { year: 2015, month: 8 });
121
+
122
+ date = dateFormat.parse("MM, yyyy.", "09, 2015.");
123
+ verifyLocalDate(date, { year: 2015, month: 8 });
124
+ });
125
+
126
+ describe("should match all the date parts", function() {
127
+ it("works with dd", function() {
128
+ var date = dateFormat.parse("dd", "21");
129
+ verifyLocalDate(date, { day: 21 });
130
+ });
131
+
132
+ it("works with hh", function() {
133
+ var date = dateFormat.parse("hh", "12");
134
+ verifyLocalDate(date, { hours: 12 });
135
+ });
136
+
137
+ it("works with mm", function() {
138
+ var date = dateFormat.parse("mm", "34");
139
+ verifyLocalDate(date, { minutes: 34 });
140
+ });
141
+
142
+ it("works with ss", function() {
143
+ var date = dateFormat.parse("ss", "59");
144
+ verifyLocalDate(date, { seconds: 59 });
145
+ });
146
+
147
+ it("works with ss.SSS", function() {
148
+ var date = dateFormat.parse("ss.SSS", "23.452");
149
+ verifyLocalDate(date, { seconds: 23, milliseconds: 452 });
150
+ });
151
+
152
+ it("works with hh:mm O (+1000)", function() {
153
+ var date = dateFormat.parse("hh:mm O", "05:23 +1000");
154
+ verifyDate(date, { hours: 19, minutes: 23 });
155
+ });
156
+
157
+ it("works with hh:mm O (-200)", function() {
158
+ var date = dateFormat.parse("hh:mm O", "05:23 -200");
159
+ verifyDate(date, { hours: 7, minutes: 23 });
160
+ });
161
+
162
+ it("works with hh:mm O (+09:30)", function() {
163
+ var date = dateFormat.parse("hh:mm O", "05:23 +09:30");
164
+ verifyDate(date, { hours: 19, minutes: 53 });
165
+ });
166
+ });
167
+ });
168
+
169
+ describe("with a date formatted by this library", function() {
170
+ describe("should format and then parse back to the same date", function() {
171
+ function testDateInitWithUTC() {
172
+ var td = new Date();
173
+ td.setUTCFullYear(2018);
174
+ td.setUTCMonth(8);
175
+ td.setUTCDate(13);
176
+ td.setUTCHours(18);
177
+ td.setUTCMinutes(10);
178
+ td.setUTCSeconds(12);
179
+ td.setUTCMilliseconds(392);
180
+ return td;
181
+ }
182
+
183
+ it("works with ISO8601_WITH_TZ_OFFSET_FORMAT", function() {
184
+ // For this test case to work, the date object must be initialized with
185
+ // UTC timezone
186
+ var td = testDateInitWithUTC();
187
+ var d = dateFormat(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, td);
188
+ dateFormat.parse(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, d)
189
+ .should.eql(td);
190
+ });
191
+
192
+ it("works with ISO8601_FORMAT", function() {
193
+ var td = new Date();
194
+ var d = dateFormat(dateFormat.ISO8601_FORMAT, td);
195
+ var actual = dateFormat.parse(dateFormat.ISO8601_FORMAT, d);
196
+ actual.should.eql(td);
197
+ });
198
+
199
+ it("works with DATETIME_FORMAT", function() {
200
+ var testDate = new Date();
201
+ dateFormat
202
+ .parse(
203
+ dateFormat.DATETIME_FORMAT,
204
+ dateFormat(dateFormat.DATETIME_FORMAT, testDate)
205
+ )
206
+ .should.eql(testDate);
207
+ });
208
+
209
+ it("works with ABSOLUTETIME_FORMAT", function() {
210
+ var testDate = new Date();
211
+ dateFormat
212
+ .parse(
213
+ dateFormat.ABSOLUTETIME_FORMAT,
214
+ dateFormat(dateFormat.ABSOLUTETIME_FORMAT, testDate)
215
+ )
216
+ .should.eql(testDate);
217
+ });
218
+ });
219
+ });
220
+ });
package/.npmignore DELETED
@@ -1,15 +0,0 @@
1
- lib-cov
2
- *.seed
3
- *.log
4
- *.csv
5
- *.dat
6
- *.out
7
- *.pid
8
- *.gz
9
-
10
- pids
11
- logs
12
- results
13
-
14
- npm-debug.log
15
- node_modules