date-format 0.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +12 -0
- package/.travis.yml +6 -0
- package/README.md +44 -19
- package/lib/index.js +124 -59
- package/package.json +15 -8
- package/test/date_format-test.js +61 -39
- package/test/parse-test.js +139 -0
- package/.npmignore +0 -15
    
        package/.eslintrc
    ADDED
    
    
    
        package/.travis.yml
    ADDED
    
    
    
        package/README.md
    CHANGED
    
    | @@ -3,31 +3,56 @@ date-format | |
| 3 3 |  | 
| 4 4 | 
             
            node.js formatting of Date objects as strings. Probably exactly the same as some other library out there.
         | 
| 5 5 |  | 
| 6 | 
            -
             | 
| 6 | 
            +
            ```sh
         | 
| 7 | 
            +
            npm install date-format
         | 
| 8 | 
            +
            ```
         | 
| 7 9 |  | 
| 8 10 | 
             
            usage
         | 
| 9 11 | 
             
            =====
         | 
| 10 12 |  | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
                    format.asString('hh:mm:ss.SSS', new Date()); //just the time
         | 
| 13 | 
            +
            Formatting dates as strings
         | 
| 14 | 
            +
            ----
         | 
| 14 15 |  | 
| 15 | 
            -
             | 
| 16 | 
            +
            ```javascript
         | 
| 17 | 
            +
            var format = require('date-format');
         | 
| 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 | 
            +
            ```
         | 
| 16 22 |  | 
| 17 | 
            -
             | 
| 18 | 
            -
                    format(new Date());
         | 
| 19 | 
            -
                    format('hh:mm:ss.SSS', new Date());
         | 
| 23 | 
            +
            or
         | 
| 20 24 |  | 
| 25 | 
            +
            ```javascript
         | 
| 26 | 
            +
            var format = require('date-format');
         | 
| 27 | 
            +
            format(); //defaults to ISO8601 format and current date.
         | 
| 28 | 
            +
            format(new Date());
         | 
| 29 | 
            +
            format('hh:mm:ss.SSS', new Date());
         | 
| 30 | 
            +
            ```
         | 
| 21 31 |  | 
| 22 32 | 
             
            Format string can be anything, but the following letters will be replaced (and leading zeroes added if necessary):
         | 
| 23 | 
            -
            * dd - date.getDate()
         | 
| 24 | 
            -
            * MM - date.getMonth() + 1
         | 
| 25 | 
            -
            * yy - date.getFullYear().toString().substring(2, 4)
         | 
| 26 | 
            -
            * yyyy - date.getFullYear()
         | 
| 27 | 
            -
            * hh - date.getHours()
         | 
| 28 | 
            -
            * mm - date.getMinutes()
         | 
| 29 | 
            -
            * ss - date.getSeconds()
         | 
| 30 | 
            -
            * SSS - date.getMilliseconds()
         | 
| 31 | 
            -
            * O - timezone offset in +hm format
         | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 33 | 
            +
            * dd - `date.getDate()`
         | 
| 34 | 
            +
            * MM - `date.getMonth() + 1`
         | 
| 35 | 
            +
            * yy - `date.getFullYear().toString().substring(2, 4)`
         | 
| 36 | 
            +
            * yyyy - `date.getFullYear()`
         | 
| 37 | 
            +
            * hh - `date.getHours()`
         | 
| 38 | 
            +
            * mm - `date.getMinutes()`
         | 
| 39 | 
            +
            * ss - `date.getSeconds()`
         | 
| 40 | 
            +
            * SSS - `date.getMilliseconds()`
         | 
| 41 | 
            +
            * O - timezone offset in +hm format (note that time will be in UTC if displaying offset)
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            Built-in formats:
         | 
| 44 | 
            +
            * `format.ISO8601_FORMAT` - `2017-03-14T14:10:20.391` (local time used)
         | 
