There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox firewall format freezes freezing hard drive hardware hijackthis internet internet explorer itunes laptop malware motherboard mouse network networking outlook outlook 2007 power printer problem ram router screen slow sound trojan usb virus vista vista 32-bit windows windows xp winxp wireless
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
TimeClient in C


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
WhiteTree's Avatar
Junior Member with 18 posts.
 
Join Date: May 2004
29-May-2004, 12:49 AM #1
TimeClient in C
Hello,

Iam trying to create a timeclient that connects to a timeserver on port 37, sends an empty datagram and recieves the time from that server. When I recieve the time from the server (an integer representing the number of seconds passed since midnight january 1st 1900) I need to subtract 2208988800 (70 years worth of seconds) so I can use the ctime function in C (which displays the time since 1970).

Its quite simple, however, the problem is that the ctime function displays my time(the current time of my PC) not the time of the timeserver.
How do I display the time of the timeserver (not my current time)? Also anyway to change the display of the time from army time (24 oclock) to 12 hour time?

Thanks.
Regicide's Avatar
Senior Member with 304 posts.
 
Join Date: Aug 2003
Experience: Advanced
29-May-2004, 04:05 PM #2
First off, what OS are you dealing with? Secondly, what you might want to try it to send the time as string and then recieve it and use the atoi() function to get it into an integer. Am I correct to say that you are using UDP? Once you get it into a time format, you can simply have a if statement for converting to 12 hours time:
Code:
int hour;
bool pm;
if(hour > 12)
{
hour -= 12;
pm = true;
} esle {
pm = false;
}
Hope this helps.
__________________
When you give everything and the feeling is gone
All the giving is fake, did you fake enough?
When you take everything and you stay too long
When there's nothing left, did you take enough?
WhiteTree's Avatar
Junior Member with 18 posts.
 
Join Date: May 2004
29-May-2004, 06:04 PM #3
Yes, Im using UDP. Im also working in Windows but using Xwindows. However, the way a timeserver works is that the client sends an empty datagram (UDP) and the Time server then responds by sending an integer representing the number of seconds since midnight Jan 1st 1900. Now the ctime function in C returns the number seconds since midnight Jan 1 1970! and also returns it in a string format (Wed Jan 5 etc....). To make the ctime function work you need to convert the number recieved from the timeserver (seconds since 1900) to midnight jan 1st 1970 (you simply subtract the number of seconds in 70 years). This is all well and good, but for some reason the converted time is my system time! not the time server time. Its really annoying. Lets say Its 1 pm on the west, if I contact a timeserver in the east, I should get back 4 pm ( a 3 hour differenct), but nope, I recieve 1 pm.
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
29-May-2004, 06:16 PM #4
Well, the number of seconds since 1970 (or 1900) is still the same, whether or you are on the East coast, the West coast or anywhere else; it's how we interpret them that counts. You might consider adjusting the result to the timezone of the server that you connect with, if this is really what you intend ...

Note that the ctime function returns the local time based on a calendar interval; as long as you use this function, you will receive the local time.

Hope this helps.
__________________
The slowest component still sits at the keyboard.
WhiteTree's Avatar
Junior Member with 18 posts.
 
Join Date: May 2004
29-May-2004, 06:42 PM #5
Thanks that cleared up the reason for it always displaying my time.

But do you know of any function that simply displays the time as a string without converting it to the local time?

thanks

Last edited by WhiteTree : 29-May-2004 07:15 PM.
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
30-May-2004, 11:14 AM #6
The "time" is always local -- i.e., relative to some location/time zone. The same moment that is 9:00 A.M. in New York is 6:00 A.M. in California, so a string such as "9:00 A.M. May 29, 2004" is ambiguous, at best -- which is why the server reports an elapsed interval (number of seconds since some reference point) instead. The interval can be converted to the "local" equivalent; if the server reported its "local time" (i.e., did the conversion), you would have to know the time zone/location of the server in order to interpret the result.

If you really want to know the local time at the server when the request is made, you will need to adjust the reported value manually, based on the difference between your time zone and that of the server, and then use the ctime function to produce an ascii string.

Hope this helps.
__________________
The slowest component still sits at the keyboard.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 02:37 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.