[lug] OT: My own C/C++ mix Q

Tkil tkil at scrye.com
Wed Apr 17 14:20:28 MDT 2002


>>>>> "DS" == D Stimits <stimits at idcomm.com> writes:

DS> Ok, I have some legacy C (and you thought C didn't have
DS> inheritance...heheh) being compiled under g++. One of the legacy
DS> code snippets uses strftime(), and all it is used for is to format
DS> the year.  This warning always generates:

DS>   warning: `%c' yields only last 2 digits of 
DS>   year in some locales

DS> Does anyone know if there is a newer method of formatting time and
DS> getting year out of "struct tm", the underlying struct producing
DS> the unformatted time data, such that it does not issue this
DS> warning (and I won't turn off warnings, I am looking to see if
DS> there was something new created to deal with that locale warning)?

well, if you can use a four-digit year, do so.  ("%Y" formatter.)

if you can't, then there's no way to shut up the warning.  there was a
big stink about this on the gcc list only a month or two ago.  (pick
your position:  (1) it's always wrong; (2) i don't care, i'm the
programmer, i know what i'm doing; (3) legacy output formats require
the two-digit year; (4) windowing is easy, what y2k stink?)

you could extract the value manually, mod it by 100, then format with
"%02d" using snprintf:

   char year[3];
   time_t t;
   struct tm *tp;

   t = time();
   tp = localtime(t); /* thread unsafe, use localtime_r */
   assert(tp);
   snprintf(year, 3, "%02d", tp->tm_year%100);

or similar.

t.



More information about the LUG mailing list