windows 在 SYSTEMTIME 上执行算术

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8308236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:33:00  来源:igfitidea点击:

performing Arithmetic on SYSTEMTIME

windowsmsdnsystemtime

提问by Peter

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 表示的时间值,我想从中加/减 1 小时并获得新获得的 SYSTEMTIME。我希望转换应该处理加法/减法或月份更改或 e1 年更改的日期更改。

Can someone help me with this if there is some windows api which does arithmetic on SYSTEMTIME

如果有一些 Windows api 在 SYSTEMTIME 上进行算术运算,有人可以帮我解决这个问题吗

回答by Marco

If you're using C# (or VB.NET, or ASP.NET) you can use

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

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

You can use negative numbers to subtract:

您可以使用负数来减去:

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

EDITED:I extract an asnwer from 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 转换为 FILETIME,这是自一个纪元以来的刻度数。然后,您可以添加所需数量的“滴答”(即 100ns 间隔)以指示您的时间,并转换回 SYSTEMTIME。

ULARGE_INTEGER 结构是与 QuadPart 成员的联合,它是一个 64 位数字,可以直接添加到(在最近的硬件上)。

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

If you want to add an hour use SYSTEMTIME s2 = add(s1, 60*60)

如果你想增加一个小时使用 SYSTEMTIME s2 = add(s1, 60*60)

回答by Rick C. Hodgin

To add signed seconds (forward or backward in time) in C++:

在 C++ 中添加有符号秒数(时间向前或向后):

const double clfSecondsPer100ns = 100. * 1.E-9;
void iAddSecondsToSystemTime(SYSTEMTIME* timeIn, SYSTEMTIME* timeOut, double tfSeconds)
{
    union {
        ULARGE_INTEGER li;
        FILETIME       ft;
    };

    // Convert timeIn to filetime
    SystemTimeToFileTime(timeIn, &ft);

    // Add in the seconds
    li.QuadPart += tfSeconds / clfSecondsPer100ns;

    // Convert back to systemtime
    FileTimeToSystemTime(&ft, timeOut);
}

回答by puccipucci

#include <stdio.h>
#include <windows.h>
#define NSEC 60*60

main()
{
SYSTEMTIME st;
FILETIME ft;

// Get local time from system
GetLocalTime(&st);

printf("%02d/%02d/%04d %02d:%02d:%02d\n",
  st.wDay,st.wMonth,st.wYear,st.wHour,st.wMinute,st.wSecond);

// Convert to filetime
SystemTimeToFileTime(&st,&ft);

// Add NSEC seconds
((ULARGE_INTEGER *)&ft)->QuadPart +=(NSEC*10000000LLU);

// Convert back to systemtime
FileTimeToSystemTime(&ft,&st);

printf("%02d/%02d/%04d %02d:%02d:%02d\n",
  st.wDay,st.wMonth,st.wYear,st.wHour,st.wMinute,st.wSecond);
}