如何在DATE与SYSTEMTIME之间转化

某些COM组件用到的时间数据类型是DATE,我们可以查到,DATE类型定义如下:
typedef double DATE;

DATE变量可以直接赋值给_variant_t对象,在用ADO访问数据库的时候,需要在_variant_t和SYSTEMTIME之间转换,MS提供了如下两个函数:
SystemTimeToVariantTime<script src="langref.js"></script><script src="linkcss.js"></script> rel="stylesheet" href="backsdk4.css">

SystemTimeToVariantTime

Converts a system time to a variant representation.

INT SystemTimeToVariantTime(
  LPSYSTEMTIME  lpSystemTime,  
  double        *pvtime           
);
Parameters
lpSystemTime
The system time.
pvtime
Returned variant time.
Return Value

The function returns one of the following:

ValueMeaning
zeroFailure.
non-zeroSuccess.
Comments

A variant time is stored as an 8-byte real value (double), representing a date between January 1, 100 and December 31, 9999, 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. Negative numbers represent dates prior to December 30, 1899.

The variant time resolves to one second. Any milliseconds in the input date are ignored.

The LPSYSTEMTIME structure is useful for the following reasons:

  • It spans all time/date periods. MS-DOS date/time is limited to representing only those dates between 1/1/1980 and 12/31/2107.
  • The date/time elements are all easily accessible without needing to do any bit decoding.
  • The National Data Support data and time formatting functions GetDateFormat and GetTimeFormat take a LPSYSTEMTIME value as input. For more information, see the COM Programmer’s Reference in the Platform SDK.
  • It is the default Win32 time/date data format supported by Windows NT and Windows 95.

The SystemTimeToVariantTime function will accept invalid dates and try to fix them when resolving to a VARIANT time. For example, an invalid date such as 2/29/2001 will resolve to 3/1/2001. Only days are fixed, so invalid month values result in an error being returned. Days are checked to be between 1 and 31. Negative days and days greater than 31 results in an error. A day less than 31 but greater than the maximum day in that month has the day promoted to the appropriate day of the next month. A day equal to zero resolves as the last day of the previous month. For example, an invalid dates such as 2/0/2001 will resolve to 1/31/2001.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 OSR2 or later.
  Header: Declared in oleauto.h.
  Library: Use oleaut32.lib.


VariantTimeToSystemTime<script src="langref.js"></script><script src="linkcss.js"></script> rel="stylesheet" href="backsdk4.css">

VariantTimeToSystemTime

Converts the variant representation of time to system time values.

INT VariantTimeToSystemTime( 
  double  vtime,              
  LPSYSTEMTIME  lpSystemTime  
);
Parameters
vtime
The variant time that will be converted.
lpSystemTime
Pointer to the location where the converted time will be stored.
Return Value

The return value obtained from the returned INT is one of the following.

ValueMeaning
TRUESuccess.
FALSEFailure.
Comments

A variant time is stored as an 8-byte real value (double), representing a date between January 1, 100 and December 31, 9999, 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. Negative numbers represent the dates prior to December 30, 1899.

Using the SYSTEMTIME structure is useful because:

  • It spans all time/date periods. MS-DOS date/time is limited to representing only those dates between 1/1/1980 and 12/31/2107.
  • The date/time elements are all easily accessible without needing to do any bit decoding.
  • The National Language Support data and time formatting functions GetDateFormat and GetTimeFormat take a SYSTEMTIME value as input.
  • It is the default Win32 time and date data format supported by Windows NT and Windows 95.

The VariantTimeToSystemTime function will accept invalid dates and try to fix them when resolving to a VARIANT time. For example, an invalid date such as 2/29/2001 will resolve to 3/1/2001. Only days are fixed, so invalid month values result in an error being returned. Days are checked to be between 1 and 31. Negative days and days greater than 31 results in an error. A day less than 31 but greater than the maximum day in that month has the day promoted to the appropriate day of the next month. A day equal to zero resolves as the last day of the previous month. For example, an invalid dates such as 2/0/2001 will resolve to 1/31/2001.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 OSR2 or later.
  Header: Declared in oleauto.h.
  Library: Use oleaut32.lib.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值