Boolean OperatingSystem::getLastBootUpTime(CIMDateTime& lastBootUpTime)
{
    Uint64 sysUpTime = 0;

    if(getSystemUpTime(sysUpTime))
    {
        // convert sysUpTime to microseconds
        sysUpTime *= (1000 * 1000);

        CIMDateTime currentTime = CIMDateTime::getCurrentDateTime();
        CIMDateTime bootTime =
            CIMDateTime(currentTime.toMicroSeconds() - sysUpTime, false);

        // adjust UTC offset
        String s1 = currentTime.toString();
        String s2 = bootTime.toString();

        s2[20] = s1[20];
        s2[21] = s1[21];
        s2[22] = s1[22];
        s2[23] = s1[23];

        lastBootUpTime = CIMDateTime(s2);

        return true;
    }

    return false;
}
Example #2
0
String WsmUtils::toMicroSecondString(const CIMDateTime &rep)
{
    const Uint32 strDurationLength=26;
    Uint64 duration = rep.toMicroSeconds();
    Uint32 outputLength = 0;
    char buffer[strDurationLength];
    const char* output = Uint64ToString(buffer, duration, outputLength);
    return String(output, outputLength);
}
String IndicationFormatter::_localizeDateTime(
    const CIMDateTime & propertyValueDateTime,
    const Locale & locale)
{

    PEG_METHOD_ENTER (TRC_IND_FORMATTER,
	"IndicationFormatter::_localizeDateTime");

    // Convert dateTimeValue to be microSeconds,
    // the number of microseconds from the epoch starting
    // 0/0/0000 (12 am Jan 1, 1BCE)
    //

    CIMDateTime dateTimeValue = propertyValueDateTime;
    Uint64 dateTimeValueInMicroSecs =
        dateTimeValue.toMicroSeconds();

    // In ICU, as UTC milliseconds from the epoch starting
    // (1 January 1970 0:00 UTC)
    CIMDateTime dt;
    dt.set("19700101000000.000000+000");

    // Convert dateTimeValue to be milliSeconds,
    // the number of milliSeconds from the epoch starting
    // (1 January 1970 0:00 UTC)
    UDate dateTimeValueInMilliSecs =
       (Sint64)(dateTimeValueInMicroSecs - dt.toMicroSeconds())/1000;

    // Create a formatter for DATE and TIME with medium length
    // such as Jan 12, 1952 3:30:32pm
    DateFormat *fmt;

    try
    {
        if (locale == 0)
        {
            fmt = DateFormat::createDateTimeInstance(DateFormat::MEDIUM,
                                                     DateFormat::MEDIUM);
        }
        else
        {
            fmt = DateFormat::createDateTimeInstance(DateFormat::MEDIUM,
                                                     DateFormat::MEDIUM,
                                                     locale);
        }
    }
    catch(Exception& e)
    {
        PEG_TRACE_STRING(TRC_IND_FORMATTER, Tracer::LEVEL4, e.getMessage());
        PEG_METHOD_EXIT();
        return (dateTimeValue.toString());
    }
    catch(...)
    {
        PEG_TRACE_STRING(TRC_IND_FORMATTER, Tracer::LEVEL4,
        "Caught General Exception During DateFormat::createDateTimeInstance");

        PEG_METHOD_EXIT();
        return (dateTimeValue.toString());
    }

    if (fmt == 0)
    {
        PEG_TRACE_STRING(TRC_IND_FORMATTER, Tracer::LEVEL4,
            "Memory allocation error creating DateTime instance.");
        PEG_METHOD_EXIT();
        return (dateTimeValue.toString());
    }

    // Format the Date and Time
    UErrorCode status = U_ZERO_ERROR;
    UnicodeString dateTimeUniStr;
    fmt->format(dateTimeValueInMilliSecs, dateTimeUniStr, status);

    if (U_FAILURE(status))
    {
        delete fmt;
        PEG_METHOD_EXIT();
        return (dateTimeValue.toString());
    }

    // convert UnicodeString to char *
    char dateTimeBuffer[256];
    char *extractedStr = 0;

    // Copy the contents of the string into dateTimeBuffer
    Uint32 strLen = dateTimeUniStr.extract(0, sizeof(dateTimeBuffer),
					   dateTimeBuffer);

    // There is not enough space in dateTimeBuffer
    if (strLen > sizeof(dateTimeBuffer))
    {
	extractedStr = new char[strLen + 1];
	strLen = dateTimeUniStr.extract(0, strLen + 1, extractedStr);
    }
    else
    {
	extractedStr = dateTimeBuffer;
    }

    String datetimeStr = extractedStr;

    if (extractedStr != dateTimeBuffer)
    {
	delete extractedStr;
    }

    delete fmt;

    PEG_METHOD_EXIT();
    return (datetimeStr);
}