| 45 | 
            +
            * `format.ISO8601_WITH_TZ_OFFSET_FORMAT` - `2017-03-14T03:10:20.391+1100` (UTC + TZ used)
         | 
| 46 | 
            +
            * `format.DATETIME_FORMAT` - `14 03 2017 14:10:20.391` (local time used)
         | 
| 47 | 
            +
            * `format.ABSOLUTETIME_FORMAT` - `14:10:20.391` (local time used)
         | 
| 48 | 
            +
             | 
| 49 | 
            +
            Parsing strings as dates
         | 
| 50 | 
            +
            ----
         | 
| 51 | 
            +
            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).
         | 
| 52 | 
            +
             | 
| 53 | 
            +
            ```javascript
         | 
| 54 | 
            +
            var format = require('date-format');
         | 
| 55 | 
            +
            // pass in the format of the string as first argument
         | 
| 56 | 
            +
            format.parse(format.ISO8601_FORMAT, '2017-03-14T14:10:20.391');
         | 
| 57 | 
            +
            // returns Date
         | 
| 58 | 
            +
            ```
         | 
    
        package/lib/index.js
    CHANGED
    
    | @@ -1,74 +1,139 @@ | |
| 1 | 
            -
             | 
| 2 | 
            -
             | 
| 3 | 
            -
            module.exports = asString
         | 
| 4 | 
            -
            asString.asString = asString
         | 
| 5 | 
            -
             | 
| 6 | 
            -
            asString.ISO8601_FORMAT = "yyyy-MM-dd hh:mm:ss.SSS";
         | 
| 7 | 
            -
            asString.ISO8601_WITH_TZ_OFFSET_FORMAT = "yyyy-MM-ddThh:mm:ssO";
         | 
| 8 | 
            -
            asString.DATETIME_FORMAT = "dd MM yyyy hh:mm:ss.SSS";
         | 
| 9 | 
            -
            asString.ABSOLUTETIME_FORMAT = "hh:mm:ss.SSS";
         | 
| 1 | 
            +
            'use strict';
         | 
| 10 2 |  | 
| 11 3 | 
             
            function padWithZeros(vNumber, width) {
         | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 4 | 
            +
                var numAsString = vNumber.toString();
         | 
| 5 | 
            +
                while (numAsString.length < width) {
         | 
| 6 | 
            +
                    numAsString = '0' + numAsString;
         | 
| 7 | 
            +
                }
         | 
| 8 | 
            +
                return numAsString;
         | 
| 17 9 | 
             
            }
         | 
| 18 | 
            -
             | 
| 10 | 
            +
             | 
| 19 11 | 
             
            function addZero(vNumber) {
         | 
| 20 | 
            -
             | 
| 12 | 
            +
                return padWithZeros(vNumber, 2);
         | 
| 21 13 | 
             
            }
         | 
| 22 14 |  | 
| 23 15 | 
             
            /**
         | 
| 24 | 
            -
             * Formats the  | 
| 16 | 
            +
             * Formats the TimeOffset
         | 
| 25 17 | 
             
             * Thanks to http://www.svendtofte.com/code/date_format/
         | 
| 26 18 | 
             
             * @private
         | 
| 27 19 | 
             
             */
         | 
