Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Objective-C

SystemTime to VariantTime with Milliseconds

4.06/5 (8 votes)
9 Feb 20073 min read 3   1.8K  
Enhanced system time / variant time conversion with milliseconds

Image 1

Introduction

When you use the Microsoft Automation function VariantTimeToSystemTime to convert variant time to system time and the SystemTimeToVariantTime to convert system time to variant time, the milliseconds value appears as zero or is ignored. This is a known issue documented in MSDN knowledge base, under ID Q297463. However, many times, ignoring milliseconds is not a option. Two simple functions wrapping the original API functions can be used to convert SystemTime to VariantTime and vice versa without losing the millisecond information.

The sample application is just to demonstrate the use of these functions. You can type in the SystemTime entries and click convert to see the equivalent VariantTime or you can type in a VariantTime value and select conversion Type for reverse conversion and convert it back to SystemTime. Clicking on the 'GetCurrentTime' button will populate SystemTime structure from the current time on your system. The application also does some basic validations on the range of input you can specify.

Using the Code

Two functions, SystemTimeToVariantTimeWithMilliseconds and VariantTimeToSystemTimeWithMilliseconds can replace the Microsoft Automation functions and can return the result in a similar to the original functions without losing the millisecond information.

a) SystemTimeToVariantTimeWithMilliseconds takes systemtime as the input and passes the information without millisecond information to the Microsoft Automation function. The result would be the converted variant time without milliseconds. The reason for not passing the millisecond information to the Microsoft Automation function is that the function automatically rounds off the value to the nearest second, which we don't want. We then add the variant portion for the millisecond.

A variant time is stored as an 8-byte real value (double), representing a date between January 1, 1753 and December 31, 2078, inclusive. The value 2.0 represents January 1, 1900; 3.0 represents January 2, 1900, and so on. Adding 1 to the value increments the date by a day. The fractional part of the value represents the time of day. Therefore, 2.5 represents noon on January 1, 1900; 3.25 represents 6:00 A.M. on January 2, 1900, and so on. So, 0.5 represents 12 hours i.e. 12*60*60 seconds, hence 1 second = 0.5/(12*60*60) = .0000115740740740

C++
BOOL CSysTimeConversionDlg::SystemTimeToVariantTimeWithMilliseconds 
                 (/*input*/ SYSTEMTIME st, /*output*/double *dVariantTime)
{
    BOOL retVal = TRUE;

    WORD wMilliSeconds = st.wMilliseconds; // save the milli second 
                                           // information
    st.wMilliseconds = 0; // pass 0 milliseconds to the function and get  
                          // the converted value without milliseconds
    double dWithoutms;
    retVal = SystemTimeToVariantTime(&st, &dWithoutms) ;

    // manually convert the millisecond information into variant 
    // fraction and add it to system converted value
    double OneMilliSecond = ONETHOUSANDMILLISECONDS/1000 ;
    *dVariantTime = dWithoutms + (OneMilliSecond * wMilliSeconds);

    return retVal;
}

b) VariantTimeToSystemTimeWithMilliseconds takes in the variant time and calculates each component of SYSTEMTIME individually down to the milliseconds and thus will have the precious millisecond information. We start with using the Microsoft Automation function VariantTimeToSystemTime to give us the variant time from system time. We delete 0.5 seconds from the original variant time so that we remove the rounding off problem with VariantTimeToSystemTime function. We then calculate each component of the systemtime from the fraction until we obtain the millisecond information. Once we have the millisecond information, we then add 0.5 second to compensate for our earlier adjustment.

C++
BOOL CSysTimeConversionDlg::VariantTimeToSystemTimeWithMilliseconds 
                  (/*input*/ double dVariantTime, /*output*/SYSTEMTIME *st)
{
    BOOL retVal = TRUE;

    double halfsecond = ONETHOUSANDMILLISECONDS / 2.0; 
    // ONETHOUSANDMILLISECONDS is equal to 0.0000115740740740

    retVal = VariantTimeToSystemTime(dVariantTime - halfsecond, st); 
    // this takes care of rounding problem with 
    // VariantTimetoSystemTime function
    if (retVal == FALSE)
    {
        return retVal;
    }

    double fraction = dVariantTime - (int) dVariantTime; 
    // extracts the fraction part

    double hours; 
    hours = fraction = (fraction - (int)fraction) * 24;

    double minutes;
    minutes = (hours - (int)hours) * 60;

    double seconds;
    seconds = (minutes - (int)minutes) * 60;

    double milliseconds;
    milliseconds = (seconds - (int)seconds) * 1000;

    milliseconds = milliseconds + 0.5; // rounding off millisecond to the 
                                       // nearest millisecond 
    if (milliseconds < 1.0 || milliseconds > 999.0) //Fractional 
                          // calculations may yield in results like
        milliseconds = 0; // 0.00001 or 999.9999 which should actually 
                          // be zero (slightly above or below limits 
                          // are actually zero)

    if (milliseconds) 
        st->wMilliseconds = (WORD) milliseconds;
    else  // if there is 0 milliseconds, then we don't have the problem !!
        retVal = VariantTimeToSystemTime(dVariantTime, st); // 

    return retVal;
}

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.