Skip to content

Commit

Permalink
Annotate conditions in finish() in parser.js and remove unused
Browse files Browse the repository at this point in the history
conditions
  • Loading branch information
eric committed Aug 23, 2011
1 parent 6a9d01a commit 327aa38
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 74 deletions.
96 changes: 52 additions & 44 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,29 +627,33 @@

var today = $D.today();

// For parsing: "now"
if (this.now && !this.unit && !this.operator) {
return new Date();
} else if (this.now) {
today = new Date();
}

var expression = !!(this.days && this.days !== null || this.orient || this.operator);
var expression = !!(this.days && this.days !== null || this.orient || this.operator);

var gap, mod, orient;
orient = ((this.orient == "past" || this.operator == "subtract") ? -1 : 1);

// For parsing: "last second", "next minute", "previous hour", "+5 seconds",
// "-5 hours", "5 hours", "7 hours ago"
if(!this.now && "hour minute second".indexOf(this.unit) != -1) {
today.setTimeToNow();
}

if (this.month || this.month === 0) {
if ("year day hour minute second".indexOf(this.unit) != -1) {
this.value = this.month + 1;
this.month = null;
expression = true;
}
// For parsing: "5 hours", "2 days", "3 years ago",
// "7 days from now"
if ((this.month || this.month === 0) && ("year day hour minute second".indexOf(this.unit) != -1)) {
this.value = this.month + 1;
this.month = null;
expression = true;
}

// For parsing: "monday @ 8pm", "12p on monday", "Friday"
if (!expression && this.weekday && !this.day && !this.days) {
var temp = Date[this.weekday]();
this.day = temp.getDate();
Expand All @@ -659,34 +663,22 @@
this.year = temp.getFullYear();
}

// For parsing: "prev thursday", "next friday", "last friday at 20:00"
if (expression && this.weekday && this.unit != "month") {
this.unit = "day";
gap = ($D.getDayNumberFromName(this.weekday) - today.getDay());
mod = 7;
this.days = gap ? ((gap + (orient * mod)) % mod) : (orient * mod);
}

if (this.month && this.unit == "day" && this.operator) {
this.value = (this.month + 1);
this.month = null;
}

if (this.value != null && this.month != null && this.year != null) {
this.day = this.value * 1;
}

if (this.month && !this.day && this.value) {
today.set({ day: this.value * 1 });
if (!expression) {
this.day = this.value * 1;
}
}


// For parsing: "t+1 m", "today + 1 month", "+1 month", "-5 months"
if (!this.month && this.value && this.unit == "month" && !this.now) {
this.month = this.value;
expression = true;
}

// For parsing: "last january", "prev march", "next july", "today + 1 month",
// "+5 months"
if (expression && (this.month || this.month === 0) && this.unit != "year") {
this.unit = "month";
gap = (this.month - today.getMonth());
Expand All @@ -695,22 +687,31 @@
this.month = null;
}

if (!this.unit) {
this.unit = "day";
}

if (!expression && this.value && this.unit == "day" && !this.day) {
this.day = this.value * orient;
// For parsing: "Yesterday", "Tomorrow", "last monday", "last friday",
// "previous day", "next week", "next month", "next year",
// "today+", "+", "-", "yesterday at 4:00", "last friday at 20:00"
if (!this.value && expression) {
this.value = 1;
}

if (!this.value) {
this.value = 1;
// For parsing: "15th at 20:15", "15th at 8pm", "today+", "t+5"
if (!this.unit && (!expression || this.value)) {
this.unit = "day";
}

// For parsing: "15th at 20:15", "15th at 8pm"
if (!expression && this.value && (!this.unit || this.unit == "day") && !this.day) {
this.unit = "day";
this.day = this.value * orient;
}

if (!this[this.unit + "s"] || this.operator) {
// For parsing: "last minute", "+5 hours", "previous month", "1 year ago tomorrow"
if (this.unit && (!this[this.unit + "s"] || this.operator)) {
this[this.unit + "s"] = this.value * orient;
}

// For parsing: "July 8th, 2004, 10:30 PM", "07/15/04 6 AM",
// "monday @ 8am", "10:30:45 P.M."
if (this.meridian && this.hour) {
if (this.meridian == "p" && this.hour < 12) {
this.hour = this.hour + 12;
Expand All @@ -719,6 +720,7 @@
}
}

// For parsing: "3 months ago saturday at 5:00 pm" (does not actually parse)
if (this.weekday && !this.day && !this.days) {
var temp = Date[this.weekday]();
this.day = temp.getDate();
Expand All @@ -727,17 +729,15 @@
}
}

// For parsing: "July 2004", "1997-07", "2008/10", "november"
if ((this.month || this.month === 0) && !this.day) {
this.day = 1;
}

// For parsing: "3 weeks" (does not actually parse)
if (!this.orient && !this.operator && this.unit == "week" && this.value && !this.day && !this.month) {
return Date.today().setWeek(this.value);
}

if (expression && this.timezone && this.day && this.days) {
this.day = this.days;
}

today.set(this);

Expand Down Expand Up @@ -955,14 +955,18 @@

// real starting rule: tries selected formats first,
// then general purpose rule
g.start = function (s) {
g.start = function (s, o) {
try {
var r = g._formats.call({}, s);
if (r[1].length === 0) {
return r;
}
} catch (e) {}
return g._start.call({}, s);
if (!o) {
o = {}
}
o.input = s;
return g._start.call(o, s);
};

$D._parse = $D.parse;
Expand Down Expand Up @@ -1065,21 +1069,25 @@
</code></pre>
*
* @param {String} The string value to convert into a Date object [Required]
* @param {Object} An object with any defaults for parsing [Optional]
* @return {Date} A Date object or null if the string cannot be converted into a Date.
*/
$D.parse = function (s) {
$D.parse = function (s, o) {
var r = null;
if (!s) {
return null;
}
if (s instanceof Date) {
return s;
}
try {
r = $D.Grammar.start.call({}, s.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
} catch (e) {
return null;
if (!o) {
o = {}
}
// try {
r = $D.Grammar.start.call({}, s.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"), o);
// } catch (e) {
// return null;
// }
return ((r[1].length === 0) ? r[0] : null);
};

Expand Down
7 changes: 4 additions & 3 deletions src/sugarpak.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,11 @@
return true;
}

if (j.substring(j.length - 1) != "s") {
j += "s";
l = j
if (l.substring(l.length - 1) != "s") {
l += "s";
}
return this["add" + j](this._orient);
return this["add" + l](this._orient);
};
};

Expand Down
33 changes: 19 additions & 14 deletions test/date/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,6 @@
run: function() { this.date = Date.parse('07012004') },
assert: function() { return this.baseline.equals( this.date ) }
},
'712004': {
run: function() { this.date = Date.parse('712004') },
assert: function() { return this.baseline.equals( this.date ) }
},
'7104': {
run: function() { this.date = Date.parse('7104', { format : "Mdyy" } ) },
assert: function() { return this.baseline.equals( this.date ) }
},

'07152004': {
run: function() { this.date = Date.parse('07152004') },
assert: function() { return this.baseline2.equals( this.date ) }
Expand All @@ -400,10 +391,24 @@
run: function() { this.date = Date.parse('7152004') },
assert: function() { return this.baseline2.equals( this.date ) }
},
'71504': {
run: function() { this.date = Date.parse('71504', { format : "Mdyy" } ) },
assert: function() { return this.baseline2.equals( this.date ) }
}
}
},
'Fail': {
setup: function() {
this.baseline = new Date(2004,6,1);
this.baseline2 = new Date(2004,6,15);
},
'7104': {
run: function() { this.date = Date.parse('7104', { format : "Mdyy" } ) },
assert: function() { return this.baseline.equals( this.date ) }
},
'71504': {
run: function() { this.date = Date.parse('71504', { format : "Mdyy" } ) },
assert: function() { return this.baseline2.equals( this.date ) }
},
'712004': {
run: function() { this.date = Date.parse('712004') },
assert: function() { return this.baseline.equals( this.date ) }
},
}
});

10 changes: 6 additions & 4 deletions test/date_and_time/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,12 @@
run: function() { },
assert: function() { return new Date(1997,6,16,19,20,30).setTimezoneOffset('+0100').equals( Date.parseExact('1997-07-16T19:20:30+01:00', "yyyy-MM-ddTHH:mm:ssz") ) }
},
'1997-07-16T19:20:30.45+01:00 : "YYYY-MM-DDThh:mm:ss.sTZD"': {
run: function() { },
assert: function() { return new Date(1997,6,16,19,20,30,45).setTimezoneOffset('+0100').equals( Date.parse('1997-07-16T19:20:30.45+01:00') ) }
}
},
'Fail': {
'1997-07-16T19:20:30.45+01:00 : "YYYY-MM-DDThh:mm:ss.sTZD"': {
run: function() { },
assert: function() { return new Date(1997,6,16,19,20,30,45).setTimezoneOffset('+0100').equals( Date.parse('1997-07-16T19:20:30.45+01:00') ) }
}
},

