questions about time structure in C

questions about time structure in C

Post by michel » Wed, 02 Jul 2003 08:34:01



Hi, for the program I am coding now, I need to write the time as a
String to a file, and later need to compare that recorded time with
the current time.  I have problem changing time from the string to the
unsigned int (to compare with t->tv_sec).  So what I did first is
that:

struct timeval* t = (struct timeval*) malloc (sizeof(struct timeval));
gettimeofday(t, NULL);
t->tv-sec +=sec; (Use sec(any seconds) to calculate the expired time)
fprintf(file, "expire at %s\n", ctime(&(t->tv_sec))). (so print to a
file about when it expries)

And then later I need to convert this string back to unsigned int to
compare with the current time.  Does anyone know how do I convert it
back if there is a way to do it? Thanks!

Michelle

 
 
 

questions about time structure in C

Post by Barry Margoli » Wed, 02 Jul 2003 08:47:03




>Hi, for the program I am coding now, I need to write the time as a
>String to a file, and later need to compare that recorded time with
>the current time.  I have problem changing time from the string to the
>unsigned int (to compare with t->tv_sec).  So what I did first is
>that:

>struct timeval* t = (struct timeval*) malloc (sizeof(struct timeval));
>gettimeofday(t, NULL);

Don't cast the result of malloc(); if you're getting warnings from the line
without the cast, you're not including the right header.

Quote:>t->tv-sec +=sec; (Use sec(any seconds) to calculate the expired time)
>fprintf(file, "expire at %s\n", ctime(&(t->tv_sec))). (so print to a
>file about when it expries)

>And then later I need to convert this string back to unsigned int to
>compare with the current time.  Does anyone know how do I convert it
>back if there is a way to do it? Thanks!

Why not write out the integer in addition to the human-readable form?  Then
you can just read the integer back in later.

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

questions about time structure in C

Post by Marc Rochkin » Wed, 02 Jul 2003 08:48:02



Quote:> Hi, for the program I am coding now, I need to write the time as a
> String to a file, and later need to compare that recorded time with
> the current time.  I have problem changing time from the string to the
> unsigned int (to compare with t->tv_sec).  So what I did first is
> that:
> struct timeval* t = (struct timeval*) malloc (sizeof(struct timeval));
> gettimeofday(t, NULL);
> t->tv-sec +=sec; (Use sec(any seconds) to calculate the expired time)
> fprintf(file, "expire at %s\n", ctime(&(t->tv_sec))). (so print to a
> file about when it expries)

> And then later I need to convert this string back to unsigned int to
> compare with the current time.  Does anyone know how do I convert it
> back if there is a way to do it? Thanks!

> Michelle

To convert from a string, you can use one of two functions: getdate or
strptime. (The latter is probably the better choice when the string is
computer generated, as is the case here.) They give you a tm structure
(broken-down time), which you can convert to a time_t with mktime.

Also, no need to use malloc to get a suitable timeval structure. Instead,
this is easier:

   struct timeval tv;
   gettimeofday(&tv, NULL);

--Marc

 
 
 

1. Browser Question (Plug-ins) m-w.com

Hi,

I'm trying to get my netscape and/or konqueror to work with the new
sound feature on the Merriam Webster web site (http://www.m-w.com),
but all I get (in netscape) is the plug-in missing window that
redirects me to the netscape web site, which isn't helpful at all.

Interestingly, if I click on regular links to *wav files, my netscape
interpretes them fine and they get played.

Thanks

Wroot

2. how do I mount the diskette

3. Structures in structures

4. fonts random sizes each reboot

5. Subject: Ksh93 question: special built-ins: why are they special?

6. FTP Doesn't Work!

7. Firwall/forward/redierct/ipchains question

8. How to move data from one server to another?

9. How to transmit data without knowing it's structure at compile time

10. WinFS delayed, this probably means more time for semi-structured

11. convert UT time in local time / local time in UT time

12. time time time how can I tell the time

13. A question about a structure containing ':'