Commit d65d9f7f authored by Sergey.Konovalov's avatar Sergey.Konovalov Committed by Alexander.Trofimov

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

git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/OfficeWeb@52011 954022d7-b5bf-4e40-9824-e11837661b57
parent 461e69c3
......@@ -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_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.daysLeap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
this.bFormatMonthFirst = true;
};
FormatParser.prototype =
{
......@@ -2559,70 +2561,221 @@ FormatParser.prototype =
}
return res;
},
parseDate : function(value)
_parseDateFromArray : function(match)
{
var res = null;
var match = null;
if(null != (match = value.match(this.rx_date)) )
{
var d = match[3];
var m = match[4];
var y = match[6];
var h = match[8];
var min = match[9];
var s = match[11];
var ampm = match[12];
var bMonthFirst = true;
var bMonthLetter = false;
var sMonthDateFormat = "";
if(null != m)
{
//меняем текст месяца на число
if(m - 0 != m)
var bError = false;
var sPrevDelimiter = null;//разделители не могут идти 2 раза подряд(кроме пробелов)
var sTextMonth = null;//текстовое название месяца может встречаться только 1 раз
var sMonthFormat = "m";
var nMonthLetterIndex = -1;//текстовое название месяца может встречаться только на первой или второй позиции
var nTimeIndex = -1;
var sAmPm = null;
var nAmPmIndex = -1;//AmPm может встречать только 1 раз в конце
var aData = [];
for(var i = 0, length = match.length; i < length; i++)
{
var elem = match[i];
if(elem.length > 0)
{
var sFirst = elem[0];
if(("A" <= sFirst && sFirst <= "Z") || ("a" <= sFirst && sFirst <= "z"))
{
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"}];
for(var i in aArraysToCheck)
var bFound = false;
for(var index in aArraysToCheck)
{
var aArrayTemp = aArraysToCheck[i];
for(var j = 0, length = aArrayTemp.arr.length; j < length; j++)
var aArrayTemp = aArraysToCheck[index];
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;
bMonthLetter = true;
var sMonthFormat = aArrayTemp.format;
if(null != d)
bFound = true;
aData.push(j + 1);
nMonthLetterIndex = aData.length - 1;
sMonthFormat = aArrayTemp.format;
break;
}
}
if(bFound)
break;
}
if(true != bFound)
{
if (null != y){
if(y < 100)
sMonthDateFormat = "d/" + sMonthFormat +"/yy";
else
sMonthDateFormat = "d/" + sMonthFormat +"/yyyy";
bError = true;
break;
}
}
else
sMonthDateFormat = "d/" + sMonthFormat;
{
bError = true;
break;
}
sPrevDelimiter = null;
}
else if(null != y)
else if("0" <= sFirst && sFirst <= "9")
{
if(y < 100)
sMonthDateFormat = sMonthFormat +"/yy";
aData.push(elem - 0);
sPrevDelimiter = null;
}
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;
}
}
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;
d = m;
m = temp;
aTime = aData.splice(nTimeIndex);
var nTimeLength = aTime.length;
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 bTime = false;
......@@ -2660,8 +2813,11 @@ FormatParser.prototype =
else
nYear = oNowDate.getFullYear();
if(nYear < 100)
nYear = 2000 + nYear;
if(nYear < 30)
nYear = 2000 + res.y;
else if(nYear < 100)
nYear = 1900 + res.y;
//проверяем дату на валидность
bValidDate = this.isValidDate(nYear, nMounth, nDay);
}
......@@ -2670,16 +2826,29 @@ FormatParser.prototype =
bTime = true;
var nHour = h - 0;
if(null != ampm)
{
if(nHour <= 23)
{
//переводим 24
nHour = nHour % 12;
if("PM" == ampm)
nHour += 12;
}
else
bValidDate = false;
}
if(null != min)
{
nMinute = min - 0;
if(nMinute > 59)
bValidDate = false;
}
if(null != s)
{
nSecond = s - 0;
if(nSecond > 59)
bValidDate = false;
}
}
if(true == bValidDate && (true == bDate || true == bTime))
{
......@@ -2699,18 +2868,12 @@ FormatParser.prototype =
var sFormat;
if(true == bDate && true == bTime)
{
if(bMonthLetter)
sFormat = sMonthDateFormat + " h:mm:ss AM/PM";
else
sFormat = "m/d/yyyy h:mm:ss AM/PM";
sFormat = sDateFormat + " h:mm:ss";
if(null != ampm)
sFormat += " AM/PM";
}
else if(true == bDate)
{
if(bMonthLetter)
sFormat = sMonthDateFormat;
else
sFormat = "m/d/yyyy";
}
sFormat = sDateFormat;
else
{
if(dValue > 1)
......@@ -2723,6 +2886,7 @@ FormatParser.prototype =
res = {format: sFormat, value: dValue, bDateTime: true};
}
}
}
}
return res;
},
......@@ -2735,7 +2899,11 @@ FormatParser.prototype =
if(nMounth < 0 || nMounth > 11)
return false;
else
{
return this.isValidDay(nYear, nMounth, nDay);
}
return true;
},
isValidDay : function(nYear, nMounth, nDay){
if(this.isLeapYear(nYear))
{
if(nDay <= 0 || nDay > this.daysLeap[nMounth])
......@@ -2746,8 +2914,6 @@ FormatParser.prototype =
if(nDay <= 0 || nDay > this.days[nMounth])
return false;
}
}
}
return true;
},
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