F:/KPlato/koffice/libs/kotext/IsoDuration.h

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  * Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include <QString>
00020 
00030 static QString minutesToISODuration( int mn )
00031 {
00032     bool neg = mn < 0;
00033     // PT == period of time - see ISO8601
00034     QString str = QString::fromLatin1( "PT00H%1M00S" ).arg( QABS( mn ) );
00035     if ( neg )
00036         str.prepend( '-' );
00037     return str;
00038 }
00039 
00040 static QString daysToISODuration( int days )
00041 {
00042     bool neg = days < 0;
00043     // P == period, time is ommitted - see ISO8601
00044     QString str = QString::fromLatin1( "P%1D" ).arg( QABS( days ) );
00045     if ( neg )
00046         str.prepend( '-' );
00047     return str;
00048 }
00049 
00050 static int ISODurationToMinutes( const QString& str )
00051 {
00052     int idx = 0;
00053     const int len = str.length();
00054     bool neg = str[idx] == '-';
00055     if ( neg )
00056         ++idx;
00057     if ( idx < len && str[idx] == 'P' ) // should always be true
00058         ++idx;
00059     if ( idx < len && str[idx] == 'T' )
00060         ++idx;
00061     int minutes = 0;
00062     int currentNum = 0;
00063     while ( idx < len ) {
00064         if ( str[idx].isDigit() )
00065             currentNum = currentNum * 10 + str[idx].toLatin1() - '0';
00066         else {
00067             if ( str[idx] == 'D' )
00068                 minutes += 24 * 60 * currentNum;
00069             else if ( str[idx] == 'H' )
00070                 minutes += 60 * currentNum;
00071             else if ( str[idx] == 'M' )
00072                 minutes += currentNum;
00073             currentNum = 0;
00074         }
00075         ++idx;
00076     }
00077     return neg ? -minutes : minutes;
00078 }
00079 
00080 static int ISODurationToDays( const QString& str )
00081 {
00082     int idx = 0;
00083     const int len = str.length();
00084     bool neg = str[idx] == '-';
00085     if ( neg )
00086         ++idx;
00087     if ( idx < len && str[idx] == 'P' ) // should always be true
00088         ++idx;
00089     if ( idx < len && str[idx] == 'T' )
00090         ++idx;
00091     int days = 0;
00092     int currentNum = 0;
00093     while ( idx < len ) {
00094         if ( str[idx].isDigit() )
00095             currentNum = currentNum * 10 + str[idx].toLatin1() - '0';
00096         else {
00097             if ( str[idx] == 'Y' )
00098                 days += 365 * currentNum;
00099             else if ( str[idx] == 'M' )
00100                 days += 30 * currentNum; // approx
00101             else if ( str[idx] == 'D' )
00102                 days += currentNum;
00103             currentNum = 0;
00104         }
00105         ++idx;
00106     }
00107     return neg ? -days : days;
00108 }
00109 

Généré le Wed Nov 22 23:41:07 2006 pour KPlato par  doxygen 1.5.1-p1