Hi David,
 
OK, please consider following changed code.
I accept you criticism concerning thread safe issue.
Concerning 'len', see comments inthe edge of the function.

typedef struct snmp_data_and_time_s {
  unsigned short year;
  unsigned char  month;
  unsigned char  day;
  unsigned char  hour;
  unsigned char  minutes;
  unsigned char  seconds;
  unsigned char  deci_seconds;
  unsigned char  direction_from_UTC;
  unsigned char  hours_from_UTC;
  unsigned char  minutes_from_UTC;
} DT_T;
 
size_t AGUTIL_get_date_and_time (time_t * tv,
                                 DT_T * dtt)
{
  unsigned short iday;
  struct tm * tm_data = gmtime(tv);
  iday = tm_data->tm_year + 1900;
  dtt->year = htons(iday);
  dtt->month = 1 + tm_data->tm_mon;
  dtt->day = tm_data->tm_mday;
  dtt->hour = tm_data->tm_hour;
  dtt->minutes = tm_data->tm_min;
  dtt->seconds = tm_data->tm_sec;
  dtt->deci_seconds = 0;
#if 1 /* I know only localtime */
  return 8;
#else /* if I know not only localtome */
  /* Calculate direction_from_UTC, hours_from_UTC & minutes_from_UTC */
  return 11;
#endif
}
Regards, Beth

"David T. Perkins" wrote:
HI,

Your example below is not thread-safe. I suggest that you
change it to pass in a pointer to a place to store the
result instead of using static "dtt" in the function
(and remove the "len" argument, since it is not needed).

That is, change the function signature to:

DT_T *AGUTIL_get_date_and_time(DT_T *buf, time_t *tv)

By the way, even if yout program is not using threads,
it's a good thing to write all of your code to be
thread-safe, since someone else may want to use it
in a threaded environment, and if a signal handler
gets control, you may have the same issues as with
threading.



Find local movie times and trailers on Yahoo! Movies.

Reply via email to