Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VisualC++

Get Server Side Data-time with C++

1.92/5 (4 votes)
17 Oct 2015CPOL1 min read 25K  
This tip will help you to get server side date-time

Introduction

Sometimes, we want to get server side date-time for checking something in its own application, example check licence key, check time using of any program, and... anything. This tip might help you!

Get Server Time In C++

Background

I use SYSTEMTIME, HINTERNET, HttpQueryInfo, InternetOpenUrl, InternetOpen API and must include WinInet.h.

C++
#include <WinInet.h>

Using the Code

Using SYSTEMTIME for storing datatime when you get on server.

Systemtime Structure

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

First, clear output buffer by using SecureZeroMemory API.

C++
SYSTEMTIME sysTime;
SecureZeroMemory(&sysTime, sizeof(SYSTEMTIME));

Next, open connection by using InternetOpen API.

C++
HINTERNET hInternetSession = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);

Get hInternetFile by using InternetOpenUrl.

C++
HINTERNET hInternetFile = InternetOpenUrl(hInternetSession, 
L"http://learn-tech-tips.blogspot.com", 0, 0, 
INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE, 0);

Okie, done. You can query datetime in server learn-tech-tips.blogspot.com now.

C++
DWORD dwSize = sizeof(SYSTEMTIME);
if (!HttpQueryInfo(hInternetFile, HTTP_QUERY_DATE | 
HTTP_QUERY_FLAG_SYSTEMTIME, &sysTime, &dwSize, NULL))
{
    InternetCloseHandle(hInternetSession);
    InternetCloseHandle(hInternetFile);
    return sysTime;
}

Remember to clean up all resources when exiting the function, (I always forget it, hehe :), hope you don't do the same as me. :D)

C++
InternetCloseHandle(hInternetFile);
InternetCloseHandle(hInternetSession);

Note: You can easily view the parameters below from MSDN.

Source Code

C++
/*********************************************************************
Developer: zidane (huuvi168@gmail.com)
Last Modified: 2015-09-01
Blog: http://learn-tech-tips.blogspot.com/
*********************************************************************/

SYSTEMTIME CAutoPlayDlg::GetServerTime()
{
	
 	// Clear output buffer
 	SYSTEMTIME sysTime;
 	SecureZeroMemory(&sysTime, sizeof(SYSTEMTIME));

 	// Open connection
 	HINTERNET hInternetSession = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
 	if (!hInternetSession) 
 		return sysTime;

 	HINTERNET hInternetFile = InternetOpenUrl(hInternetSession, 
 	L"http://learn-tech-tips.blogspot.com", 0, 0,  
 	INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE, 0);

	
	if (!hInternetFile)
  	{
		InternetCloseHandle(hInternetSession);
   		return sysTime;
 	}

 	// Query date time in format systemtime
 	DWORD dwSize = sizeof(SYSTEMTIME);
 	if (!HttpQueryInfo(hInternetFile, HTTP_QUERY_DATE | 
 	HTTP_QUERY_FLAG_SYSTEMTIME, &sysTime, &dwSize, NULL))
	{
 		InternetCloseHandle(hInternetSession);
 		InternetCloseHandle(hInternetFile);
 		return sysTime;
	}
	
	// Clean up ...
 	InternetCloseHandle(hInternetFile);
 	InternetCloseHandle(hInternetSession);
	return sysTime;
}

If you want to understand more information on this code, you can visit this link.

Points of Interest

I like coding, I like researching new technology.

My tech blog is http://learn-tech-tips.blogspot.com.

My blog sharing experiences about technology tips and trick include: language programming: C+, C#, ....
Design skills: photoshop, Office: Excel, Outlook and .... other things!

If you have any feedback about This topic , please leave your comment, we can discuss about it. Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)