Click here to Skip to main content
15,357,293 members
Articles / Desktop Programming / MFC
Article
Posted 23 Feb 2002

Stats

119.7K views
1.8K downloads
25 bookmarked

Time Client (RFC868)

Rate me:
Please Sign up or sign in to vote.
2.88/5 (5 votes)
6 Apr 20022 min read
Learn how to synchronize your date and time

Introduction

This article describes a time client which uses RFC 868 protocol. The program uses TCP port 37 for connection with a time server. This is a MFC based console application.

The following block calculates current date and time.

C++
lnTime = (long)((sBuffer - (unsigned long)(DATE_1970)));
Time_Int64 = Int32x32To64(lnTime, 10000000) + 116444736000000000;
FileTime.dwLowDateTime = (DWORD)Time_Int64;
FileTime.dwHighDateTime = Time_Int64 >> 32;
FileTimeToLocalFileTime(&FileTime, &LocalFileTime);
FileTimeToSystemTime(&LocalFileTime, &SystemTime);
SetLocalTime( &SystemTime );

The time is calculated and converted to 64-bit in the first and second lines and then converted to local time zone before being converted to a SYSTEMTIME structure. Last of all, the system time is set.

The following is the procedure for communicating with the time server. (These notes are copied from http://www.faqs.org/rfcs/rfc868.html.)

  1. S: Listen on port 37 (45 octal)
  2. U: Connect to port 37
  3. S: Send the time as a 32 bit binary number
  4. U: Receive the time
  5. U: Close the connection
  6. S: Close the connection

The server listens for a connection on port 37. When the connection is established, the server returns a 32-bit time value and closes the connection. If the server is unable to determine the time at its site, it should either refuse the connection or close it without sending anything. The most important thing is, the time is the number of seconds since 00:00 (midnight) 1 January 1900 GMT, such that the time 1 is 12:00:01 am on 1 January 1900 GMT; this base will serve until the year 2036.

For example:

  • 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
  • 2,398,291,200 corresponds to 00:00 1 Jan 1976 GMT
  • 2,524,521,600 corresponds to 00:00 1 Jan 1980 GMT
  • 2,629,584,000 corresponds to 00:00 1 May 1983 GMT
  • and -1,297,728,000 corresponds to 00:00 17 Nov 1858 GMT.

You can use the time.ien.it time service for testing (settime time.ien.it). I hope you have enjoyed with my time client. You can send any bug or comments below.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

Share

About the Author

Orkun GEDiK
Software Developer (Senior) ASTRON
Turkey Turkey
I'm working at Yasar Holding IT department as senior SAP R/3 BASIS and Development Consultant and system integrator. I have experience on C/C++ for 6 years. Implementing and inviting new technologies is most biggest part of my life. Object oriented paradigm is a philosophy of my vision.

Comments and Discussions

 
Generalstrange result!!! Pin
songgunyoung1-Sep-10 23:02
Membersonggunyoung1-Sep-10 23:02 
GeneralRe: strange result!!! Pin
songgunyong2-Sep-10 0:11
Membersonggunyong2-Sep-10 0:11 
GeneralDialog + separate thread Pin
bruno voisin20-Sep-03 6:17
Memberbruno voisin20-Sep-03 6:17 
GeneralNeed More Help Pin
mjdeibert14-May-03 4:04
Membermjdeibert14-May-03 4:04 
GeneralRe: Need More Help Pin
Orkun GEDiK21-May-03 1:43
MemberOrkun GEDiK21-May-03 1:43 
GeneralCan't get the files to compile Pin
niverson16-Aug-02 10:11
Memberniverson16-Aug-02 10:11 
GeneralRe: Can't get the files to compile Pin
oged16-Aug-02 11:20
Memberoged16-Aug-02 11:20 
GeneralRe: Can't get the files to compile Pin
niverson16-Aug-02 12:16
Memberniverson16-Aug-02 12:16 
GeneralSlight Bug Pin
John Burton8-Apr-02 2:19
MemberJohn Burton8-Apr-02 2:19 
GeneralRe: Slight Bug Pin
Orkun GEDiK8-Apr-02 9:10
MemberOrkun GEDiK8-Apr-02 9:10 
GeneralSomething wrong Pin
Davide Zaccanti7-Apr-02 5:29
MemberDavide Zaccanti7-Apr-02 5:29 
GeneralRe: Something wrong Pin
Orkun GEDiK7-Apr-02 9:46
MemberOrkun GEDiK7-Apr-02 9:46 
GeneralList of timeservers Pin
2-Mar-02 0:02
suss2-Mar-02 0:02 
GeneralRe: List of timeservers (correction) Pin
David Crow6-Apr-04 16:14
MemberDavid Crow6-Apr-04 16:14 
GeneralSimple but useful example Pin
26-Feb-02 16:07
suss26-Feb-02 16:07 
GeneralRe: Simple but useful example Pin
Orkun GEDiK27-Feb-02 1:04
MemberOrkun GEDiK27-Feb-02 1:04 
GeneralCut/Paste Pin
Todd Jeffreys24-Feb-02 10:14
MemberTodd Jeffreys24-Feb-02 10:14 
GeneralRe: Cut/Paste Pin
Orkun GEDiK25-Feb-02 10:32
MemberOrkun GEDiK25-Feb-02 10:32 
GeneralRe: Cut/Paste Pin
Pierre Couderc7-Apr-02 22:43
MemberPierre Couderc7-Apr-02 22:43 
GeneralRe: Cut/Paste Pin
Orkun GEDiK8-Apr-02 9:11
MemberOrkun GEDiK8-Apr-02 9:11 

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.