forked from open-eid/qt-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTime.cpp
51 lines (45 loc) · 1.6 KB
/
DateTime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* QEstEidCommon
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "DateTime.h"
#include <QtCore/QLocale>
DateTime::DateTime( const QDateTime &other )
: QDateTime( other )
{}
QString DateTime::formatDate( const QString &format ) const
{
int pos = format.indexOf( "MMMM" );
if( pos == -1 )
return toString( format );
QString d = toString( QString( format ).remove( pos, 4 ) );
return d.insert( pos, QLocale().monthName( date().month() ) );
}
int DateTime::timeDiff() const
{
if( !isValid() || timeSpec() == Qt::UTC ) return 0;
QDateTime utc = toUTC();
utc.setTimeSpec( Qt::LocalTime );
return utc.secsTo( toLocalTime() );
}
QString DateTime::toStringZ( const QString &format ) const
{
if( !isValid() ) return "";
int diffsec = timeDiff();
return toString( format ) + QString(" %1%2").arg( diffsec >= 0 ? "+" : "-" )
.arg( QTime( 0, 0 ).addSecs( diffsec >= 0 ? diffsec : -diffsec ).toString( "hh:mm" ) );
}