> If you want already-available code. Here you are for YYYYMMDDHHMMSS,
> with nondigits ignored. Not that it was hard to write off the top of
> my head:
>
> ----- C/C++:

If you want already-available code, why reinvent the wheel?  For example, it
makes me mad when I see other people writing their own ascii-to-number
conversion.
It's a liability and a focus for bugs, and this function is SO simple that
any bugs in it would be embarrassing.  (Also your code will behave
unpredictably on C++ as the tm_isdst member isn't being set.)

I do a lot of embedded programming and recently found nearly ten
almost-identical functions for converting integers into ASCII strings, each
one slightly different with slightly different bugs in.  However, for the
embedded application in question, we didn't have a C-Runtime library to help
us out, and all functions were in straight assembler.  But when you have a
CRT why deliberately avoid using it?!  May I suggest instead:

time_t iso2unix(char *iso)
{
  // It is good practice to initialise all unused members of a struct.
  // A side effect of the following is that it sets t.tm_isdst to zero which
is probably
  // what we'd want in this situation.
  struct tm t = {0};
  unsigned short Y,M,D,h,m,s;
  sscanf(iso,
   "%4hd%2hd%2hd%2hd%2hd%2hd",
   &Y,&M,&D,&h,&m,&s);
  t.tm_year = Y-1900;
  t.tm_mon  = M - 1;
  t.tm_mday  = D;
  t.tm_hour = h;
  t.tm_min  = m;
  t.tm_sec  = s;
  return mktime(&t);
}

Dave


_______________________________________________
Devl mailing list
Devl at freenetproject.org
http://lists.freenetproject.org/mailman/listinfo/devl

Reply via email to