//==============================================================================
String RelativeTime::getDescription (const String& returnValueForZeroTime) const
{
    if (seconds < 0.001 && seconds > -0.001)
        return returnValueForZeroTime;

    String result;
    result.preallocateBytes (32);

    if (seconds < 0)
        result << '-';

    int fieldsShown = 0;
    int n = std::abs ((int) inWeeks());
    if (n > 0)
    {
        result << n << TRANS (n == 1 ? " week "
                                     : " weeks ");
        ++fieldsShown;
    }

    n = std::abs ((int) inDays()) % 7;
    if (n > 0)
    {
        result << n << TRANS (n == 1 ? " day "
                                     : " days ");
        ++fieldsShown;
    }

    if (fieldsShown < 2)
    {
        n = std::abs ((int) inHours()) % 24;
        if (n > 0)
        {
            result << n << TRANS (n == 1 ? " hr "
                                         : " hrs ");
            ++fieldsShown;
        }

        if (fieldsShown < 2)
        {
            n = std::abs ((int) inMinutes()) % 60;
            if (n > 0)
            {
                result << n << TRANS (n == 1 ? " min "
                                             : " mins ");
                ++fieldsShown;
            }

            if (fieldsShown < 2)
            {
                n = std::abs ((int) inSeconds()) % 60;
                if (n > 0)
                {
                    result << n << TRANS (n == 1 ? " sec "
                                                 : " secs ");
                    ++fieldsShown;
                }

                if (fieldsShown == 0)
                {
                    n = std::abs ((int) inMilliseconds()) % 1000;
                    if (n > 0)
                        result << n << TRANS (" ms");
                }
            }
        }
    }

    return result.trimEnd();
}
示例#2
0
String RelativeTime::getDescription (const String& returnValueForZeroTime) const
{
    if (numSeconds < 0.001 && numSeconds > -0.001)
        return returnValueForZeroTime;

    String result;
    result.preallocateBytes (32);

    if (numSeconds < 0)
        result << '-';

    int fieldsShown = 0;
    int n = std::abs ((int) inWeeks());
    if (n > 0)
    {
        translateTimeField (result, n, NEEDS_TRANS("1 week"), NEEDS_TRANS("2 weeks"));
        ++fieldsShown;
    }

    n = std::abs ((int) inDays()) % 7;
    if (n > 0)
    {
        translateTimeField (result, n, NEEDS_TRANS("1 day"), NEEDS_TRANS("2 days"));
        ++fieldsShown;
    }

    if (fieldsShown < 2)
    {
        n = std::abs ((int) inHours()) % 24;
        if (n > 0)
        {
            translateTimeField (result, n, NEEDS_TRANS("1 hour"), NEEDS_TRANS("2 hours"));
            ++fieldsShown;
        }

        if (fieldsShown < 2)
        {
            n = std::abs ((int) inMinutes()) % 60;
            if (n > 0)
            {
                translateTimeField (result, n, NEEDS_TRANS("1 minute"), NEEDS_TRANS("2 minutes"));
                ++fieldsShown;
            }

            if (fieldsShown < 2)
            {
                n = std::abs ((int) inSeconds()) % 60;
                if (n > 0)
                {
                    translateTimeField (result, n, NEEDS_TRANS("1 seconds"), NEEDS_TRANS("2 seconds"));
                    ++fieldsShown;
                }

                if (fieldsShown == 0)
                {
                    n = std::abs ((int) inMilliseconds()) % 1000;
                    if (n > 0)
                        result << n << ' ' << TRANS ("ms");
                }
            }
        }
    }

    return result.trimEnd();
}
const String RelativeTime::getDescription (const String& returnValueForZeroTime) const throw()
{
    if (seconds < 0.001 && seconds > -0.001)
        return returnValueForZeroTime;

    String result;

    if (seconds < 0)
        result = T("-");

    int fieldsShown = 0;
    int n = abs ((int) inWeeks());
    if (n > 0)
    {
        result << n << ((n == 1) ? TRANS(" week ")
                                 : TRANS(" weeks "));
        ++fieldsShown;
    }

    n = abs ((int) inDays()) % 7;
    if (n > 0)
    {
        result << n << ((n == 1) ? TRANS(" day ")
                                 : TRANS(" days "));
        ++fieldsShown;
    }

    if (fieldsShown < 2)
    {
        n = abs ((int) inHours()) % 24;
        if (n > 0)
        {
            result << n << ((n == 1) ? TRANS(" hr ")
                                     : TRANS(" hrs "));
            ++fieldsShown;
        }

        if (fieldsShown < 2)
        {
            n = abs ((int) inMinutes()) % 60;
            if (n > 0)
            {
                result << n << ((n == 1) ? TRANS(" min ")
                                         : TRANS(" mins "));
                ++fieldsShown;
            }

            if (fieldsShown < 2)
            {
                n = abs ((int) inSeconds()) % 60;
                if (n > 0)
                {
                    result << n << ((n == 1) ? TRANS(" sec ")
                                             : TRANS(" secs "));
                    ++fieldsShown;
                }

                if (fieldsShown < 1)
                {
                    n = abs ((int) inMilliseconds()) % 1000;
                    if (n > 0)
                    {
                        result << n << TRANS(" ms");
                        ++fieldsShown;
                    }
                }
            }
        }
    }

    return result.trimEnd();
}