GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Apr-2009, 14:34
fireball fireball is offline
Awaiting Email Confirmation
 
Join Date: Jul 2007
Posts: 15
fireball is on a distinguished road

Create a system time, set it, and get it


I 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  
Old 08-Apr-2009, 15:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Time


Quote:
Originally Posted by fireball
I am trying to create a system time, set it, and get it.
.
.
.
In addition, I need to be able to set and get the milliseconds of this time.

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:
Originally Posted by fireball
Is there a simple way of doing that?

Well, "simple," like "beauty" or "evil" may be in the eye of the beholder.

Quote:
Originally Posted by fireball
Please let me know if you want to see what coding I have done so far.

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  
Old 09-Apr-2009, 08:44
fireball fireball is offline
Awaiting Email Confirmation
 
Join Date: Jul 2007
Posts: 15
fireball is on a distinguished road

Re: Create a system time, set it, and get it


Thanks 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:
struct timeval        theTime;

void SetTimeSecondsAndMilliseconds(uint32 TimeInSeconds, uint8 MilliSeconds)
{
    theTime.tv_sec = aTimeInSeconds;
    theTime.tv_usec = (aMilliSec * 1000);
    settimeofday(&theTime, NULL);
}

uint32 GetTimeInSeconds()
{  
   gettimeofday(&theTime, NULL);
   return(theTime->tv_sec);
}

uint8 GetTimeSubMilliSeconds()
{
   gettimeofday(&theTime, NULL);
   return((theTime->tv_usec)/1000);
}
  #4  
Old 09-Apr-2009, 10:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Create a system time, set it, and get it


Quote:
Originally Posted by fireball
...I am not sure if everything works fine

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:
Originally Posted by fireball
.... uint32 and uint8.
These are not standard data types. If you include <stdint.h> you can use the standard types uint32_t, uint8_t, etc. I highly recommend sticking to standards when possible. Not all compilers have <stdint.h>, but since you are apparently using GNU, it should be there.
Quote:
Originally Posted by fireball
.
I am not sure if I need to do conversions...

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:
uint32_t GetTimeInSeconds(struct timeval *tv);
uint8_t  GetTimeSubMilliSeconds(struct timeval *tv);

int main()
{
    struct timeval theTime;
    uint32_t seconds;
    uint8_t  milliseconds;

    gettimeofday(&theTime,NULL);

    seconds      = GetTimeInSeconds(&theTime);
    milliseconds = GetTimeSubMilliSeconds(&theTime);

    printf("Time = %d.%03d\n", seconds, milliseconds);
    return 0;
}

uint32_t GetTimeInSeconds(struct timeval *tv)
{  
   return (tv->tv_sec);
}

uint8_t GetTimeSubMilliSeconds(struct timeval *tv)
{
   return ((tv->tv_usec)/1000);
}

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  
Old 20-Apr-2009, 08:03
fireball fireball is offline
Awaiting Email Confirmation
 
Join Date: Jul 2007
Posts: 15
fireball is on a distinguished road

Re: Create a system time, set it, and get it


Thanks 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  
Old 20-Apr-2009, 08:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Create a system time, set it, and get it


Quote:
Originally Posted by fireball
...I have one question left and my guess the answer is...
Your guess is better than mine, because I don't know what kernel time is in this context.

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:
[ -x /sbin/hwclock ] && action $"Syncing hardware clock to system time" /sbin/hwclock $CLOCKFLAGS

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 GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 03:52.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.