|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -var assert = require('assert'); |
4 | | -var strptime = require('../lib/micro-strptime.js').strptime; |
| 3 | +import test from 'node:test'; |
| 4 | +import assert from 'node:assert'; |
| 5 | +import { strptime } from '../lib/micro-strptime.js'; |
5 | 6 |
|
6 | | -var anyErrors = false; |
| 7 | +test('strptime returns Date', () => { |
| 8 | + assert.ok(strptime('2012', '%Y') instanceof Date); |
| 9 | +}); |
7 | 10 |
|
8 | | -assert.ok(strptime('2012', '%Y') instanceof Date); |
| 11 | +test('strptime throws on parse error', () => { |
| 12 | + assert.throws(() => strptime('xxxx', '%Y-%m-%d'), /Failed to parse/); |
| 13 | + assert.throws(() => strptime('xxxx'), /Missing format/); |
| 14 | + assert.throws(() => strptime('2012', '%"unknown"'), /Unknown format descriptor: "unknown"/); |
| 15 | +}); |
9 | 16 |
|
10 | | -function assertThrows(callback, message){ |
11 | | - try{ |
12 | | - callback(); |
13 | | - }catch(error){ |
14 | | - assert.ok(error); |
15 | | - assert.equal(error.message, message); |
16 | | - return; |
17 | | - } |
18 | | - throw Error("Callback didn't throw any error."); |
| 17 | +function testDate(strings, date) { |
| 18 | + strings.forEach(i => { |
| 19 | + const expectedDate = new Date(date); |
| 20 | + let actualDate; |
| 21 | + try { |
| 22 | + actualDate = strptime(i.string, i.format); |
| 23 | + assert.equal(String(actualDate), String(expectedDate)); |
| 24 | + } catch (e) { |
| 25 | + console.log(`FAIL: ${i.format}: ${i.string}`); |
| 26 | + console.log(`ACTUAL: ${String(actualDate)}`); |
| 27 | + console.log(`EXPECTED: ${String(expectedDate)}`); |
| 28 | + console.log(e); |
| 29 | + } |
| 30 | + }); |
19 | 31 | } |
20 | 32 |
|
21 | | -assertThrows(function () { |
22 | | - strptime('xxxx', '%Y-%m-%d'); |
23 | | -}, 'Failed to parse'); |
| 33 | +test('strptime date patterns', () => { |
| 34 | + testDate([ |
| 35 | + { string: '2012-05-05T09:00:00.00+09:00', format : '%Y-%m-%dT%H:%M:%S.%s%Z' }, |
| 36 | + { string: '20120505000000', format : '%Y%m%d%H%M%S' }, |
| 37 | + { string: '2012-05-05T09:00:00+09:00', format : '%Y-%m-%dT%H:%M:%S%Z' }, |
| 38 | + { string: '2012-05-05 09:00:00+09:00', format : '%Y-%m-%d %H:%M:%S%Z' }, |
| 39 | + { string: '2012-05-05 00:00:00+00:00', format : '%Y-%m-%d %H:%M:%S%Z' }, |
| 40 | + { string: '2012-05-05 00:00:00Z', format : '%Y-%m-%d %H:%M:%S%Z' }, |
| 41 | + { string: '05/May/2012:09:00:00 +0900', format : '%d/%B/%Y:%H:%M:%S %Z' }, |
| 42 | + { string: '05/5/2012:09:00:00 +0900', format : '%d/%m/%Y:%H:%M:%S %Z' }, |
| 43 | + { string: 'Sat, 05 May 2012 09:00:00 +0900', format : '%A, %d %B %Y %H:%M:%S %Z' }, |
| 44 | + { string: 'Sat, 05 May 2012 09:00:00 +0900', format : '%a, %d %b %Y %H:%M:%S %z' }, |
| 45 | + { string: 'Sat May 05 2012 09:00:00 GMT+0900 (JST)', format : '%A %B %d %Y %H:%M:%S GMT%Z' }, |
| 46 | + { string: 'Saturday May 05 2012 09:00:00 GMT+0900 (JST)', format : '%A %B %d %Y %H:%M:%S GMT%Z' } |
| 47 | + ], Date.UTC(2012, 4, 5, 0, 0, 0)); |
| 48 | +}); |
24 | 49 |
|
25 | | -assertThrows(function () { |
26 | | - strptime('xxxx'); |
27 | | -}, 'Missing format'); |
| 50 | +test('strptime 12-hour digital clocks', () => { |
| 51 | + testDate([{ string: '2012-05-05 12:00:00 AM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 0, 0, 0)); |
| 52 | + testDate([{ string: '2012-05-05 12:01:00 AM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 0, 1, 0)); |
| 53 | + testDate([{ string: '2012-05-05 01:00:00 AM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 1, 0, 0)); |
| 54 | + testDate([{ string: '2012-05-05 12:00:00 PM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 12, 0, 0)); |
| 55 | + testDate([{ string: '2012-05-05 12:01:00 PM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 12, 1, 0)); |
| 56 | + testDate([{ string: '2012-05-05 01:00:00 PM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 13, 0, 0)); |
| 57 | +}); |
28 | 58 |
|
29 | | -assertThrows(function () { |
30 | | - strptime('2012', '%"unknown"'); |
31 | | -}, 'Unknown format descripter: "unknown"'); |
| 59 | +test('strptime 12-hour digital clocks (AM/PM前置)', () => { |
| 60 | + testDate([{ string: '2012-05-05 AM 12:00:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 0, 0, 0)); |
| 61 | + testDate([{ string: '2012-05-05 AM 12:01:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 0, 1, 0)); |
| 62 | + testDate([{ string: '2012-05-05 AM 01:00:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 1, 0, 0)); |
| 63 | + testDate([{ string: '2012-05-05 PM 12:00:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 12, 0, 0)); |
| 64 | + testDate([{ string: '2012-05-05 PM 12:01:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 12, 1, 0)); |
| 65 | + testDate([{ string: '2012-05-05 PM 01:00:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 13, 0, 0)); |
| 66 | +}); |
32 | 67 |
|
33 | | -function test (strings, date) { |
34 | | - strings.forEach(function (i) { |
35 | | - var actualDate; |
36 | | - var expectedDate = new Date(date); |
37 | | - try { |
38 | | - actualDate = strptime(i.string, i.format); |
39 | | - assert.equal(String(actualDate), String(expectedDate)); |
40 | | - } catch (e) { |
41 | | - console.log("FAIL: %s: %s", i.format, i.string); |
42 | | - console.log("ACTUAL: " + String(actualDate)); |
43 | | - console.log("EXPECTED: " + String(expectedDate)); |
44 | | - console.log(e); |
45 | | - anyErrors = true; |
46 | | - } |
47 | | - }); |
48 | | -} |
49 | | - |
50 | | -test([ |
51 | | - { string: '2012-05-05T09:00:00.00+09:00', format : '%Y-%m-%dT%H:%M:%S.%s%Z' }, |
52 | | - { string: '20120505000000', format : '%Y%m%d%H%M%S' }, |
53 | | - { string: '2012-05-05T09:00:00+09:00', format : '%Y-%m-%dT%H:%M:%S%Z' }, |
54 | | - { string: '2012-05-05 09:00:00+09:00', format : '%Y-%m-%d %H:%M:%S%Z' }, |
55 | | - { string: '2012-05-05 00:00:00+00:00', format : '%Y-%m-%d %H:%M:%S%Z' }, |
56 | | - { string: '2012-05-05 00:00:00Z', format : '%Y-%m-%d %H:%M:%S%Z' }, |
57 | | - { string: '05/May/2012:09:00:00 +0900', format : '%d/%B/%Y:%H:%M:%S %Z' }, |
58 | | - { string: '05/5/2012:09:00:00 +0900', format : '%d/%m/%Y:%H:%M:%S %Z' }, |
59 | | - { string: 'Sat, 05 May 2012 09:00:00 +0900', format : '%A, %d %B %Y %H:%M:%S %Z' }, |
60 | | - { string: 'Sat, 05 May 2012 09:00:00 +0900', format : '%a, %d %b %Y %H:%M:%S %z' }, |
61 | | - { string: 'Sat May 05 2012 09:00:00 GMT+0900 (JST)', format : '%A %B %d %Y %H:%M:%S GMT%Z' }, |
62 | | - { string: 'Saturday May 05 2012 09:00:00 GMT+0900 (JST)', format : '%A %B %d %Y %H:%M:%S GMT%Z' } |
63 | | -], Date.UTC(2012, 4, 5, 0, 0, 0)); |
64 | | - |
65 | | -// 12-hour digital clocks format |
66 | | -test([{ string: '2012-05-05 12:00:00 AM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 0, 0, 0) ); |
67 | | -test([{ string: '2012-05-05 12:01:00 AM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 0, 1, 0) ); |
68 | | -test([{ string: '2012-05-05 01:00:00 AM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 1, 0, 0) ); |
69 | | -test([{ string: '2012-05-05 12:00:00 PM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 12, 0, 0) ); |
70 | | -test([{ string: '2012-05-05 12:01:00 PM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 12, 1, 0) ); |
71 | | -test([{ string: '2012-05-05 01:00:00 PM', format : '%Y-%m-%d %I:%M:%S %p' } ], Date.UTC(2012, 4, 5, 13, 0, 0) ); |
72 | | - |
73 | | -test([{ string: '2012-05-05 AM 12:00:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 0, 0, 0) ); |
74 | | -test([{ string: '2012-05-05 AM 12:01:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 0, 1, 0) ); |
75 | | -test([{ string: '2012-05-05 AM 01:00:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 1, 0, 0) ); |
76 | | -test([{ string: '2012-05-05 PM 12:00:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 12, 0, 0) ); |
77 | | -test([{ string: '2012-05-05 PM 12:01:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 12, 1, 0) ); |
78 | | -test([{ string: '2012-05-05 PM 01:00:00', format : '%Y-%m-%d %p %I:%M:%S' } ], Date.UTC(2012, 4, 5, 13, 0, 0) ); |
79 | | - |
80 | | -// Leap year |
81 | | -// https://github.com/cho45/micro-strptime.js/issues/2 |
82 | | -test([ |
83 | | - { string: '29/Feb/2016:09:00:00 +0700', format : '%d/%B/%Y:%H:%M:%S %Z' } |
84 | | -], new Date("2016-02-29T02:00:00Z")); |
85 | | - |
86 | | -if(anyErrors){ |
87 | | - console.log("Tests failed."); |
88 | | - process.exit(1); |
89 | | -}else{ |
90 | | - console.log("All tests passed."); |
91 | | - process.exit(0); |
92 | | -} |
| 68 | +test('strptime leap year', () => { |
| 69 | + testDate([ |
| 70 | + { string: '29/Feb/2016:09:00:00 +0700', format : '%d/%B/%Y:%H:%M:%S %Z' } |
| 71 | + ], new Date("2016-02-29T02:00:00Z")); |
| 72 | +}); |
0 commit comments