From f9e6b3e6e694113eb911a016325c82a0725ca6ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Netto?= Date: Thu, 8 Aug 2019 12:02:28 -0300 Subject: [PATCH] Extended util.printd support to cover new formats --- autotests/kjsfunctionstest.cpp | 24 +++++++++++++++--- core/script/kjs_util.cpp | 46 +++++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/autotests/kjsfunctionstest.cpp b/autotests/kjsfunctionstest.cpp index 4ad76cff4..759fdd58f 100644 --- a/autotests/kjsfunctionstest.cpp +++ b/autotests/kjsfunctionstest.cpp @@ -313,7 +313,7 @@ void KJSFunctionsTest::testAlert() void KJSFunctionsTest::testPrintD() { 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 ) );" ) ); QScopedPointer< MessageBoxHelper > messageBoxHelper; messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "01\\2010" ), QMessageBox::Critical, QStringLiteral( "Okular" ), false ) ); @@ -326,12 +326,12 @@ void KJSFunctionsTest::testPrintD() delete action; 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 ); delete action; 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 ); delete action; @@ -339,6 +339,24 @@ void KJSFunctionsTest::testPrintD() messageBoxHelper.reset( new MessageBoxHelper( QMessageBox::Ok, QStringLiteral( "2010\\01 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( 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() diff --git a/core/script/kjs_util.cpp b/core/script/kjs_util.cpp index 65c7e80db..701a1dbc4 100644 --- a/core/script/kjs_util.cpp +++ b/core/script/kjs_util.cpp @@ -67,24 +67,46 @@ static KJSObject printd( KJSContext *context, void *, { 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" ); - QString format = arguments.at( 0 ).toString( context ).replace( "tt", "ap" ); - format.replace( "t", "a" ); - QStringList str = arguments.at( 1 ).toString( context ).split( QRegularExpression( "\\W") ); 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] ); 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 ) ); } -- 2.24.0