windows 使用 SYSTEMTIME、FILETIME 和 ULARGE_INTEGER 修改日期和时间值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5118411/
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 16:17:02  来源:igfitidea点击:

Modifying the date and time values using SYSTEMTIME, FILETIME, and ULARGE_INTEGER

c++windowssystemtimefiletime

提问by Seb

I am making a program, in C++ using Visual Studio 2005, that needs to create a watermark with the time on a set of images.

我正在使用 Visual Studio 2005 在 C++ 中制作一个程序,该程序需要在一组图像上创建带有时间的水印。

These images are taken from a video that were processed at certain time intervals. What I am trying to do is to modify the time on each image through SYSTEMTIME. I looked at the MSDN and it says not to modify the values within SYSTEMTIME itself, but to convert it into a FILETIME and then an ULARGE_INTEGER. My question is how is the ULARGE_INTEGER split up? Is the HighPart the date and the Low Part the time and if that's the case how to I take into account rollover? Like if a image shows up at 11:58pm on 2/25/2011 and goes through until 12:11 2/26/2011? Would just adding the specified value automatically be taken into account and shown when I convert it back into a SYSTEMTIME variable?

这些图像取自以特定时间间隔处理的视频。我想要做的是通过SYSTEMTIME修改每个图像上的时间。我查看了 MSDN,它说不要修改 SYSTEMTIME 本身内的值,而是将其转换为 FILETIME,然后是 ULARGE_INTEGER。我的问题是 ULARGE_INTEGER 是如何拆分的?HighPart 是日期,Low Part 是时间,如果是这种情况,我该如何考虑翻转?就像图像在 2/25/2011 的晚上 11:58 显示并一直显示到 2/26/2011 的 12:11 一样?当我将其转换回 SYSTEMTIME 变量时,是否会自动考虑并显示添加指定的值?

Thanks in advance for your help.

在此先感谢您的帮助。

采纳答案by xtofl

They suggest converting SYSTEMTIMEto 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 yourtime, and convert back to SYSTEMTIME.

他们建议转换SYSTEMTIMEFILETIME,这是一个纪元以来的刻度数。然后,您可以添加所需数量的“滴答”(即 100ns 间隔)以指示您的时间,然后转换回SYSTEMTIME.

The ULARGE_INTEGERstruct is a union with a QuadPartmember, which is a 64bit number, that can be directly added to (on recent hardware).

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;
    const double c_dNumberOf100nsIntervals = 
                    seconds / c_dSecondsPer100nsInterval;

    // note: you may want to round the number of intervals.
    u.QuadPart += c_dNumberOf100nsIntervals;

    memcpy( &f, &u, sizeof( f ) );

    FileTimeToSystemTime( &f, &s );
    return s;
 }