Commit f9e6b3e6 authored by Joao Oliveira's avatar Joao Oliveira Committed by Albert Astals Cid

Extended util.printd support to cover new formats

parent 7c361d05
Pipeline #6374 passed with stage
in 13 minutes and 32 seconds
...@@ -313,7 +313,7 @@ void KJSFunctionsTest::testAlert() ...@@ -313,7 +313,7 @@ void KJSFunctionsTest::testAlert()
void KJSFunctionsTest::testPrintD() void KJSFunctionsTest::testPrintD()
{ {
Okular::ScriptAction *action = new Okular::ScriptAction( Okular::JavaScript, Okular::ScriptAction *action = new Okular::ScriptAction( Okular::JavaScript,
QStringLiteral( "var date = new Date( 2010, 0, 1, 11, 10, 32, 1 );\ QStringLiteral( "var date = new Date( 2010, 0, 5, 11, 10, 32, 1 );\
ret = app.alert( util.printd( \"mm\\\\yyyy\", date ) );" ) ); ret = app.alert( util.printd( \"mm\\\\yyyy\", date ) );" ) );
QScopedPointer< MessageBoxHelper > messageBoxHelper; QScopedPointer< MessageBoxHelper > messageBoxHelper;
messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "01\\2010" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "01\\2010" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) );
...@@ -326,12 +326,12 @@ void KJSFunctionsTest::testPrintD() ...@@ -326,12 +326,12 @@ void KJSFunctionsTest::testPrintD()
delete action; delete action;
action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( \"dd\\\\mm HH:MM\", date ) );" ) ); action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( \"dd\\\\mm HH:MM\", date ) );" ) );
messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "01\\01 11:10" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "05\\01 11:10" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) );
m_document->processAction( action ); m_document->processAction( action );
delete action; delete action;
action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( \"dd\\\\mm HH:MM:ss\", date ) );" ) ); action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( \"dd\\\\mm HH:MM:ss\", date ) );" ) );
messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "01\\01 11:10:32" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "05\\01 11:10:32" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) );
m_document->processAction( action ); m_document->processAction( action );
delete action; delete action;
...@@ -339,6 +339,24 @@ void KJSFunctionsTest::testPrintD() ...@@ -339,6 +339,24 @@ void KJSFunctionsTest::testPrintD()
messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "2010\\01 11:10:32" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "2010\\01 11:10:32" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) );
m_document->processAction( action ); m_document->processAction( action );
delete action; delete action;
action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( 0, date ) );" ) );
messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "D:20100105111032" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) );
m_document->processAction( action );
delete action;
action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( 1, date ) );" ) );
messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "2010.01.05 11:10:32" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) );
m_document->processAction( action );
delete action;
action = new Okular::ScriptAction( Okular::JavaScript, QStringLiteral( "ret = app.alert( util.printd( 2, date ) );" ) );
QLocale locale = QLocale::system();
QDate date( 2010, 1, 5 );
messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, date.toString( locale.dateFormat( QLocale::ShortFormat ) ) + QStringLiteral( " 11:10:32" ), QMessageBox::Critical,
QStringLiteral( "Okular" ), false ) );
m_document->processAction( action );
delete action;
} }
void KJSFunctionsTest::cleanupTestCase() void KJSFunctionsTest::cleanupTestCase()
......
...@@ -67,24 +67,46 @@ static KJSObject printd( KJSContext *context, void *, ...@@ -67,24 +67,46 @@ static KJSObject printd( KJSContext *context, void *,
{ {
return context->throwException( QStringLiteral("Invalid arguments") ); return context->throwException( QStringLiteral("Invalid arguments") );
} }
KJSObject oFormat = arguments.at( 0 );
QString format;
if( oFormat.isNumber() )
{
int formatType = oFormat.toInt32( context );
switch( formatType )
{
case 0:
format = QStringLiteral( "D:yyyyMMddHHmmss" );
break;
case 1:
format = QStringLiteral( "yyyy.MM.dd HH:mm:ss");
break;
case 2:
QLocale system = QLocale::system();
format = system.dateTimeFormat( QLocale::ShortFormat ) + QStringLiteral( ":ss" );
break;
}
}
else
{
format = arguments.at( 0 ).toString( context ).replace( "tt", "ap" );
format.replace( "t", "a" );
for( int i = 0 ; i < format.size() ; ++i )
{
if( format[i] == 'M' )
format[i] = 'm';
else if( format[i] == 'm' )
format[i] = 'M';
}
}
QLocale locale( "en_US" ); QLocale locale( "en_US" );
QString format = arguments.at( 0 ).toString( context ).replace( "tt", "ap" );
format.replace( "t", "a" );
QStringList str = arguments.at( 1 ).toString( context ).split( QRegularExpression( "\\W") ); QStringList str = arguments.at( 1 ).toString( context ).split( QRegularExpression( "\\W") );
QString myStr = QStringLiteral( "%1/%2/%3 %4:%5:%6" ).arg( str[1] ). QString myStr = QStringLiteral( "%1/%2/%3 %4:%5:%6" ).arg( str[1] ).
arg( str[2] ).arg( str[3] ).arg( str[4] ).arg( str[5] ).arg( str[6] ); arg( str[2] ).arg( str[3] ).arg( str[4] ).arg( str[5] ).arg( str[6] );
QDateTime date = locale.toDateTime( myStr, QStringLiteral( "MMM/d/yyyy H:m:s" ) ); QDateTime date = locale.toDateTime( myStr, QStringLiteral( "MMM/d/yyyy H:m:s" ) );
for( int i = 0 ; i < format.size() ; ++i )
{
if( format[i] == 'M' )
format[i] = 'm';
else if( format[i] == 'm' )
format[i] = 'M';
}
return KJSString( date.toString( format ) ); return KJSString( date.toString( format ) );
} }
......
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