summaryrefslogtreecommitdiffstats
path: root/lib/kotext/IsoDuration.h
blob: 6ccb2891320f5e7d02c448ec84abac3b4bf47538 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* This file is part of the KDE project
   Copyright (C) 2005 David Faure <faure@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqstring.h>

/**
 * Routines for converting to and from the ISO-8601 duration format
 * http://www.w3.org/TR/xmlschema-2/datatypes.html#duration
 *
 * This is not the ISO time format supported by TQt (HH:MM:SS),
 * it's PT1H15M12S for 1:15:12.
 *
 */

static TQString minutesToISODuration( int mn )
{
    bool neg = mn < 0;
    // PT == period of time - see ISO8601
    TQString str = TQString::tqfromLatin1( "PT00H%1M00S" ).tqarg( TQABS( mn ) );
    if ( neg )
        str.prepend( '-' );
    return str;
}

static TQString daysToISODuration( int days )
{
    bool neg = days < 0;
    // P == period, time is ommitted - see ISO8601
    TQString str = TQString::tqfromLatin1( "P%1D" ).tqarg( TQABS( days ) );
    if ( neg )
        str.prepend( '-' );
    return str;
}

static int ISODurationToMinutes( const TQString& str )
{
    int idx = 0;
    const int len = str.length();
    bool neg = str[idx] == '-';
    if ( neg )
        ++idx;
    if ( idx < len && str[idx] == 'P' ) // should always be true
        ++idx;
    if ( idx < len && str[idx] == 'T' )
        ++idx;
    int minutes = 0;
    int currentNum = 0;
    while ( idx < len ) {
        if ( str[idx].isDigit() )
            currentNum = currentNum * 10 + str[idx].latin1() - '0';
        else {
            if ( str[idx] == 'D' )
                minutes += 24 * 60 * currentNum;
            else if ( str[idx] == 'H' )
                minutes += 60 * currentNum;
            else if ( str[idx] == 'M' )
                minutes += currentNum;
            currentNum = 0;
        }
        ++idx;
    }
    return neg ? -minutes : minutes;
}

static int ISODurationToDays( const TQString& str )
{
    int idx = 0;
    const int len = str.length();
    bool neg = str[idx] == '-';
    if ( neg )
        ++idx;
    if ( idx < len && str[idx] == 'P' ) // should always be true
        ++idx;
    if ( idx < len && str[idx] == 'T' )
        ++idx;
    int days = 0;
    int currentNum = 0;
    while ( idx < len ) {
        if ( str[idx].isDigit() )
            currentNum = currentNum * 10 + str[idx].latin1() - '0';
        else {
            if ( str[idx] == 'Y' )
                days += 365 * currentNum;
            else if ( str[idx] == 'M' )
                days += 30 * currentNum; // approx
            else if ( str[idx] == 'D' )
                days += currentNum;
            currentNum = 0;
        }
        ++idx;
    }
    return neg ? -days : days;
}