Converting SYSTEMTIME to time_t?

Jul 10, 2014 at 7:19pm
I am having trouble converting SYSTEMTIME to time_t

Can anyone help me with this situation please?
Jul 10, 2014 at 7:21pm
Not sure if there is another way but you could probably use std::mktime.
http://en.cppreference.com/w/cpp/chrono/c/mktime
Jul 10, 2014 at 7:50pm
bump -
Jul 10, 2014 at 7:59pm
bump --
Jul 10, 2014 at 8:00pm
I meant something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <ctime>

int main()
{
	SYSTEMTIME st;
	std::tm tm;
	
	tm.tm_sec = st.wSecond;
	tm.tm_min = st.wMinute;
	tm.tm_hour = st.wHour;
	tm.tm_mday = st.wDay;
	tm.tm_mon = st.wMonth - 1;
	tm.tm_year = st.wYear - 1900;
	tm.tm_isdst = -1;
	
	std::time_t t = std::mktime(&tm);
}
Jul 10, 2014 at 8:08pm
Thanks for your reply, mktime returns -1 with that code
Jul 10, 2014 at 8:10pm
Did you use a valid SYSTEMTIME?
Jul 10, 2014 at 8:21pm
yeah its from the calendar component
Jul 10, 2014 at 8:29pm
Sorry mate it was the wrong SYSTEMTIME after that. I think I been working too long lol break for me. It works brilliantly thanks mate. Could you explain to me why you have to minus 1900 and 1 from the month?
Jul 10, 2014 at 8:52pm
It's just that they store some of the data differently. SYSTEMTIME stores the month as a number from 1 to 12 while std::tm stores it as a number from 0 to 11. SYSTEMTIME stores the year like you would expect (starting from 0) but std::tm stores it as the number of years since 1900.

You can compare them for yourself if you want:
http://en.cppreference.com/w/cpp/chrono/c/tm
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724950.aspx
Jul 10, 2014 at 9:40pm
thanks
Topic archived. No new replies allowed.