@@ -51,7 +51,7 @@ QgsExpression::Interval QgsExpression::Interval::invalidInterVal()
51
51
QgsExpression::Interval QgsExpression::Interval::fromString ( QString string )
52
52
{
53
53
int seconds = 0 ;
54
- QRegExp rx ( " (\\ d?\\ .?\\ d+\\ s+[a-z] +)" , Qt::CaseInsensitive );
54
+ QRegExp rx ( " ([-+]? \\ d?\\ .?\\ d+\\ s+\\ S +)" , Qt::CaseInsensitive );
55
55
QStringList list;
56
56
int pos = 0 ;
57
57
@@ -61,44 +61,43 @@ QgsExpression::Interval QgsExpression::Interval::fromString( QString string )
61
61
pos += rx.matchedLength ();
62
62
}
63
63
64
+ QMap<int , QStringList> map;
65
+ map.insert ( 1 , QStringList () << " second" << " seconds" << QObject::tr ( " second|seconds" , " list of words separated by | which reference years" ).split ( " |" ) );
66
+ map.insert ( 0 + MINUTE, QStringList () << " minute" << " minutes" << QObject::tr ( " minute|minutes" , " list of words separated by | which reference minutes" ).split ( " |" ) );
67
+ map.insert ( 0 + HOUR, QStringList () << " hour" << " hours" << QObject::tr ( " hour|hours" , " list of words separated by | which reference minutes hours" ).split ( " |" ) );
68
+ map.insert ( 0 + DAY, QStringList () << " day" << " days" << QObject::tr ( " day|days" , " list of words separated by | which reference days" ).split ( " |" ) );
69
+ map.insert ( 0 + WEEKS, QStringList () << " week" << " weeks" << QObject::tr ( " week|weeks" , " wordlist separated by | which reference weeks" ).split ( " |" ) );
70
+ map.insert ( 0 + MONTHS, QStringList () << " month" << " months" << QObject::tr ( " month|months" , " list of words separated by | which reference months" ).split ( " |" ) );
71
+ map.insert ( 0 + YEARS, QStringList () << " year" << " years" << QObject::tr ( " year|years" , " list of words separated by | which reference years" ).split ( " |" ) );
72
+
64
73
foreach ( QString match, list )
65
74
{
66
75
QStringList split = match.split ( QRegExp ( " \\ s+" ) );
67
76
bool ok;
68
- int value = split.at ( 0 ).toInt ( &ok );
77
+ double value = split.at ( 0 ).toDouble ( &ok );
69
78
if ( !ok )
70
79
{
71
80
continue ;
72
81
}
73
82
74
- if ( match.contains ( " day" , Qt::CaseInsensitive ) ||
75
- match.contains ( QObject::tr ( " day" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
76
- match.contains ( QObject::tr ( " days" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
77
- seconds += value * QgsExpression::Interval::DAY;
78
- if ( match.contains ( " week" , Qt::CaseInsensitive ) ||
79
- match.contains ( QObject::tr ( " week" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
80
- match.contains ( QObject::tr ( " weeks" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
81
- seconds += value * QgsExpression::Interval::WEEKS;
82
- if ( match.contains ( " month" , Qt::CaseInsensitive ) ||
83
- match.contains ( QObject::tr ( " month" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
84
- match.contains ( QObject::tr ( " months" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
85
- seconds += value * QgsExpression::Interval::MONTHS;
86
- if ( match.contains ( " year" , Qt::CaseInsensitive ) ||
87
- match.contains ( QObject::tr ( " year" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
88
- match.contains ( QObject::tr ( " years" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
89
- seconds += value * QgsExpression::Interval::YEARS;
90
- if ( match.contains ( " second" , Qt::CaseInsensitive ) ||
91
- match.contains ( QObject::tr ( " second" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
92
- match.contains ( QObject::tr ( " seconds" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
93
- seconds += value;
94
- if ( match.contains ( " minute" , Qt::CaseInsensitive ) ||
95
- match.contains ( QObject::tr ( " minute" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
96
- match.contains ( QObject::tr ( " minutes" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
97
- seconds += value * QgsExpression::Interval::MINUTE;
98
- if ( match.contains ( " hour" , Qt::CaseInsensitive ) ||
99
- match.contains ( QObject::tr ( " hour" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
100
- match.contains ( QObject::tr ( " hours" , " Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
101
- seconds += value * QgsExpression::Interval::HOUR;
83
+ bool matched = false ;
84
+ foreach ( int duration, map.keys () )
85
+ {
86
+ foreach ( QString name, map[duration] )
87
+ {
88
+ if ( match.contains ( name, Qt::CaseInsensitive ) )
89
+ {
90
+ matched = true ;
91
+ break ;
92
+ }
93
+ }
94
+
95
+ if ( matched )
96
+ {
97
+ seconds += value * duration;
98
+ break ;
99
+ }
100
+ }
102
101
}
103
102
104
103
// If we can't parse the string at all then we just return invalid
0 commit comments