| 28 | 
            -
            function offset( | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
                 | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
                 | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 20 | 
            +
            function offset(timezoneOffset) {
         | 
| 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 ? '+' + h + m : '-' + h + m;
         | 
| 31 | 
            +
            }
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            function datePart(date, displayUTC, part) {
         | 
| 34 | 
            +
              return displayUTC ? date['getUTC' + part]() : date['get' + part]();
         | 
| 35 | 
            +
            }
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            function asString(format, date) {
         | 
| 38 | 
            +
                if (typeof format !== 'string') {
         | 
| 39 | 
            +
                    date = format;
         | 
| 40 | 
            +
                    format = module.exports.ISO8601_FORMAT;
         | 
| 41 | 
            +
                }
         | 
| 42 | 
            +
                if (!date) {
         | 
| 43 | 
            +
                    date =  module.exports.now();
         | 
| 44 | 
            +
                }
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                var displayUTC = format.indexOf('O') > -1;
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                var vDay = addZero(datePart(date, displayUTC, 'Date'));
         | 
| 49 | 
            +
                var vMonth = addZero(datePart(date, displayUTC, 'Month') + 1);
         | 
| 50 | 
            +
                var vYearLong = addZero(datePart(date, displayUTC, 'FullYear'));
         | 
| 51 | 
            +
                var vYearShort = addZero(vYearLong.substring(2, 4));
         | 
| 52 | 
            +
                var vYear = (format.indexOf('yyyy') > -1 ? vYearLong : vYearShort);
         | 
| 53 | 
            +
                var vHour = addZero(datePart(date, displayUTC, 'Hours'));
         | 
| 54 | 
            +
                var vMinute = addZero(datePart(date, displayUTC, 'Minutes'));
         | 
| 55 | 
            +
                var vSecond = addZero(datePart(date, displayUTC, 'Seconds'));
         | 
| 56 | 
            +
                var vMillisecond = padWithZeros(datePart(date, displayUTC, 'Milliseconds'), 3);
         | 
| 57 | 
            +
                var vTimeZone = offset(date.getTimezoneOffset());
         | 
| 58 | 
            +
                var formatted = format
         | 
| 59 | 
            +
                    .replace(/dd/g, vDay)
         | 
| 60 | 
            +
                    .replace(/MM/g, vMonth)
         | 
| 61 | 
            +
                    .replace(/y{1,4}/g, vYear)
         | 
| 62 | 
            +
                    .replace(/hh/g, vHour)
         | 
| 63 | 
            +
                    .replace(/mm/g, vMinute)
         | 
| 64 | 
            +
                    .replace(/ss/g, vSecond)
         | 
| 65 | 
            +
                    .replace(/SSS/g, vMillisecond)
         | 
| 66 | 
            +
                    .replace(/O/g, vTimeZone);
         | 
| 67 | 
            +
                return formatted;
         | 
| 40 68 | 
             
            }
         | 
| 41 69 |  | 
| 42 | 
            -
            function  | 
| 43 | 
            -
              var  | 
| 44 | 
            -
             | 
| 45 | 
            -
                 | 
| 46 | 
            -
                date  | 
| 70 | 
            +
            function extractDateParts(pattern, str) {
         | 
| 71 | 
            +
              var matchers = [
         | 
| 72 | 
            +
                { pattern: /y{1,4}/, regexp: "\\d{1,4}", fn: function(date, value) { date.setFullYear(value); } },
         | 
| 73 | 
            +
                { pattern: /MM/, regexp: "\\d{1,2}", fn: function(date, value) { date.setMonth(value -1); } },
         | 
| 74 | 
            +
                { pattern: /dd/, regexp: "\\d{1,2}", fn: function(date, value) { date.setDate(value); } },
         | 
| 75 | 
            +
                { pattern: /hh/, regexp: "\\d{1,2}", fn: function(date, value) { date.setHours(value); } },
         | 
| 76 | 
            +
                { pattern: /mm/, regexp: "\\d\\d", fn: function(date, value) { date.setMinutes(value); } },
         | 
| 77 | 
            +
                { pattern: /ss/, regexp: "\\d\\d", fn: function(date, value) { date.setSeconds(value); } },
         | 
| 78 | 
            +
                { pattern: /SSS/, regexp: "\\d\\d\\d", fn: function(date, value) { date.setMilliseconds(value); } },
         | 
| 79 | 
            +
                { pattern: /O/, regexp: "[+-]\\d{3,4}|Z", fn: function(date, value) {
         | 
| 80 | 
            +
                  if (value === 'Z') {
         | 
| 81 | 
            +
                    value = 0;
         | 
| 82 | 
            +
                  }
         | 
| 83 | 
            +
                  var offset = Math.abs(value);
         | 
| 84 | 
            +
                  var minutes = (offset % 100) + (Math.floor(offset / 100) * 60);
         | 
| 85 | 
            +
                  date.setMinutes(date.getMinutes() + (value > 0 ? minutes : -minutes));
         | 
| 86 | 
            +
                } }
         | 
| 87 | 
            +
              ];
         | 
| 88 | 
            +
             | 
| 89 | 
            +
              var parsedPattern = matchers.reduce(function(p, m) {
         | 
| 90 | 
            +
                if (m.pattern.test(p.regexp)) {
         | 
| 91 | 
            +
                  m.index = p.regexp.match(m.pattern).index;
         | 
| 92 | 
            +
                  p.regexp = p.regexp.replace(m.pattern, "(" + m.regexp + ")");
         | 
| 93 | 
            +
                } else {
         | 
| 94 | 
            +
                  m.index = -1;
         | 
| 95 | 
            +
                }
         | 
| 96 | 
            +
                return p;
         | 
| 97 | 
            +
              }, { regexp: pattern, index: [] });
         | 
| 98 | 
            +
             | 
| 99 | 
            +
              var dateFns = matchers.filter(function(m) {
         | 
| 100 | 
            +
                return m.index > -1;
         | 
| 101 | 
            +
              });
         | 
| 102 | 
            +
              dateFns.sort(function(a, b) { return a.index - b.index; });
         | 
| 103 | 
            +
             | 
| 104 | 
            +
              var matcher = new RegExp(parsedPattern.regexp);
         | 
| 105 | 
            +
              var matches = matcher.exec(str);
         | 
| 106 | 
            +
              if (matches) {
         | 
| 107 | 
            +
                var date = module.exports.now();
         | 
| 108 | 
            +
                dateFns.forEach(function(f, i) {
         | 
| 109 | 
            +
                  f.fn(date, matches[i+1]);
         | 
| 110 | 
            +
                });
         | 
| 111 | 
            +
                return date;
         | 
| 47 112 | 
             
              }
         | 
| 48 | 
            -
             | 
| 49 | 
            -
               | 
| 50 | 
            -
             | 
| 113 | 
            +
             | 
| 114 | 
            +
              throw new Error('String \'' + str + '\' could not be parsed as \'' + pattern + '\'');
         | 
| 115 | 
            +
            }
         | 
| 116 | 
            +
             | 
| 117 | 
            +
            function parse(pattern, str) {
         | 
| 118 | 
            +
              if (!pattern) {
         | 
| 119 | 
            +
                throw new Error('pattern must be supplied');
         | 
| 51 120 | 
             
              }
         | 
| 52 121 |  | 
| 53 | 
            -
               | 
| 54 | 
            -
             | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
               | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
                .replace(/O/g, vTimeZone);
         | 
| 72 | 
            -
              return formatted;
         | 
| 73 | 
            -
             | 
| 74 | 
            -
            };
         | 
| 122 | 
            +
              return extractDateParts(pattern, str);
         | 
| 123 | 
            +
            }
         | 