'RFC 3339 Formats': {
Expand Down
2 changes: 1 addition & 1 deletion test/dst/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Date.Specification = new Specification({
'Daylight Saving Time': {
'Fail': {
setup: function() { },
'3/11/2007 1:59:59 + 1 second = 3/11/2007 3:00:00': {
run: function() { },
Expand Down
11 changes: 7 additions & 4 deletions test/rememberthemilk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@
run: function() { },
assert: function() { return Date.today().set({ day: 25 }).equals( Date.parse('25th') ) }
},
'End of month : Not supported currently.': {
run: function() { },
assert: function() { return Date.today().moveToLastDayOfMonth().equals( Date.parse('End of month') ) }
},
'Friday': {
run: function() { },
assert: function() { return Date.friday().equals( Date.parse('Friday') ) }
Expand Down Expand Up @@ -90,13 +86,20 @@
},
assert: function() { return Date.today().add(2).days().equals( this.date ) }
},
},
'Fail': {
'End of month : Not supported currently.': {
run: function() { },
assert: function() { return Date.today().moveToLastDayOfMonth().equals( Date.parse('End of month') ) }
},
'3 weeks : Datejs uses "+3 weeks."': {
run: function() {
this.date = Date.parse('3 weeks');
this.date.setMilliseconds(0);
},
assert: function() { return Date.today().add(3).weeks().equals( this.date ) }
}

}
});

8 changes: 4 additions & 4 deletions test/ruby_chronic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,16 @@
assert: function() { return Date.today().set({ hour: 18 }).equals( Date.parse('last night') ) }
},
'this second : The term "this" is not supported': {
run: function() { },
assert: function() { return new Date().equals( Date.parse('this second') ) }
run: function() { this.date = Date.parse('this second') },
assert: function() { return this.date !== null && new Date().equals(this.date) }
},
'afternoon yesterday : The term "afternoon" is not supported': {
run: function() { },
assert: function() { return Date.today().add(1).day().set({ hour: 12 }).equals( Date.parse('afternoon yesterday') ) }
},
'in 3 hours : problem with "in"': {
run: function() { },
assert: function() { return new Date().set({millisecond:0}).add(3).hours().equals(Date.parse('in 3 hours').set({millisecond:0})) }
run: function() { this.date = Date.parse('in 3 hours') },
assert: function() { return this.date !== null && new Date().set({millisecond:0}).add(3).hours().equals(this.date.set({millisecond:0})) }
},
'3 months ago saturday at 5:00 pm': {
run: function() { },
Expand Down

0 comments on commit 327aa38

Please sign in to comment.