Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?

Thanks in advance for your help.

like image 281
Seb Avatar asked Feb 25 '11 14:02

Seb


1 Answers

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;
    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;
 }
like image 62
xtofl Avatar answered Oct 27 '22 01:10

xtofl