title: Correcting timestamp on C code log file
body: <p>I'm currently programming my Raspberry Pi (model B) with a DHT11 temperature &amp; humidity sensor and lcd display. I have written a C script which logs values obtained from the temperature sensor to a status file which is then added to a log file. The script is as follows:</p>
  
  <pre><code>#include &lt;time.h&gt;
  
  #define LOGFILE "/var/log/temp.log"
  #define CURRENTFILE "/var/temp.data"
  
  
  
  /* Saves the date time and humidity to a log file and current file */
  
  
  void write_value (int temp, int humidity) {
  
      time_t current_time;
      current_time = time(NULL);
  
      /* Write to log file */
      FILE *logfd;
      logfd = fopen (LOGFILE, "a");
      fprintf (logfd, "%ld %d %d\n", current_time, temp, humidity);
      fclose (logfd);
  
      /* Write to current file */
      FILE *currfd;
      currfd = fopen(CURRENTFILE, "w");
      fprintf (currfd, "%ld %d %d\n", current_time, temp, humidity);
      fclose (currfd); 
  
  }
  </code></pre>
  
  <p>It works, however; the output I get in the log file is as follows:</p>
  
  <pre><code>1428539174 16 41
  1428539232 17 40
  1428539257 18 40
  1428539304 19 39
  1428539319 19 39
  </code></pre>
  
  <p>With the first row intended to be the date &amp; time stamp, the temperature, and the humidity.</p>
  
  <p>Do you have any suggestions as to how I could fix the date &amp; time, as well as changing it to a DD/MM/YYYY HH:MM:SS (Day/Month/Year Hour:Min:Sec) format?</p>
  
  <p>Based on suggestions from @pmg the script was changed to:</p>
  
  <pre><code>#include &lt;time.h&gt;
  
  #define LOGFILE "/var/log/temp.log"
  #define CURRENTFILE "/var/temp.data"
  
  
  
  /* Saves the date time and humidity to a log file and current file */
  
  
  void write_value (int temp, int humidity) {
  
      char dt[20]; // space enough for YYYY-MM-DD HH:MM:SS and terminator
      struct tm tm;
      time_t current_time;
      current_time = time(NULL);
      tm = *localtime(&amp;current_time); // convert time_t to struct tm
      strftime(dt, sizeof dt, "%Y-%m-%d %H:%M:%S", &amp;tm); // format
  
      /* Write to log file */
      FILE *logfd;
      logfd = fopen (LOGFILE, "a");
      fprintf (logfd, "%s %d %d/n", dt, temp, humidity);
      fclose (logfd);
  
      /* Write to current file */
      FILE *currfd;
      currfd = fopen(CURRENTFILE, "w");
      fprintf (currfd, "%s %d %d/n", dt, temp, humidity);
      fclose (currfd); 
  
  }
  </code></pre>