| 124 | 
            +
             | 
| 125 | 
            +
            /**
         | 
| 126 | 
            +
             * Used for testing - replace this function with a fixed date.
         | 
| 127 | 
            +
             */
         | 
| 128 | 
            +
            function now() {
         | 
| 129 | 
            +
              return new Date();
         | 
| 130 | 
            +
            }
         | 
| 131 | 
            +
             | 
| 132 | 
            +
            module.exports = asString;
         | 
| 133 | 
            +
            module.exports.asString = asString;
         | 
| 134 | 
            +
            module.exports.parse = parse;
         | 
| 135 | 
            +
            module.exports.now = now;
         | 
| 136 | 
            +
            module.exports.ISO8601_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSS';
         | 
| 137 | 
            +
            module.exports.ISO8601_WITH_TZ_OFFSET_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSSO';
         | 
| 138 | 
            +
            module.exports.DATETIME_FORMAT = 'dd MM yyyy hh:mm:ss.SSS';
         | 
| 139 | 
            +
            module.exports.ABSOLUTETIME_FORMAT = 'hh:mm:ss.SSS';
         | 
    
        package/package.json
    CHANGED
    
    | @@ -1,26 +1,33 @@ | |
| 1 1 | 
             
            {
         | 
| 2 2 | 
             
              "name": "date-format",
         | 
| 3 | 
            -
              "version": "0.0 | 
| 4 | 
            -
              "description": " | 
| 3 | 
            +
              "version": "2.0.0",
         | 
| 4 | 
            +
              "description": "Formatting Date objects as strings since 2013",
         | 
| 5 5 | 
             
              "main": "lib/index.js",
         | 
| 6 | 
            -
              "scripts": {
         | 
| 7 | 
            -
                "test": "mocha"
         | 
| 8 | 
            -
              },
         | 
| 9 6 | 
             
              "repository": {
         | 
| 10 7 | 
             
                "type": "git",
         | 
| 11 8 | 
             
                "url": "https://github.com/nomiddlename/date-format.git"
         | 
| 12 9 | 
             
              },
         | 
| 10 | 
            +
              "engines": {
         | 
| 11 | 
            +
                "node": ">=4.0"
         | 
| 12 | 
            +
              },
         | 
| 13 | 
            +
              "scripts": {
         | 
| 14 | 
            +
                "lint": "eslint lib/* test/*",
         | 
| 15 | 
            +
                "pretest": "eslint lib/* test/*",
         | 
| 16 | 
            +
                "test": "mocha"
         | 
| 17 | 
            +
              },
         | 
| 13 18 | 
             
              "keywords": [
         | 
| 14 19 | 
             
                "date",
         | 
| 15 20 | 
             
                "format",
         | 
| 16 21 | 
             
                "string"
         | 
| 17 22 | 
             
              ],
         | 
| 18 | 
            -
              "author": "Gareth Jones <gareth. | 
| 23 | 
            +
              "author": "Gareth Jones <gareth.nomiddlename@gmail.com>",
         | 
| 19 24 | 
             
              "license": "MIT",
         | 
| 20 25 | 
             
              "readmeFilename": "README.md",
         | 
| 21 26 | 
             
              "gitHead": "bf59015ab6c9e86454b179374f29debbdb403522",
         | 
| 22 27 | 
             
              "devDependencies": {
         | 
| 23 | 
            -
                " | 
| 24 | 
            -
                "mocha": " | 
| 28 | 
            +
                "eslint": "^5.5.0",
         | 
| 29 | 
            +
                "eslint-plugin-mocha": "^5.2.0",
         | 
| 30 | 
            +
                "mocha": "^5.2.0",
         | 
| 31 | 
            +
                "should": "^13.2.3"
         | 
| 25 32 | 
             
              }
         | 
| 26 33 | 
             
            }
         | 
    
        package/test/date_format-test.js
    CHANGED
    
    | @@ -1,42 +1,64 @@ | |
