在SYSTEMTIME上执行算术 [英] performing Arithmetic on SYSTEMTIME
问题描述
我有一个用SYSTEMTIME表示的时间值,我想从中减去1小时并获得新获得的SYSTEMTIME.我希望转换应考虑加减法的日期更改或月份更改或e1年更改.
I have a time value represented in SYSTEMTIME, i want to add/subtract 1 hour from it and get the newly obtained SYSTEMTIME. I want the conversion should take care of the date change on addition/subtraction or month change or e1 year change .
如果有一些可以在SYSTEMTIME上进行算术运算的Windows API,有人可以帮我吗?
Can someone help me with this if there is some windows api which does arithmetic on SYSTEMTIME
推荐答案
如果您使用的是C#(或VB.NET或ASP.NET),则可以使用
If you're using C# (or VB.NET, or ASP.NET) you can use
DateTime dt = DateTime.Now.AddHours(1);
您可以使用负数减去:
DateTime dt = DateTime.Now.AddHours(-1);
已编辑: 我从这篇文章
他们建议将SYSTEMTIME转换为FILETIME,这是许多 从一个时代开始滴答作响.然后,您可以添加所需数量的滴答声" (即100ns间隔)表示您的时间,然后转换回 系统时间.
They suggest converting SYSTEMTIME to FILETIME, which is a number of ticks since an epoch. You can then add the required number of 'ticks' (i.e. 100ns intervals) to indicate your time, and convert back to SYSTEMTIME.
ULARGE_INTEGER结构是与QuadPart成员的并集,即 一个64位的数字,可以直接添加到该数字(在最新的硬件上).
The ULARGE_INTEGER struct is a union with a QuadPart member, which is a 64bit number, that can be directly added to (on recent hardware).
SYSTEMTIME add( SYSTEMTIME s, double seconds ) {
FILETIME f;
SystemTimeToFileTime( &s, &f );
ULARGE_INTEGER u ;
memcpy( &u , &f , sizeof( u ) );
const double c_dSecondsPer100nsInterval = 100. * 1.E-9;
u.QuadPart += seconds / c_dSecondsPer100nsInterval;
memcpy( &f, &u, sizeof( f ) );
FileTimeToSystemTime( &f, &s );
return s;
}
如果要增加一个小时,请使用SYSTEMTIME s2 = add(s1, 60*60)
If you want to add an hour use SYSTEMTIME s2 = add(s1, 60*60)
这篇关于在SYSTEMTIME上执行算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!