![]() |
|
#1
|
|||
|
|||
Create a system time, set it, and get itI am new to C and linux.
I am trying to create a system time, set it, and get it. I need to be able to set the time as seconds epoch. I.e. a 32-bit value of seconds. The function will recieve the time as an uint32. I need to be able to get the time in seconds too. In addition, I need to be able to set and get the milliseconds of this time. I have looked for a way of doing that a lot. But, the more I look, the more confused I get I have looked at time_t and timeb. Milliseconds are in timeb but not in time_t. Is there a simple way of doing that? Please let me know if you want to see what coding I have done so far. Thanks, |
|||
|
#2
|
|||
|
|||
Re: TimeQuote:
In Linux/gcc distributions, look at the functions gettimeofday() and settimeofday(). Function prototypes on my Linux systems are in <sys/time.h>. Do "man gettimeofday" to see some details. The timeval struct has data members for seconds and microseconds since the Epoch. Quote:
Well, "simple," like "beauty" or "evil" may be in the eye of the beholder. Quote:
If you try these functions and have problems, show us the code and tell us what happened (or didn't happen). Heck, if you try them and they work as you expect, show us the code anyhow (if you feel like it). We come here to learn. Regards, Dave Footnote: Note that settimeofday() probably won't take effect unless you execute the program as root. In other words, I think it's a good idea to check the return values from the functions. If you have ntpd (the Network Time Protocol demon) running, having programs manually change system time may very well screw the pooch, so beware. |
|
#3
|
|||
|
|||
Re: Create a system time, set it, and get itThanks davekw7x for your reply. Here is what I have so far. I have not compiled yet. So I am not sure if everything works fine. I still have my doubts about the gets and sets since I am passing and getting uint32 and uint8. I am not sure if I need to do conversions or access value in the struct to assign and return as uint values.
Thanks again, fireball. CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: Create a system time, set it, and get itQuote:
1. Compile (I strongly recommend that you turn on compiler warnings by using -Wall -W on the command line. Maybe even throw in -pedantic.) 2. Make changes in source to try to get rid of all errors and warnings. 3. Repeat if necessary. Quote:
Quote:
These are all integer data types. The functions themselves are not part of the C standard library, but the man page for GNU gettimeofday() indicates that the timeval struct members have types of time_t and suseconds_t. Somewhere in the GNU documentation, these may be described, but all we really need to know is that they are integer data types large enough to hold values for system time in seconds and microseconds since the Epoch. See Footnotes. Even if it doesn't happen that time_t is a uint32_t, implicitly converting it to a uint32_t by an assignment statement (or as part of an expression in a return statement) will give valid results. Same for the microseconds member. Regards, Dave Footnote: I don't think using a global struct to do the deed is very good programming practice. I'm funny that way. I also don't think that extracting seconds and milliseconds from system time should be done with two separate function calls. The result could be off by a second if the call to get the seconds occurred just before the system time incremented to the next second and the millisecond call occurred just after. If I were going to use functions like yours, I would probably define the functions to have a timeval struct as an argument. Actually, I would almost certainly use a pointer to the struct to keep from unnecessarily copying the struct members at each function call. So my test program for getting current time might look like: CPP / C++ / C Code:
To set the time, I would just pass seconds and milliseconds and declare a local timeval struct with the corresponding member values. I would also probably define SetTimeSecondsAndMilliseconds() to have an integer return type and return the value from settimeofday(), so that the calling function could know whether the settimeofday() was successful. Next-to-last word: After Tue, Jan 19, 03:14:07 2038, we will no longer be able to express time in seconds since the Epoch as a 32-bit signed integer. Lots of programs and depend on this. Is it too early to start worrying about a potentially disastrous "Y2K038" problem? Just a thought. I know, I know, the overhyped Y2K problem never actually caused planes to crash or elevators to become stuck between floors, but... Absolutely the last word: For people building data bases with four decimal digits to represent the year, what about the "Y10K" problem? Has anyone addressed this yet? |
|
#5
|
|||
|
|||
Re: Create a system time, set it, and get itThanks davekw7x. Your help is vey much appreciated.
My code is a bit different than yours but the core is the same. And I am glad to tell you that everything works great. I have one question left and my guess the answer is yes. I tried to google it but couldn't find a confirming answer. Maybe because I not an expert I could not see the answer The question is does settimeofday() set the kernel time? I know it sets the system time. But is system time the kernel time? Thanks, fireball. |
|
#6
|
|||
|
|||
Re: Create a system time, set it, and get itQuote:
User programs (including sh, bash, etc., as well as programs compiled by your friendly C compiler) get their system time information from a system "something" that is affected by settimeofday(). Certain programs (like the uptime utility) keep their own counter that is initialized upon startup and they are not affected by the system time that you change with settimeofday(). Note that settimeofday() does not set the hardware clock (the time of day circuitry in desktop/laptop systems that suffers from the misnomer "Real Time Clock"), but, as far as I know, all desktop/laptop Linux systems are configured to set the RTC from system clock when they are shut down in the "normal" way. They may be configured to set system time from the hardware RTC upon startup. I almost always configure the systems to use the NTP (Network Time Protocol) daemon to try to get time of day from an NTP server if internet access is available at startup, and system time is set from that. For example on my Centos workstation, /etc/init.d/halt there is a line Code:
I think that all Linux systems have a hwclock utility that affects the hardware clock. You could try calling this from a user program that is being executed with root privileges. Try man hwclock and man adjtimex. (Hint: Try these utilities from a command line shell before trying to incorporate them into a program.) Over the years, different Linux distributions (and other UNIX-like distributions) have implemented certain system stuff like this differently, so, I must say that YMMV. (Your Mileage May Vary.) Regards, Dave |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| BryanSoft.Com Free Cpanel Hosting And Resellers One Time Posting!!! | rewer | Free Web Hosting | 0 | 22-Oct-2008 19:56 |
| Asynchronous transfer question | crystalattice | Miscellaneous Programming Forum | 2 | 24-Jan-2007 20:39 |
| constructors/classes | mapes479 | C++ Forum | 3 | 19-Nov-2006 17:34 |
| Fortran problem... | Justin Fox | Miscellaneous Programming Forum | 6 | 24-Oct-2006 15:30 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The