//-- gcc (Debian 10.2.1-6) 10.2.1 20210110
//-- Debian 11.7
/*cut and paste and compile*/

#include <stdio.h>
#include <time.h>
typedef enum {
   false=0,
   true=1
}
predikaat;

static char *timestamp(predikaat bStamp){
//-------------------------------------
   static char stamp[22];

   time_t curtime;
   struct tm *gmtijd, *loctijd;

   curtime=time (NULL);
   gmtijd = gmtime (&curtime);
   loctijd = localtime(&curtime);
   if (bStamp==true){
      strftime (stamp, 22, "GM%Y%m%d%H%M%S", gmtijd );
   }
   else {
      strftime (stamp, 22, "%d/%m/%Y-%H:%M:%S",loctijd);
   }
   return stamp;
}
int main(void){
   printf("Look what happens when I call timestamp multiple times with 
different args within the same printf...\n"
          "Is this normal behaviour?\n"
          "I presume it has to do with the static variable?\n\n");

   printf("\ntime as stamp : %s",timestamp(true));

   printf("\ntime as human readable : %s",timestamp(false));

   printf("\nstamp=\n%s\n%s\n%s\n%s",
            timestamp(true),
            timestamp(false),
            timestamp(false),
            timestamp(true));

   printf("\nstamp=\n%s\n%s\n%s\n%s\n",
            timestamp(false),
            timestamp(true),
            timestamp(false),
            timestamp(true));

   return 0;
}

Reply via email to