简体   繁体   English

在C ++中为FILETIME结构分配数据和时间

[英]Assigning data and time to FILETIME structure in C++

In windows and in C++. 在Windows和C ++中。 I have following code. 我有以下代码。

typedef struct _FILETIME {
    DWORD dwLowDateTime;
    DWORD dwHighDateTime;
} FILETIME, *PFILETIME, *LPFILETIME;

struct DateTime
{
    unsigned int dwLowDateTime;
    unsigned int dwHighDateTime;
};

FILETIME ftTime;
GetSystemTimeAsFileTime(&ftTime);

DateTime myTime;
myTime.dwHighDateTime = (unsigned int)ftTime.dwHighDateTime;
myTime.dwLowDateTime  = (unsigned int)ftTime.dwLowDateTime;

Now I have requirement to assign values like 现在我需要分配诸如

2012-06-25 12:00:10.123

to "myTime" or ftTime. 到“ myTime”或ftTime。

How can I achive this? 我该如何实现?

Another question is how can I get number of seconds elapsed like 64-bit integer for date "2012-06-25 12:00:10.123" ? 另一个问题是如何获取日期“ 2012-06-25 12:00:10.123”所经过的秒数(如64位整数)? How do I convert this to __int64 so that I assign to FILETIME? 如何将其转换为__int64以便分配给FILETIME?

I have seen in another question post 我在另一个问题帖子中看到了

__int64 t;
FILETIME ft;
ft.dwLowDateTime = (DWORD)t;
ft.dwHighDateTime = (DWORD)(t >> 32);

I am not supposed to use Boost in my project. 我不应该在项目中使用Boost。

Thanks! 谢谢!

Assign the value "2012-06-25 12:00:10.123" to the SystemTime structure and convert it into FileTime using the function SystemTimeToFileTime. 将值“ 2012-06-25 12:00:10.123”分配给SystemTime结构,并使用SystemTimeToFileTime函数将其转换为FileTime。

SYSTEMTIME st,st1;
st.wDay = 25;
st.wMonth = 06;
st.wYear = 2012;
st.wHour = 12;
st.wMinute = 10;
st.wSecond = 10;
st.wMilliseconds = 10;

FILETIME ft;
SystemTimeToFileTime(&st,&ft);
FileTimeToSystemTime(&ft,&st1);

Use SystemTimeToFileTime . 使用SystemTimeToFileTime

Assign the datetime data to the SYSTEMTIME structure. 将日期时间数据分配给SYSTEMTIME结构。

typedef struct _SYSTEMTIME {
  WORD wYear;
  WORD wMonth;
  WORD wDayOfWeek;
  WORD wDay;
  WORD wHour;
  WORD wMinute;
  WORD wSecond;
  WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;

And use 并使用

BOOL WINAPI SystemTimeToFileTime(
  __in   const SYSTEMTIME *lpSystemTime,
  __out  LPFILETIME lpFileTime
);

for the conversion. 进行转换。

Update: For converting from the string to date,time components, you can use DateTime::Parse Method(String) method. 更新:要从字符串转换为日期,时间分量,可以使用DateTime::Parse Method(String)方法。 Then assign the values to the SYSTEMTIME structure and then convert that to FileTime . 然后将值分配给SYSTEMTIME结构,然后将其转换为FileTime

There are multiple ways and functions that can get you there. 有多种方法和功能可助您一臂之力。

You can use DosDateTimeToFileTime() to convert year/month/day h:m:s into FILETIME . 您可以使用DosDateTimeToFileTime()将年/月/日的h:m:s转换为FILETIME You'll need to take additional care of the odd seconds and second fractions. 您需要格外注意秒数和秒数。

You also have C(++)'s mktime() to first get time in seconds since 1970. Then you'd need to convert it to seconds since January 1st 1601 by adding 11644473600 to it, scale it to 100-nanoseconds and add the seconds fraction. 您还可以使用C(++)的mktime()来获取自1970年以来的时间,以秒为单位。然后,您需要将其添加为11644473600,将其转换为秒,以自1601年1月1日起,将其缩放为100纳秒并添加秒分数。

But SystemTimeToFileTime() is a better choice because it handles milliseconds as well. 但是SystemTimeToFileTime()是一个更好的选择,因为它也可以处理毫秒。

It's not clear what you mean by how can I get number of seconds elapsed like 64-bit integer for date "2012-06-25 12:00:10.123"? 目前还不清楚您是什么意思, how can I get number of seconds elapsed like 64-bit integer for date "2012-06-25 12:00:10.123"?

Seconds elapsed since then until now? 从那以后到现在已经过去了几秒钟? If so, you get the current time in seconds and convert the given date/time to seconds (described above) and subtract one from the other. 如果是这样,您将获得以秒为单位的当前时间,并将给定的日期/时间转换为秒(如上所述),然后再减去一个。

Seconds elapsed since an earlier time until then? 从那之前到现在已经过去了几秒钟? Do the same, get seconds for both date/time pairs and subtract. 进行相同的操作,获得两个日期/时间对的秒数并减去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM