src/xmlpatterns/data/qduration.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qabstractdatetime_p.h"
       
    43 #include "qbuiltintypes_p.h"
       
    44 #include "qitem_p.h"
       
    45 
       
    46 #include "qduration_p.h"
       
    47 
       
    48 QT_BEGIN_NAMESPACE
       
    49 
       
    50 using namespace QPatternist;
       
    51 
       
    52 Duration::Duration(const bool isPositiveP,
       
    53                    const YearProperty yearsP,
       
    54                    const MonthProperty monthsP,
       
    55                    const DayCountProperty daysP,
       
    56                    const HourProperty hoursP,
       
    57                    const MinuteProperty mins,
       
    58                    const SecondProperty secs,
       
    59                    const MSecondProperty msecs) : AbstractDuration(isPositiveP),
       
    60                                                   m_years(yearsP),
       
    61                                                   m_months(monthsP),
       
    62                                                   m_days(daysP),
       
    63                                                   m_hours(hoursP),
       
    64                                                   m_minutes(mins),
       
    65                                                   m_seconds(secs),
       
    66                                                   m_mseconds(msecs)
       
    67 {
       
    68 }
       
    69 
       
    70 Duration::Ptr Duration::fromLexical(const QString &lexical)
       
    71 {
       
    72     static const CaptureTable captureTable(
       
    73         /* The extra paranthesis is a build fix for GCC 3.3. */
       
    74         (QRegExp(QLatin1String(
       
    75                 "^\\s*"                         /* Any preceding whitespace. */
       
    76                 "(-)?"                          /* Any minus sign. */
       
    77                 "P"                             /* Delimiter. */
       
    78                 "(?:(\\d+)Y)?"                  /* Year part. */
       
    79                 "(?:(\\d+)M)?"                  /* Month part. */
       
    80                 "(?:(\\d+)D)?"                  /* Day part. */
       
    81                 "(?:"                           /* Here starts the optional time part. */
       
    82                 "(T)"                           /* SchemaTime delimiter. */
       
    83                 "(?:(\\d+)H)?"                  /* Hour part. */
       
    84                 "(?:(\\d+)M)?"                  /* Minute part. */
       
    85                 "(?:(\\d+)(?:\\.(\\d+))?S)?"    /* Seconds & milli seconds. */
       
    86                 ")?"                            /* End of optional time part. */
       
    87                 "\\s*$"                         /* Any terminating whitespace. */))),
       
    88         /*yearP*/       2,
       
    89         /*monthP*/      3,
       
    90         /*dayP*/        4,
       
    91         /*tDelimiterP*/ 5,
       
    92         /*hourP*/       6,
       
    93         /*minutesP*/    7,
       
    94         /*secondsP*/    8,
       
    95         /*msecondsP*/   9);
       
    96 
       
    97     YearProperty years = 0;
       
    98     MonthProperty months = 0;
       
    99     DayCountProperty days = 0;
       
   100     HourProperty hours = 0;
       
   101     MinuteProperty minutes = 0;
       
   102     SecondProperty sec = 0;
       
   103     MSecondProperty msec = 0;
       
   104     bool isPos;
       
   105 
       
   106     const AtomicValue::Ptr err(create(captureTable, lexical, &isPos, &years, &months,
       
   107                                       &days, &hours, &minutes, &sec, &msec));
       
   108 
       
   109     return err ? err : Duration::Ptr(new Duration(isPos, years, months, days, hours,
       
   110                                                   minutes, sec, msec));
       
   111 }
       
   112 
       
   113 Duration::Ptr Duration::fromComponents(const bool isPositive,
       
   114                                        const YearProperty years,
       
   115                                        const MonthProperty months,
       
   116                                        const DayCountProperty days,
       
   117                                        const HourProperty hours,
       
   118                                        const MinuteProperty minutes,
       
   119                                        const SecondProperty seconds,
       
   120                                        const MSecondProperty mseconds)
       
   121 {
       
   122     return Duration::Ptr(new Duration(isPositive, years, months, days,
       
   123                                       hours, minutes, seconds, mseconds));
       
   124 }
       
   125 
       
   126 AbstractDuration::Value Duration::value() const
       
   127 {
       
   128     Q_ASSERT_X(false, Q_FUNC_INFO,
       
   129                "Calling Duration::value() makes no sense");
       
   130     return 0;
       
   131 }
       
   132 
       
   133 Item Duration::fromValue(const Value) const
       
   134 {
       
   135     Q_ASSERT_X(false, Q_FUNC_INFO,
       
   136                "Calling Duration::fromValue() makes no sense");
       
   137     return Item();
       
   138 }
       
   139 
       
   140 QString Duration::stringValue() const
       
   141 {
       
   142     QString retval;
       
   143 
       
   144     if(!m_isPositive)
       
   145         retval.append(QLatin1Char('-'));
       
   146 
       
   147     retval.append(QLatin1Char('P'));
       
   148 
       
   149     if(m_years)
       
   150     {
       
   151         retval.append(QString::number(m_years));
       
   152         retval.append(QLatin1Char('Y'));
       
   153     }
       
   154 
       
   155     if(m_months)
       
   156     {
       
   157         retval.append(QString::number(m_months));
       
   158         retval.append(QLatin1Char('M'));
       
   159     }
       
   160 
       
   161     if(m_days)
       
   162     {
       
   163         retval.append(QString::number(m_days));
       
   164         retval.append(QLatin1Char('D'));
       
   165     }
       
   166 
       
   167     if(!m_hours && !m_minutes && !m_seconds && !m_seconds)
       
   168     {
       
   169         if(!m_years && !m_months && !m_days)
       
   170             return QLatin1String("PT0S");
       
   171         else
       
   172             return retval;
       
   173     }
       
   174 
       
   175     retval.append(QLatin1Char('T'));
       
   176 
       
   177     if(m_hours)
       
   178     {
       
   179         retval.append(QString::number(m_hours));
       
   180         retval.append(QLatin1Char('H'));
       
   181     }
       
   182 
       
   183     if(m_minutes)
       
   184     {
       
   185         retval.append(QString::number(m_minutes));
       
   186         retval.append(QLatin1Char('M'));
       
   187     }
       
   188 
       
   189     if(m_seconds || m_seconds)
       
   190     {
       
   191         retval.append(QString::number(m_seconds));
       
   192 
       
   193         if(m_mseconds)
       
   194             retval.append(serializeMSeconds(m_mseconds));
       
   195 
       
   196         retval.append(QLatin1Char('S'));
       
   197     }
       
   198     else if(!m_years && !m_months && !m_days && !m_hours && !m_minutes)
       
   199         retval.append(QLatin1String("0S"));
       
   200 
       
   201     return retval;
       
   202 }
       
   203 
       
   204 YearProperty Duration::years() const
       
   205 {
       
   206     return m_years;
       
   207 }
       
   208 
       
   209 MonthProperty Duration::months() const
       
   210 {
       
   211     return m_months;
       
   212 }
       
   213 
       
   214 DayCountProperty Duration::days() const
       
   215 {
       
   216     return m_days;
       
   217 }
       
   218 
       
   219 HourProperty Duration::hours() const
       
   220 {
       
   221     return m_hours;
       
   222 }
       
   223 
       
   224 MinuteProperty Duration::minutes() const
       
   225 {
       
   226     return m_minutes;
       
   227 }
       
   228 
       
   229 SecondProperty Duration::seconds() const
       
   230 {
       
   231     return m_seconds;
       
   232 }
       
   233 
       
   234 MSecondProperty Duration::mseconds() const
       
   235 {
       
   236     return m_mseconds;
       
   237 }
       
   238 
       
   239 ItemType::Ptr Duration::type() const
       
   240 {
       
   241     return BuiltinTypes::xsDuration;
       
   242 }
       
   243 
       
   244 QT_END_NAMESPACE