Skip to content

Commit

Permalink
Minor update to smalltime.js
Browse files Browse the repository at this point in the history
- Added ‘ago’ and number words from 0-99
- times specified without date are assumed to be the first future
instance (today or tomorrow) of that time
  • Loading branch information
RobTrew committed Apr 22, 2014
1 parent c9eb25a commit 485f3f4
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 28 deletions.
Expand Up @@ -63,6 +63,7 @@ _Informal days_ | Preceding or following the name of the month, and assumed to b
_Informal years_ | Before or after the month and day. Assumed to be the first future instance of the date) if omitted | `12 June, 2015 june 12, june 12 2015`
_Last_ | most recent past instance | `last wednesday, last jan 12, last month` _(midnight at the start of the month before this)_
_Next_ | soonest future instance after the current week | `next friday` _(not the friday at the end of this week, if one remains, but the following friday)_
_Ago_ | Before the current moment (if the unit is hours), otherwise, before midnight last night | `two hours ago`, `3 weeks ago`

#### Installation in FoldingText Dev 2, or TaskPaper Dev 3
- From the application's main menu, choose `File > Open Application Folder`
Expand Down
Expand Up @@ -140,4 +140,25 @@ define(function(require, exports, module) {
});
});

// List date phrases and their ISO translations to the console
// window.peformAction('test date phrases')
Extensions.add('com.foldingtext.editor.commands', {
name: 'test date phrases',
description: 'translate date phrase to ISO 8601',
performCommand: function (editor) {
var lstIn = ['fifth june', '3rd','four hours ago','five days ago', '2h ago', '3d ago', 'a month ago', 'last month +3h','+1', '-1','w', 'd', 'y', '16h', '1m', '16H', '1M', 'now + 1h', 'now + 1m', 'o', 'next month', 'last month', '2021 aug 10', '2021 10 aug ', 'aug 10 2021', '10 aug 2021', '21:15','aug', 'jan', '6am january 3', '3 jan 6am', 'today +7d', '11:20 +7d', 'today', 'today -7d', '11:20 -7d', 'last thursday', 'next thursday', 'last week', 'next week', 'in 18 months', 'in 2 months', 'yesterday 8am', 'yesterday 8:00','+2d +3d', 'aug 10 2019', '+3y', 'jan 10 -3d', 'tomorrow 2pm', 'aug 10 2019', 'aug 2019', 'today -7d', '11:20 +4d', '2014-02-15 +1w', 'jan 10', 'jan 10 2pm', 'jan 10 11am', '10 jan at 10am', 'now-3d', '+7d', '7', '11:15', '11:15p', '11p', 'jan 5', 'now', 'now +4h', 'today + 1w', '1w', '1w', '8am monday', 'monday 8am', '8am tomorrow', 'wed at 10', 'friday 2pm', 'monday', 'next monday', 'tuesday', 'next tuesday', 'wednesday', 'next wednesday', 'thursday', 'next thursday', 'friday', 'next friday', 'saturday', 'next saturday', 'sunday', 'next sunday'],
lenTest = lstIn.length, i, dctResult=null,
strPhrase='', strTrans='', strOut='';

for (i=0; i<lenTest; i++) {
strPhrase = lstIn[i];


strTrans = dateLogic.datePhraseToISO(strPhrase);
strOut += ['[' + strPhrase + ']→', strTrans].join('\t') + '\n';
}
console.log(strOut);
}
});

});
Expand Up @@ -10,9 +10,11 @@ define(function(require, exports, module) {
// informal phrases like 'now +7d', 'thu', jan 12 2pm' to ISO string
// yyyy-mm-dd [HH:MM] (unless 00:00)
// returns strPhrase itself if can not be parsed
function datePhraseToISO(strPhrase) {
var iWeekStart = 1, //Monday (or Sunday=0)
dte = phraseToDate(strPhrase, iWeekStart);
function datePhraseToISO(strPhrase, iWeekStart) {
if (typeof iWeekStart === "undefined" || iWeekStart === null) {
iWeekStart = 1; //Monday=1 (or Sunday=0)
}
var dte = phraseToDate(strPhrase, iWeekStart);
if (dte) {
if (isNaN(dte)) {
return strPhrase;
Expand Down Expand Up @@ -42,6 +44,15 @@ define(function(require, exports, module) {
}
}

// if a time specified for today has already passed, assume tomorrow
function adjustDay(dteAnchor) {
if (dteAnchor < new Date()) {
if (dteAnchor > (new Date().setHours(0,0,0,0))) {
dteAnchor.setDate(dteAnchor.getDate() +1);
}
}
}

// informal phrases like 'now +7d', 'thu', jan 12 2pm' to .js Date()
function phraseToDate(strPhrase, iWeekStart) {
if (typeof iWeekStart === "undefined" || iWeekStart === null) {
Expand All @@ -55,29 +66,34 @@ define(function(require, exports, module) {
'wk','day', 'month', 'o'],
lstTime = ['h', 'hr', 'hour', 'm', 'min', 'minute'],
lstAmPm = ['am', 'pm', 'a', 'p'], lstSign = ['+', '-'],
lstMonths = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
lstShift = ['next', 'last'],
dctMonths = {'jan':0, 'feb':1, 'mar':2, 'apr':3, 'may':4, 'jun':5,
'jul':6, 'aug':7, 'sep':8, 'oct':9, 'nov':10, 'dec':11},
dctShift = {'next':1, 'last':-1, 'ago':0},
dctNum = {'zero':0, 'one':1, 'two':2, 'three':3, 'four':4,
'five':5, 'six':6, 'seven':7, 'eight':8, 'nine':9, 'ten':10,
'eleven':11, 'twelve':12, 'thirteen':13, 'fourteen':14,
'fifteen':15, 'sixteen':16, 'seventeen':17, 'eighteen':18,
'nineteen':19, 'twenty':20, 'thirty':30, 'forty':40,
'fifty':50, 'sixty':60, 'seventy':70, 'eighty': 80,
'ninety':90},
dctOrd = {'first':1, 'second':2, 'third':3, 'fifth':5,
'eighth':8, 'twelfth':12},
lstNth = ['st','nd','rd','th'],
dctAnchor = extractISODate(strPhrase),
dteAnchor = dctAnchor['date'],
rDelta = 0, dteResult = null,
lstTokens = tokens(dctAnchor['rest']),
lngTokens = lstTokens.length,
strTkn = '', strLower = '', iToday, iWkDay, lngDays,
lngTokens = lstTokens.length, blnOrd = false,
strBase = '', strAffix = '',
strTkn = '', strLower = '', iToday, iWkDay, iDay,
lngSign =+1, rQuant = 0, strUnit='d', i,
blnDate = false, blnNewQuant = false, strAbbrev ='',
blnNewUnit = false, blnPostColon = false, blnNextLast = false;

// Closure - shares core variables with parent function
// Closure - shares core variables with parent function phraseToDate
function upDate(strUnit) {
var lngYear, strMonth='', iMonth=-1, dteToday, rYearDelta=0;
var lngYear, strMonth='', iMonth=-1,
dteToday=null, dteNow=null, rYearDelta=0;
rQuant *= lngSign;
switch (strUnit) {
case 'w':
Expand All @@ -88,15 +104,26 @@ define(function(require, exports, module) {
rDelta += (rQuant * WEEK_MSECS);
} break;
case 'd':
rDelta += (rQuant * DAY_MSECS); break;
if (blnOrd) {
if (rQuant < dteAnchor.getDate()) {
dteAnchor.setMonth(dteAnchor.getMonth() + 1);
}
dteAnchor.setDate(rQuant);
} else {
rDelta += (rQuant * DAY_MSECS);
} break;
case 'h': // add quantity of hours
if (!blnDate) {dteAnchor = new Date(); blnDate = true;}
rDelta += (rQuant * 3600000); break;
case 'H': // set the clock hour
dteAnchor.setHours(~~rQuant); break;
dteAnchor.setHours(~~rQuant);
adjustDay(dteAnchor); break;
case 'm': // add quantity of minutes
if (!blnDate) {dteAnchor = new Date(); blnDate = true;}
rDelta += (rQuant * 60000); break;
case 'M': // set the clock minutes
dteAnchor.setMinutes(~~rQuant); break;
dteAnchor.setMinutes(~~rQuant);
adjustDay(dteAnchor); break;
case 'o': // month(s)
if ((rYearDelta = ~~(rQuant / 12)) !== 0) {
dteAnchor.setFullYear(dteAnchor.getFullYear()+rYearDelta);
Expand All @@ -114,24 +141,27 @@ define(function(require, exports, module) {
case 'a':
if (!blnPostColon) {
dteAnchor.setHours(~~rQuant);
} break;
adjustDay(dteAnchor);
}
break;
case 'p':
if (blnPostColon) {
if (dteAnchor.getHours() < 12) {
dteAnchor.setHours(dteAnchor.getHours() + 12);
adjustDay(dteAnchor);
}
} else {
if (rQuant < 12) {
dteAnchor.setHours(~~rQuant + 12);
} else {
dteAnchor.setHours(~~rQuant);
}
adjustDay(dteAnchor);
} break;
default:
if (strUnit.length >= 3) {
iMonth = lstMonths.indexOf(strUnit);
if (iMonth !== -1) {
dteAnchor.setMonth(iMonth);
if (strUnit in dctMonths) {
dteAnchor.setMonth(dctMonths[strUnit]);
if (rQuant <= 31) {
if (rQuant) {
dteAnchor.setDate(rQuant);
Expand All @@ -149,11 +179,10 @@ define(function(require, exports, module) {
}
}
}
blnNewUnit = blnNewQuant = false; blnNextLast = false;
blnNewUnit = blnNewQuant = false; blnNextLast = false; rQuant = 0;
blnDate = true; lngSign = 1; //scope of sign limited to one number
}


if (lngTokens) { // get a base date,
if (dteAnchor) {
blnDate = true;
Expand All @@ -163,18 +192,33 @@ define(function(require, exports, module) {
dteAnchor.setHours(0,0,0,0);
}
for (i=0; i<lngTokens; i++) { // tokens adjust the Anchor date or Delta
strTkn = lstTokens[i];
strTkn = lstTokens[i]; strBase = strAffix = '';
if (strTkn) {
blnOrd = false; strAffix = '';
strLower = strTkn.toLowerCase();
if (strLower.length > 1) {
strAffix = strLower.slice(-2);
if (strAffix === 'th') {strBase = strLower.slice(0,-2);}
}

// normalise any numeric
//debugger;
if (strLower in dctNum) {
strLower = dctNum[strLower].toString();
} else if (strLower in dctOrd) {
strLower = dctOrd[strLower].toString();
blnOrd = true;
} else if (strBase && strBase in dctNum) {
strLower = dctNum[strBase].toString();
blnOrd = true;
}

if (strLower.slice(-1) === 's') {
strLower = strLower.slice(0,-1);
}
strAbbrev = strLower.slice(0,4);
if (!isNaN(strLower)) {
rQuant = parseFloat(strLower);
rQuant += parseFloat(strLower);
blnNewQuant = true;
if (rQuant > 2000 && rQuant < 2500) {
if (blnNewUnit) {
Expand All @@ -183,19 +227,21 @@ define(function(require, exports, module) {
strUnit = 'y'; blnNewUnit = true;
}
}
} else if (lstNth.indexOf(strLower) !== -1) {
blnOrd = true;
} else if (lstDate.indexOf(strLower) !== -1) {
if (strLower !== 'month') {
strUnit = strTkn[0];
} else {
strUnit = 'o';
}
blnDate = blnNewUnit = true;
} else if (lstMonths.indexOf(strLower.substring(0,3)) !== -1) {
} else if (strLower.substring(0,3) in dctMonths) {
strUnit = strLower.substring(0,3);
blnDate = blnNewUnit = true;
} else if (lstTime.indexOf(strLower) !== -1) {
strUnit = strTkn[0];
blnDate = blnNewUnit = true;
blnNewUnit = true;
} else if (lstAnchors.indexOf(strAbbrev) !== -1) {
blnDate = true;
if (strAbbrev !== 'now') {
Expand Down Expand Up @@ -229,10 +275,17 @@ define(function(require, exports, module) {
}
} else if (strTkn == ':' || strTkn == '.') {
blnPostColon = true; upDate('H'); strUnit = 'M';
rQuant = 0;
blnNewQuant = false; blnNewUnit = true;
} else if (lstShift.indexOf(strTkn) != -1) {
blnNextLast = blnNewQuant = true; rQuant = 1;
if (strTkn !== 'next') {lngSign = -1;}
} else if (strLower in dctShift) {
if (dctShift[strLower]) { // unles 'ago'
blnNextLast = blnNewQuant = true; rQuant = 1;
if (strTkn !== 'next') {lngSign = -1;}
} else if (rDelta > 0) { // 'ago: reflect around now'
rDelta = -rDelta;
dteAnchor = new Date();
if (strUnit !== 'h') {dteAnchor.setHours(0,0,0,0);}
}
} else if (lstAmPm.indexOf(strLower) !== -1) {
strUnit = strTkn[0];
blnNewUnit = blnNewQuant = true;
Expand Down Expand Up @@ -309,7 +362,8 @@ define(function(require, exports, module) {

// FUNCTION(S) TO EXPORT
exports.datePhraseToISO = datePhraseToISO; //Phrase to ISO string
//exports.phraseToDate = phraseToDate; // Phrase to JS Date()
//exports.fmtTP = fmtTP; // JS Date() to ISO string
exports.phraseToDate = phraseToDate; // Phrase to JS Date()
exports.fmtTP = fmtTP; // JS Date() to ISO string


});
Binary file not shown.
Binary file modified TaskPaper scripts/TaskPaperViews-005.scpt
Binary file not shown.

0 comments on commit 485f3f4

Please sign in to comment.