How to convert getmeasure timestamps

The Netatmo API is a set of webservices allowing developers and third parties to access Netatmo device's data.
Post Reply
Bernard
Posts: 3
Joined: 18 Aug 2015, 18:26

How to convert getmeasure timestamps

Post by Bernard »

Hi,
I have 2 .NET C# related questions, when using the 'getmeasure' api:
1. How can I generate or convert a Date to the timestamps required by the netatmo private api's querystring ('date_begin' and 'date_end')'?
2. How can I reconvert the timestamps that I receive form the netatmo private api 'beg_time' and 'timeserver' into Date objects?
many thanks in advance for your help, best regards,
Bernard
rdg
Posts: 58
Joined: 22 Dec 2012, 12:40
Location: 77510

Re: How to convert getmeasure timestamps

Post by rdg »

Hi,

I don't know much about C# but here are the routines I use in plain C :

Code: Select all

// -------------------------------------------------------------------------
// Time stuff.
//
void TimetToFileTime(time_t t, LPFILETIME pft)
{
	LONGLONG	ll;

	ll = Int32x32To64(t, 10000000) + 116444736000000000;
	pft->dwLowDateTime = (DWORD)ll;
	pft->dwHighDateTime = (DWORD)(ll >> 32);
}

time_t FileTimeToTimet(LPFILETIME pft)
{
	LONGLONG	ll;

	ll = (LONGLONG)pft->dwLowDateTime | ((LONGLONG)pft->dwHighDateTime << 32);
	return (time_t)((ll - 116444736000000000) / 10000000);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Convert a time_t to a "dd/mm/yyyy hh:mm:ss" string.
//
LPTSTR Time2String(UINT nTime)
{
static TCHAR szBuf[40];	// WARNING!!!
	FILETIME	ftUTC, ftLocal;
	SYSTEMTIME	st;
	
	TimetToFileTime((time_t)nTime, &ftUTC);				// UTC
	(void)FileTimeToLocalFileTime(&ftUTC, &ftLocal);	// local
	(void)FileTimeToSystemTime(&ftLocal, &st);
	(void)_stprintf_s(szBuf, DIM(szBuf), TEXT("%02u/%02u/%04u %02u:%02u:%02u"),
				st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond);
	return szBuf;
} // Time2String
HTH.
Bernard
Posts: 3
Joined: 18 Aug 2015, 18:26

Re: How to convert getmeasure timestamps

Post by Bernard »

Hi,
Thanks for your help, I successfully convert the values into dates now :-) !
The final code in C# looks something like:

DateTime timeserver = new DateTime((long.Parse(netatmo_timeserver) * 10000000) + 116444736000000000);
timeserver = timeserver.AddYears(1600);

Please note I needed to add an extra 1600 years to the formula ...
I'm sure there's a better way to retrieve timestamps from the netatmo api's ;-).
But at least, it seems to do the trick pretty well.

Keep up the good work @netatmo !
Thanks again, best regards,

Bernard
Post Reply

Return to “Netatmo API”