gpt4 book ai didi

windows - 在 SYSTEMTIME 上执行算术运算

转载 作者:可可西里 更新时间:2023-11-01 13:27:47 25 4
gpt4 key购买 nike

我有一个用 SYSTEMTIME 表示的时间值,我想从中加/减 1 小时并得到新获得的 SYSTEMTIME。我希望转换应该注意加法/减法或月份变化或 e1 年变化的日期变化。

如果有一些在 SYSTEMTIME 上做算术的 windows api,有人可以帮我吗

最佳答案

如果您使用的是 C#(或 VB.NET 或 ASP.NET),则可以使用

DateTime dt = DateTime.Now.AddHours(1);

可以用负数减去:

DateTime dt = DateTime.Now.AddHours(-1);

已编辑:我从 this post 中提取了一个答案

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.

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)

关于windows - 在 SYSTEMTIME 上执行算术运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8308236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com