00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <QString>
00020
00030 static QString minutesToISODuration( int mn )
00031 {
00032 bool neg = mn < 0;
00033
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
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' )
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' )
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;
00101 else if ( str[idx] == 'D' )
00102 days += currentNum;
00103 currentNum = 0;
00104 }
00105 ++idx;
00106 }
00107 return neg ? -days : days;
00108 }
00109