| 1 | 
            -
             | 
| 2 | 
            -
             | 
| 3 | 
            -
             | 
| 1 | 
            +
            'use strict';
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require('should');
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            var dateFormat = require('../lib');
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            function createFixedDate() {
         | 
| 8 | 
            +
                return new Date(2010, 0, 11, 14, 31, 30, 5);
         | 
| 9 | 
            +
            }
         | 
| 4 10 |  | 
| 5 11 | 
             
            describe('date_format', function() {
         | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
                 | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 12 | 
            +
                var date = createFixedDate();
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                it('should default to now when a date is not provided', function() {
         | 
| 15 | 
            +
                    dateFormat.asString(dateFormat.DATETIME_FORMAT).should.not.be.empty();
         | 
| 16 | 
            +
                });
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                it('should be usable directly without calling asString', function() {
         | 
| 19 | 
            +
                    dateFormat(dateFormat.DATETIME_FORMAT, date).should.eql('11 01 2010 14:31:30.005');
         | 
| 20 | 
            +
                });
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                it('should format a date as string using a pattern', function() {
         | 
| 23 | 
            +
                    dateFormat.asString(dateFormat.DATETIME_FORMAT, date).should.eql('11 01 2010 14:31:30.005');
         | 
| 24 | 
            +
                });
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                it('should default to the ISO8601 format', function() {
         | 
| 27 | 
            +
                    dateFormat.asString(date).should.eql('2010-01-11T14:31:30.005');
         | 
| 28 | 
            +
                });
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                it('should provide a ISO8601 with timezone offset format', function() {
         | 
| 31 | 
            +
                    var tzDate = createFixedDate();
         | 
| 32 | 
            +
                    tzDate.setMinutes(tzDate.getMinutes() - tzDate.getTimezoneOffset() - 660);
         | 
| 33 | 
            +
                    tzDate.getTimezoneOffset = function () {
         | 
| 34 | 
            +
                        return -660;
         | 
| 35 | 
            +
                    };
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                    // when tz offset is in the pattern, the date should be in UTC
         | 
| 38 | 
            +
                    dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
         | 
| 39 | 
            +
                        .should.eql('2010-01-11T03:31:30.005+1100');
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                    tzDate = createFixedDate();
         | 
| 42 | 
            +
                    tzDate.setMinutes((tzDate.getMinutes() - tzDate.getTimezoneOffset()) + 120);
         | 
| 43 | 
            +
                    tzDate.getTimezoneOffset = function () {
         | 
| 44 | 
            +
                        return 120;
         | 
| 45 | 
            +
                    };
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                    dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
         | 
| 48 | 
            +
                        .should.eql('2010-01-11T16:31:30.005-0200');
         | 
| 49 | 
            +
                });
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                it('should provide a just-the-time format', function() {
         | 
| 52 | 
            +
                    dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date).should.eql('14:31:30.005');
         | 
| 53 | 
            +
                });
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                it('should provide a custom format', function() {
         | 
| 56 | 
            +
                    var customDate = createFixedDate();
         | 
| 57 | 
            +
                    customDate.setMinutes((customDate.getMinutes() - customDate.getTimezoneOffset()) + 120);
         | 
| 58 | 
            +
                    customDate.getTimezoneOffset = function () {
         | 
| 59 | 
            +
                        return 120;
         | 
| 60 | 
            +
                    };
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                    dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.16.11.01.10');
         | 
| 63 | 
            +
                });
         | 
| 42 64 | 
             
            });
         | 
