PRB: VariantTimeToSystemTime and SystemTimeToVariantTime Functions Ignore Milliseconds Value of System Time (297463)



The information in this article applies to:

  • Microsoft Win32 Software Development Kit (SDK) 4.0

This article was previously published under Q297463

SYMPTOMS

When you use the VariantTimeToSystemTime function to convert variant time to system time and the SystemTimeToVariantTime function to convert system time to variant time, the milliseconds value appears as zero or is ignored.

CAUSE

SystemTimeToVariantTime (which uses the VarUdateFromDate function) and VariantTimeToSystemTime (which uses the VarDateFromUdate function) are not resolute up to milliseconds.

RESOLUTION

Do not use SystemTimeToVariantTime, VariantTimeToSystemTime, VarUdateFromDate, or VarDateFromUdate if you require millisecond resolution.

Beginning with Microsoft Windows NT 4.0 Service Pack 4 (SP4), Windows 95 with Distributed Component Object Model (DCOM) 1.2, and Windows 98, Automation now supports passing user-defined types (UDTs) in variants and safearrays of user-defined types as arguments to methods. This allows methods to return UDTs and allows programmers of Automation controllers (Automation clients) to call methods that require pointers to structures. As an alternative to using the above-mentioned functions, you can wrap the SYSTEMTIME structure to a user and pass the user-defined data type to automation interfaces.

For more information on user-defined data types, see the following Microsoft Web site:

STATUS

This behavior is by design.

MORE INFORMATION

To reproduce this problem, replace the code that is generated for a standard console application with the following code:
#include "stdafx.h"
#include <ole2.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
	SYSTEMTIME stNow1, stNow2;
	
	GetSystemTime(&stNow1);
	
	stNow2 = stNow1;
	
	// Set different values for milliseconds.
	stNow1.wMilliseconds =10;
	stNow2.wMilliseconds =0;
	
	printf("SystemTime with Milliseconds:\n"
		"Hours: %d, Minutes: %d, Seconds: %d, Milliseconds: %d\n",
		stNow1.wHour, stNow1.wMinute, stNow1.wSecond, stNow1.wMilliseconds);
	printf("SystemTime without Milliseconds:\n"
		"Hours: %d, Minutes: %d, Seconds: %d, Milliseconds: %d\n",
		stNow2.wHour, stNow2.wMinute, stNow2.wSecond, stNow2.wMilliseconds);
	
	double dTime1,dTime2;
	
	SystemTimeToVariantTime (&stNow1,&dTime1);
	SystemTimeToVariantTime (&stNow2,&dTime2);
	
	printf("VariantTime from SystemTime with Milliseconds: "
		"%0.9f\n", dTime1);
	printf("VariantTime from SystemTime without Milliseconds: "
		"%0.9f\n", dTime2);
	printf("(Both of the above values are same, which shows that the\n"
		"SystemTimeToVariantTime conversion routine ignores Milliseconds)");
	
	// Calculate a variant time with milliseconds.
	// For example, .840845486 portion of 37085.840845486 corresponds
	// to 20 hours, 10 minutes, 49 seconds, and 50 milliseconds.
	dTime1 = 37085.840845486;
	printf("\n\nVariantTime with Milliseconds: %0.9f \n", dTime1);
	printf("SystemTime corresponding to above variant date:\n"
		"Hours: 20, Minutes: 20, Seconds: 49, Milliseconds: 50\n");
	
	SYSTEMTIME stCheck;
	VariantTimeToSystemTime(dTime1,&stCheck);
	
	printf("Value of Milliseconds in SystemTime after conversion: %d\n",
		stCheck.wMilliseconds );
	printf("(The above value shows that the VariantTimeToSystemTime\n"
		"conversion routine ignores Milliseconds)\n\n");
	
	return 0;
}
				

Modification Type:MajorLast Reviewed:7/18/2001
Keywords:kbAPI kbDSupport kbinfo kbprb KB297463