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

Article translations Article translations
Article ID: 297463
This article was previously published under Q297463
Expand all | Collapse all

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.

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;
}
				

Properties

Article ID: 297463 - Last Review: October 26, 2012 - Revision: 2.0
Keywords: 
kbapi kbDSupport kbinfo kbprb KB297463
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com