| @@ -0,0 +1,139 @@ | |
| 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() { dateFormat.parse() }).should.throw(/pattern must be supplied/);
         | 
| 9 | 
            +
                (function() { dateFormat.parse(null) }).should.throw(/pattern must be supplied/);
         | 
| 10 | 
            +
                (function() { dateFormat.parse('') }).should.throw(/pattern must be supplied/);
         | 
| 11 | 
            +
              });
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              describe('with a pattern that has no replacements', function() {
         | 
| 14 | 
            +
                it('should return a new date when the string matches', function() {
         | 
| 15 | 
            +
                  dateFormat.parse('cheese', 'cheese').should.be.a.Date()
         | 
| 16 | 
            +
                });
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                it('should throw if the string does not match', function() {
         | 
| 19 | 
            +
                  (function() {
         | 
| 20 | 
            +
                    dateFormat.parse('cheese', 'biscuits');
         | 
| 21 | 
            +
                  }).should.throw(/String 'biscuits' could not be parsed as 'cheese'/);
         | 
| 22 | 
            +
                });
         | 
| 23 | 
            +
              });
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              describe('with a full pattern', function() {
         | 
| 26 | 
            +
                var pattern = 'yyyy-MM-dd hh:mm:ss.SSSO';
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                it('should return the correct date if the string matches', function() {
         | 
| 29 | 
            +
                  var testDate = new Date();
         | 
| 30 | 
            +
                  testDate.setFullYear(2018);
         | 
| 31 | 
            +
                  testDate.setMonth(8);
         | 
| 32 | 
            +
                  testDate.setDate(13);
         | 
| 33 | 
            +
                  testDate.setHours(18);
         | 
| 34 | 
            +
                  testDate.setMinutes(10);
         | 
| 35 | 
            +
                  testDate.setSeconds(12);
         | 
| 36 | 
            +
                  testDate.setMilliseconds(392);
         | 
| 37 | 
            +
                  testDate.getTimezoneOffset = function() { return 600; };
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                  dateFormat.parse(pattern, '2018-09-13 08:10:12.392+1000').getTime().should.eql(testDate.getTime());
         | 
| 40 | 
            +
                });
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                it('should throw if the string does not match', function() {
         | 
| 43 | 
            +
                  (function() {
         | 
| 44 | 
            +
                    dateFormat.parse(pattern, 'biscuits')
         | 
| 45 | 
            +
                  }).should.throw(/String 'biscuits' could not be parsed as 'yyyy-MM-dd hh:mm:ss.SSSO'/);
         | 
| 46 | 
            +
                });
         | 
| 47 | 
            +
              });
         | 
