Click here to Skip to main content
15,891,375 members
Articles / Programming Languages / VC++
Tip/Trick

Get Server Side Data-time with C++

Rate me:
Please Sign up or sign in to vote.
1.92/5 (4 votes)
17 Oct 2015CPOL1 min read 24.4K   5   5
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)


Written By
Technical Lead
Vietnam Vietnam
---------------------------------------------------
Thanks for code project, I joined code project's community in the begin of the year 2008. Code project helped me so much when I still a student.

Now I had working about 14 years (2023). Let Code project plentiful as thank its. So I decide sharing my little knowledge for help another students and developers.

Here is my technical blog: https://learn-tech-tips.blogspot.com
I will share many knowledge about Application, Web, Game, Programming languages C#, C++, Web(HTML, CSS, javascript). Office (Excel, Photoshop) and another useful things Smile | :)

---------------------------------------------------
I'm developer, I like code,
I like to learn new technology and want to be friend with people to improve my skills Smile | :)

Thanks and Best Regards!
Zidane (huuvi168@gmail.com)
https://learn-tech-tips.blogspot.com

Comments and Discussions

 
GeneralThoughts Pin
PIEBALDconsult17-Oct-15 5:37
mvePIEBALDconsult17-Oct-15 5:37 
GeneralRe: Thoughts Pin
zidane16817-Oct-15 21:23
zidane16817-Oct-15 21:23 
GeneralRe: Thoughts Pin
PIEBALDconsult18-Oct-15 4:06
mvePIEBALDconsult18-Oct-15 4:06 
AnswerRe: Thoughts - Thanks Pin
zidane16818-Oct-15 16:31
zidane16818-Oct-15 16:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.