Commit cbe372a5 authored by Sergey.Konovalov's avatar Sergey.Konovalov

переделано распознавание даты из текста.

git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/OfficeWeb@52011 954022d7-b5bf-4e40-9824-e11837661b57
parent b680db55
...@@ -2514,8 +2514,10 @@ function FormatParser() ...@@ -2514,8 +2514,10 @@ function FormatParser()
this.rx_date = new RegExp("^ *(((\\d{1,2}) *[/-])? *(\\d{1,2}|[A-Za-z]{3,9}) *([/-]|[/-] *(\\d{2,4}))?)? *((\\d{1,2}) *: *(\\d{1,2})? *(: *(\\d{1,2})?)? *(AM|PM)?)?$"); this.rx_date = new RegExp("^ *(((\\d{1,2}) *[/-])? *(\\d{1,2}|[A-Za-z]{3,9}) *([/-]|[/-] *(\\d{2,4}))?)? *((\\d{1,2}) *: *(\\d{1,2})? *(: *(\\d{1,2})?)? *(AM|PM)?)?$");
this.rx_currency = new RegExp("^([+-]?)([$€£¥])?(\\d+\\.?\\d*|\\.\\d+)(р.)?$"); this.rx_currency = new RegExp("^([+-]?)([$€£¥])?(\\d+\\.?\\d*|\\.\\d+)(р.)?$");
this.rx_date = new RegExp("[A-Za-z]{2,9}|\\d{1,4}|[\\/\\.\\-:,]| +", "g");
this.days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; this.days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
this.daysLeap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; this.daysLeap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
this.bFormatMonthFirst = true;
}; };
FormatParser.prototype = FormatParser.prototype =
{ {
...@@ -2559,70 +2561,221 @@ FormatParser.prototype = ...@@ -2559,70 +2561,221 @@ FormatParser.prototype =
} }
return res; return res;
}, },
parseDate : function(value) _parseDateFromArray : function(match)
{ {
var res = null; var res = null;
var match = null; var bError = false;
if(null != (match = value.match(this.rx_date)) ) var sPrevDelimiter = null;//разделители не могут идти 2 раза подряд(кроме пробелов)
{ var sTextMonth = null;//текстовое название месяца может встречаться только 1 раз
var d = match[3]; var sMonthFormat = "m";
var m = match[4]; var nMonthLetterIndex = -1;//текстовое название месяца может встречаться только на первой или второй позиции
var y = match[6]; var nTimeIndex = -1;
var h = match[8]; var sAmPm = null;
var min = match[9]; var nAmPmIndex = -1;//AmPm может встречать только 1 раз в конце
var s = match[11]; var aData = [];
var ampm = match[12]; for(var i = 0, length = match.length; i < length; i++)
var bMonthFirst = true; {
var bMonthLetter = false; var elem = match[i];
var sMonthDateFormat = ""; if(elem.length > 0)
if(null != m) {
{ var sFirst = elem[0];
//меняем текст месяца на число if(("A" <= sFirst && sFirst <= "Z") || ("a" <= sFirst && sFirst <= "z"))
if(m - 0 != m) {
if(-1 == nAmPmIndex && ("AM" == elem || "PM" == elem))
{
sAmPm = elem;
nAmPmIndex = aData.length;
}
else if(null == sTextMonth)
{ {
sTextMonth = elem;
var aArraysToCheck = [{arr: monthCut, format: "mmm"}, {arr: month, format: "mmmm"}]; var aArraysToCheck = [{arr: monthCut, format: "mmm"}, {arr: month, format: "mmmm"}];
for(var i in aArraysToCheck) var bFound = false;
for(var index in aArraysToCheck)
{ {
var aArrayTemp = aArraysToCheck[i]; var aArrayTemp = aArraysToCheck[index];
for(var j = 0, length = aArrayTemp.arr.length; j < length; j++) for(var j = 0, length2 = aArrayTemp.arr.length; j < length2; j++)
{ {
if(aArrayTemp.arr[j].toLowerCase() == m.toLowerCase()) if(aArrayTemp.arr[j].toLowerCase() == elem.toLowerCase())
{ {
m = j + 1; bFound = true;
bMonthLetter = true; aData.push(j + 1);
var sMonthFormat = aArrayTemp.format; nMonthLetterIndex = aData.length - 1;
if(null != d) sMonthFormat = aArrayTemp.format;
break;
}
}
if(bFound)
break;
}
if(true != bFound)
{ {
if (null != y){ bError = true;
if(y < 100) break;
sMonthDateFormat = "d/" + sMonthFormat +"/yy"; }
else
sMonthDateFormat = "d/" + sMonthFormat +"/yyyy";
} }
else else
sMonthDateFormat = "d/" + sMonthFormat; {
bError = true;
break;
}
sPrevDelimiter = null;
} }
else if(null != y) else if("0" <= sFirst && sFirst <= "9")
{ {
if(y < 100) aData.push(elem - 0);
sMonthDateFormat = sMonthFormat +"/yy"; sPrevDelimiter = null;
}
else else
sMonthDateFormat = sMonthFormat +"/yyyy"; {
if(" " != sFirst)
{
if(null == sPrevDelimiter && aData.length > 0)
{
if(":" == sFirst && -1 == nTimeIndex)
nTimeIndex = aData.length - 1;
sPrevDelimiter = sFirst;
} }
else
{
bError = true;
break; break;
} }
} }
if(bMonthLetter)
break;
} }
} }
if(bMonthFirst && false == bMonthLetter && null != d && null != m) }
if(true != bError && (-1 == nMonthLetterIndex || nMonthLetterIndex <= 1) && aData.length <= 6 && (null == sAmPm || nAmPmIndex == aData.length))
{
res = {d: null, m: null, y: null, h: null, min: null, s: null, ampm: sAmPm, sDateFormat: null};
var aTime = null;
if(-1 != nTimeIndex)
{ {
var temp = d; aTime = aData.splice(nTimeIndex);
d = m; var nTimeLength = aTime.length;
m = temp; if(nTimeLength > 0 && nTimeLength <= 3)
{
var aTimeRes = [null, null, null];
for(var i = 0, length = aTime.length; i < length; i++)
aTimeRes[i] = aTime[i];
res.h = aTimeRes[0];
res.min = aTimeRes[1];
res.s = aTimeRes[2];
}
else
bError = true;
} }
var nDataLength = aData.length;
if(2 == nDataLength || nDataLength == 3)
{
var aDataRes = [null, null, null];
for(var i = 0, length = aData.length; i < length; i++)
aDataRes[i] = aData[i];
res.d = aDataRes[0];
res.m = aDataRes[1];
res.y = aDataRes[2];
//если пришло только 2 параметра даты, то решаем что есть что.
if(2 == nDataLength)
{
if(-1 != nMonthLetterIndex)
{
if(0 == nMonthLetterIndex)
{
var temp = res.m;
res.m = res.d;
res.d = temp;
} }
//приоритет у формата d-mmm, но если он не подходит пробуем сделать mmm-yy
if(this.isValidDay((new Date()).getFullYear(), res.m - 1, res.d))
res.sDateFormat = "d-mmm";
else
{
//если текстовый месяц стоит первым, то второй параметр может быть как годом так и днем, если текстовый месяц стоит первым
if(0 == nMonthLetterIndex)
{
res.sDateFormat = "mmm-yy";
res.y = res.d;
res.d = null;
}
else
bError = true;
}
}
else
{
if(this.bFormatMonthFirst)
{
var temp = res.d;
res.d = res.m;
res.m = temp;
}
if(this.isValidDay((new Date()).getFullYear(), res.m - 1, res.d))
res.sDateFormat = "d-mmm";
else
{
res.sDateFormat = "mmm-yy";
res.y = res.d;
res.d = null;
}
}
}
else
{
if(-1 != nMonthLetterIndex)
{
res.sDateFormat = "d-mmm-yy";
if(0 == nMonthLetterIndex)
{
var temp = res.d;
res.d = res.m;
res.m = temp;
}
}
else
{
res.sDateFormat = "d/m/yyyy";
if(this.bFormatMonthFirst)
{
var temp = res.d;
res.d = res.m;
res.m = temp;
}
}
}
if(null == res.sDateFormat)
res.sDateFormat = "d/m/yyyy";
if(null != res.y)
{
if(res.y < 30)
res.y = 2000 + res.y;
else if(res.y < 100)
res.y = 1900 + res.y;
}
}
else if(nDataLength > 0)
bError = true;
if(bError)
res = null;
}
return res;
},
parseDate : function(value)
{
var res = null;
var match = value.match(this.rx_date);
if(null != match)
{
var oParsedDate = this._parseDateFromArray(match);
if(null != oParsedDate)
{
var d = oParsedDate.d;
var m = oParsedDate.m;
var y = oParsedDate.y;
var h = oParsedDate.h;
var min = oParsedDate.min;
var s = oParsedDate.s;
var ampm = oParsedDate.ampm;
var sDateFormat = oParsedDate.sDateFormat;
var bDate = false; var bDate = false;
var bTime = false; var bTime = false;
...@@ -2660,8 +2813,11 @@ FormatParser.prototype = ...@@ -2660,8 +2813,11 @@ FormatParser.prototype =
else else
nYear = oNowDate.getFullYear(); nYear = oNowDate.getFullYear();
if(nYear < 100) if(nYear < 30)
nYear = 2000 + nYear; nYear = 2000 + res.y;
else if(nYear < 100)
nYear = 1900 + res.y;
//проверяем дату на валидность //проверяем дату на валидность
bValidDate = this.isValidDate(nYear, nMounth, nDay); bValidDate = this.isValidDate(nYear, nMounth, nDay);
} }
...@@ -2670,16 +2826,29 @@ FormatParser.prototype = ...@@ -2670,16 +2826,29 @@ FormatParser.prototype =
bTime = true; bTime = true;
var nHour = h - 0; var nHour = h - 0;
if(null != ampm) if(null != ampm)
{
if(nHour <= 23)
{ {
//переводим 24 //переводим 24
nHour = nHour % 12; nHour = nHour % 12;
if("PM" == ampm) if("PM" == ampm)
nHour += 12; nHour += 12;
} }
else
bValidDate = false;
}
if(null != min) if(null != min)
{
nMinute = min - 0; nMinute = min - 0;
if(nMinute > 59)
bValidDate = false;
}
if(null != s) if(null != s)
{
nSecond = s - 0; nSecond = s - 0;
if(nSecond > 59)
bValidDate = false;
}
} }
if(true == bValidDate && (true == bDate || true == bTime)) if(true == bValidDate && (true == bDate || true == bTime))
{ {
...@@ -2699,18 +2868,12 @@ FormatParser.prototype = ...@@ -2699,18 +2868,12 @@ FormatParser.prototype =
var sFormat; var sFormat;
if(true == bDate && true == bTime) if(true == bDate && true == bTime)
{ {
if(bMonthLetter) sFormat = sDateFormat + " h:mm:ss";
sFormat = sMonthDateFormat + " h:mm:ss AM/PM"; if(null != ampm)
else sFormat += " AM/PM";
sFormat = "m/d/yyyy h:mm:ss AM/PM";
} }
else if(true == bDate) else if(true == bDate)
{ sFormat = sDateFormat;
if(bMonthLetter)
sFormat = sMonthDateFormat;
else
sFormat = "m/d/yyyy";
}
else else
{ {
if(dValue > 1) if(dValue > 1)
...@@ -2723,6 +2886,7 @@ FormatParser.prototype = ...@@ -2723,6 +2886,7 @@ FormatParser.prototype =
res = {format: sFormat, value: dValue, bDateTime: true}; res = {format: sFormat, value: dValue, bDateTime: true};
} }
} }
}
} }
return res; return res;
}, },
...@@ -2735,7 +2899,11 @@ FormatParser.prototype = ...@@ -2735,7 +2899,11 @@ FormatParser.prototype =
if(nMounth < 0 || nMounth > 11) if(nMounth < 0 || nMounth > 11)
return false; return false;
else else
{ return this.isValidDay(nYear, nMounth, nDay);
}
return true;
},
isValidDay : function(nYear, nMounth, nDay){
if(this.isLeapYear(nYear)) if(this.isLeapYear(nYear))
{ {
if(nDay <= 0 || nDay > this.daysLeap[nMounth]) if(nDay <= 0 || nDay > this.daysLeap[nMounth])
...@@ -2746,8 +2914,6 @@ FormatParser.prototype = ...@@ -2746,8 +2914,6 @@ FormatParser.prototype =
if(nDay <= 0 || nDay > this.days[nMounth]) if(nDay <= 0 || nDay > this.days[nMounth])
return false; return false;
} }
}
}
return true; return true;
}, },
isLeapYear : function(year) isLeapYear : function(year)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment