コード例 #1
0
static QString formatTimeInterval(CBigNum t)
{
    enum  EUnit { YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, NUM_UNITS };

    const int SecondsPerUnit[NUM_UNITS] =
    {
        31556952, // average number of seconds in gregorian year
        31556952/12, // average number of seconds in gregorian month
        24*60*60, // number of seconds in a day
        60*60, // number of seconds in an hour
        60, // number of seconds in a minute
        1
    };

    const char* UnitNames[NUM_UNITS] =
    {
        "year",
        "month",
        "day",
        "hour",
        "minute",
        "second"
    };

    if (t > 0xFFFFFFFF)
    {
        t /= SecondsPerUnit[YEAR];
        return QString("%1 years").arg(t.ToString(10).c_str());
    }
    else
    {
        unsigned int t32 = t.getuint();

        int Values[NUM_UNITS];
        for (int i = 0; i < NUM_UNITS; i++)
        {
            Values[i] = t32/SecondsPerUnit[i];
            t32 %= SecondsPerUnit[i];
        }

        int FirstNonZero = 0;
        while (FirstNonZero < NUM_UNITS && Values[FirstNonZero] == 0)
            FirstNonZero++;

        QString TimeStr;
        for (int i = FirstNonZero; i < std::min(FirstNonZero + 3, (int)NUM_UNITS); i++)
        {
            int Value = Values[i];
            TimeStr += QString("%1 %2%3 ").arg(Value).arg(UnitNames[i]).arg((Value == 1)? "" : "s"); // FIXME: this is English specific
        }
        return TimeStr;
    }
}
コード例 #2
0
ファイル: Base58.cpp プロジェクト: CCJY/rippled
std::string Base58::raw_encode (
    unsigned char const* begin, unsigned char const* end,
        Alphabet const& alphabet, bool withCheck)
{
    CAutoBN_CTX pctx;
    CBigNum bn58 = 58;
    CBigNum bn0 = 0;

    // Convert little endian data to bignum
    CBigNum bn (begin, end);
    std::size_t const size (std::distance (begin, end));

    // Convert bignum to std::string
    std::string str;
    // Expected size increase from base58 conversion is approximately 137%
    // use 138% to be safe
    str.reserve (size * 138 / 100 + 1);
    CBigNum dv;
    CBigNum rem;

    while (bn > bn0)
    {
        if (!BN_div (&dv, &rem, &bn, &bn58, pctx))
            throw bignum_error ("EncodeBase58 : BN_div failed");

        bn = dv;
        unsigned int c = rem.getuint ();
        str += alphabet [c];
    }

    for (const unsigned char* p = end-2; p >= begin && *p == 0; p--)
        str += alphabet [0];

    // Convert little endian std::string to big endian
    reverse (str.begin (), str.end ());
    return str;
}