Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > questions about time structure in C

Reply
Thread Tools

questions about time structure in C

 
 
michelle
Guest
Posts: n/a
 
      06-30-2003
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
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      07-01-2003
On 30 Jun 2003 16:32:40 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (michelle)
wrote in comp.lang.c:

> 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);


In addition to what Clint correctly said, because neither "struct
timeval" nor "gettimeofday()" are part of C, don't ever cast the
return value of malloc. As long as a definition of struct timeval is
in scope and you are indeed using C, not C++, do this:

struct timeval *t = malloc(sizeof *t);

If your compiler complains about the assignment, include <stdlib.h>.
If it still complains, you are compiling a C++ program and you are in
the wrong newsgroup.

> 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)


The ctime() function accepts a pointer to a time_t type, so if the
tv_sec member of a struct timeval is not a time_t, you have undefined
behavior.

> 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


C does not require or guarantee that a time_t can be converted to or
from an unsigned int without loss. Even POSIX does not require that a
time_t be an integer type, it could be a double.

Your program uses non-standard functions and makes non-standard
assumptions. You really need to take it to a platform specific group.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What data structure can let me map from A to B one time, B to A another time? Andreas Leitgeb Java 1 04-08-2008 02:41 PM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Memory allocation in Structure to Structure pra_ramli@rediffmail.com C++ 2 03-09-2006 05:51 AM
Copy String structure "A" to string structure "B" Leo Nunez C Programming 3 02-09-2005 05:14 AM
Pointers to structure and array of structure. Excluded_Middle C Programming 4 10-26-2004 05:39 AM



Advertisments