| 48 | 
            +
             | 
| 49 | 
            +
              describe('with a partial pattern', function() {
         | 
| 50 | 
            +
                var testDate = new Date();
         | 
| 51 | 
            +
                dateFormat.now = function() { return testDate; };
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                function verifyDate(actual, expected) {
         | 
| 54 | 
            +
                  actual.getFullYear().should.eql(expected.year || testDate.getFullYear());
         | 
| 55 | 
            +
                  actual.getMonth().should.eql(expected.month || testDate.getMonth());
         | 
| 56 | 
            +
                  actual.getDate().should.eql(expected.day || testDate.getDate());
         | 
| 57 | 
            +
                  actual.getHours().should.eql(expected.hours || testDate.getHours());
         | 
| 58 | 
            +
                  actual.getMinutes().should.eql(expected.minutes || testDate.getMinutes());
         | 
| 59 | 
            +
                  actual.getSeconds().should.eql(expected.seconds || testDate.getSeconds());
         | 
| 60 | 
            +
                  actual.getMilliseconds().should.eql(expected.milliseconds || testDate.getMilliseconds());
         | 
| 61 | 
            +
                }
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                it('should return a date with missing values defaulting to current time', function() {
         | 
| 64 | 
            +
                  var date = dateFormat.parse('yyyy-MM', '2015-09');
         | 
| 65 | 
            +
                  verifyDate(date, { year: 2015, month: 8 });
         | 
| 66 | 
            +
                });
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                it('should handle variations on the same pattern', function() {
         | 
| 69 | 
            +
                  var date = dateFormat.parse('MM-yyyy', '09-2015');
         | 
| 70 | 
            +
                  verifyDate(date, { year: 2015, month: 8 });
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                  date = dateFormat.parse('yyyy MM', '2015 09');
         | 
| 73 | 
            +
                  verifyDate(date, { year: 2015, month: 8 });
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                  date = dateFormat.parse('MM, yyyy.', '09, 2015.');
         | 
| 76 | 
            +
                  verifyDate(date, { year: 2015, month: 8 });
         | 
| 77 | 
            +
                });
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                it('should match all the date parts', function() {
         | 
| 80 | 
            +
                  var date = dateFormat.parse('dd', '21');
         | 
| 81 | 
            +
                  verifyDate(date, { day: 21 });
         | 
| 82 | 
            +
             | 
| 83 | 
            +
                  date = dateFormat.parse('hh', '12');
         | 
| 84 | 
            +
                  verifyDate(date, { hours: 12 });
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                  date = dateFormat.parse('mm', '34');
         | 
| 87 | 
            +
                  verifyDate(date,  { minutes: 34 });
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                  date = dateFormat.parse('ss', '59');
         | 
| 90 | 
            +
                  verifyDate(date, { seconds: 59 });
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                  date = dateFormat.parse('ss.SSS', '23.452');
         | 
| 93 | 
            +
                  verifyDate(date, { seconds: 23, milliseconds: 452 });
         | 
| 94 | 
            +
             | 
| 95 | 
            +
                  date = dateFormat.parse('hh:mm O', '05:23 +1000');
         | 
| 96 | 
            +
                  verifyDate(date, { hours: 15, minutes: 23 });
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                  date = dateFormat.parse('hh:mm O', '05:23 -200');
         | 
| 99 | 
            +
                  verifyDate(date, { hours: 3, minutes: 23 });
         | 
| 100 | 
            +
             | 
| 101 | 
            +
                  date = dateFormat.parse('hh:mm O', '05:23 +0930');
         | 
| 102 | 
            +
                  verifyDate(date, { hours: 14, minutes: 53 });
         | 
| 103 | 
            +
                });
         | 
| 104 | 
            +
              });
         | 
