Skip to content

Commit

Permalink
Starting work on the iterative parser
Browse files Browse the repository at this point in the history
  • Loading branch information
timrwood committed Apr 8, 2012
1 parent 2daf097 commit 508f794
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
66 changes: 65 additions & 1 deletion moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,54 @@

var moment,
round = Math.round,
i,
// internal storage for language config files
languages = {},

// check for nodeJS
hasModule = (typeof module !== 'undefined'),

// parameters to check for on the lang config
paramsToParse = 'months|monthsShort|monthsParse|weekdays|weekdaysShort|longDateFormat|calendar|relativeTime|ordinal|meridiem'.split('|'),
i,

// ASP.NET json date format regex
jsonRegex = /^\/?Date\((\-?\d+)/i,

// format tokens
charactersToReplace = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?|LT|LL?L?L?)/g,

// regexes for format z, zz
nonuppercaseLetters = /[^A-Z]/g,
timezoneRegex = /\([A-Za-z ]+\)|:[0-9]{2} [A-Z]{3} /g,

// parsing tokens
tokenCharacters = /(\\)?(MM?M?M?|dd?d?d|DD?D?D?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|ZZ?|T)/g,
inputCharacters = /(\\)?([0-9]{1,2}[\u6708\uC6D4]|[0-9]+|([a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+|([\+\-]\d\d:?\d\d))/gi,

// parsing token regexes
parseTokenFourDigits = /[0-9]{4}/, // 0000 - 9999
parseTokenTwoDigits = /[0-9]{2}/, // 00 - 99
parseTokenOneOrTwoDigits = /[0-9]{1,2}/, // 0 - 99
parseTokenWord = /[0-9a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+/i, // any word characters or numbers
parseTokenTimezone = /[\+\-]\d\d:?\d\d/i, // +00:00 -00:00 +0000 -0000

// preliminary iso regex
// 0000-00-00 + T + 00 or 00:00 or 00:00:00 + +00:00 or +0000
isoRegex = /^\s*\d{4}-\d\d-\d\d(T(\d\d(:\d\d(:\d\d)?)?)?([\+\-]\d\d:?\d\d)?)?/,
isoFormat = 'YYYY-MM-DDTHH:mm:ssZ',

// iso time formats and regexes
isoTimes = [
['HH:mm:ss', /T\d\d:\d\d:\d\d/],
['HH:mm', /T\d\d:\d\d/],
['HH', /T\d\d/]
],

// timezone chunker "+10:00" > ["10", "00"] or "-1530" > ["-15", "30"]
timezoneParseRegex = /([\+\-]|\d\d)/gi,
VERSION = "1.5.0",

// getter and setter names
shortcuts = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|');

// Moment prototype object
Expand Down Expand Up @@ -219,6 +248,41 @@
return inputString.replace(charactersToReplace, replaceFunction);
}

// get the regex to find the next token
function getParseRegexForToken(token) {
switch (token) {
case 'YYYY':
return parseTokenFourDigits;
case 'MM':
case 'DD':
case 'dd':
case 'YY':
case 'HH':
case 'hh':
case 'mm':
case 'ss':
return parseTokenTwoDigits;
case 'M':
case 'D':
case 'd':
case 'H':
case 'h':
case 'm':
case 's':
return parseTokenOneOrTwoDigits;
case 'MMM':
case 'MMMM':
case 'ddd':
case 'dddd':
case 'a':
case 'A':
return parseTokenWord;
case 'Z':
case 'ZZ':
return parseTokenTimezone;
}
}

// date from string and format string
function makeDateFromStringAndFormat(string, format) {
var inArray = [0, 0, 1, 0, 0, 0, 0],
Expand Down
17 changes: 17 additions & 0 deletions test/moment/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ exports.create = {
test.done();
},

"string with format no separators" : function(test) {
moment.lang('en');
var a = [
['MMDDYYYY', '12021999'],
['DDMMYYYY', '12021999'],
['YYYYMMDD', '19991202']
],i;

test.expect(a.length);

for (i = 0; i < a.length; i++) {
test.equal(moment(a[i][1], a[i][0]).format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]);
}

test.done();
},

"string with format (timezone)" : function(test) {
test.expect(8);
test.equal(moment('5 -0700', 'H ZZ').toDate().getUTCHours(), 12, 'parse hours "5 -0700" ---> "H ZZ"');
Expand Down

0 comments on commit 508f794

Please sign in to comment.