| 105 | 
            +
             | 
| 106 | 
            +
              describe('with a date formatted by this library', function() {
         | 
| 107 | 
            +
                var testDate = new Date();
         | 
| 108 | 
            +
                testDate.setUTCFullYear(2018);
         | 
| 109 | 
            +
                testDate.setUTCMonth(8);
         | 
| 110 | 
            +
                testDate.setUTCDate(13);
         | 
| 111 | 
            +
                testDate.setUTCHours(18);
         | 
| 112 | 
            +
                testDate.setUTCMinutes(10);
         | 
| 113 | 
            +
                testDate.setUTCSeconds(12);
         | 
| 114 | 
            +
                testDate.setUTCMilliseconds(392);
         | 
| 115 | 
            +
             | 
| 116 | 
            +
                it('should format and then parse back to the same date', function() {
         | 
| 117 | 
            +
                  dateFormat.parse(
         | 
| 118 | 
            +
                    dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
         | 
| 119 | 
            +
                    dateFormat(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, testDate)
         | 
| 120 | 
            +
                  ).should.eql(testDate);
         | 
| 121 | 
            +
             | 
| 122 | 
            +
                  dateFormat.parse(
         | 
| 123 | 
            +
                    dateFormat.ISO8601_FORMAT,
         | 
| 124 | 
            +
                    dateFormat(dateFormat.ISO8601_FORMAT, testDate)
         | 
| 125 | 
            +
                  ).should.eql(testDate);
         | 
| 126 | 
            +
             | 
| 127 | 
            +
                  dateFormat.parse(
         | 
| 128 | 
            +
                    dateFormat.DATETIME_FORMAT,
         | 
| 129 | 
            +
                    dateFormat(dateFormat.DATETIME_FORMAT, testDate)
         | 
| 130 | 
            +
                  ).should.eql(testDate);
         | 
| 131 | 
            +
             | 
| 132 | 
            +
                  dateFormat.parse(
         | 
| 133 | 
            +
                    dateFormat.ABSOLUTETIME_FORMAT,
         | 
| 134 | 
            +
                    dateFormat(dateFormat.ABSOLUTETIME_FORMAT, testDate)
         | 
| 135 | 
            +
                  ).should.eql(testDate);
         | 
| 136 | 
            +
                });
         | 
| 137 | 
            +
             | 
| 138 | 
            +
              });
         | 
| 139 | 
            +
            });
         |