title: C/C++: Reading and writing time stamp data to file with multi architecture support
body: <p>From /usr/include/time.h:</p>
  
  <pre><code>/* Used by other time functions. */
  struct tm
  {
  int tm_sec;. . . /* Seconds..[0-60] (1 leap second) */
  int tm_min;. . . /* Minutes..[0-59] */
  int tm_hour;. . . /* Hours.. [0-23] */
  int tm_mday;. . . /* Day... [1-31] */
  int tm_mon;. . . /* Month.. [0-11] */
  int tm_year;. . . /* Year.- 1900. */
  int tm_wday;. . . /* Day of week..[0-6] */
  int tm_yday;. . . /* Days in year.[0-365].*/
  int tm_isdst;.. . /* DST... [-1/0/1]*/
  
  #ifdef. __USE_BSD
  long int tm_gmtoff;. . /* Seconds east of UTC. */
  __const char* tm_zone;. / Timezone abbreviation. */
  #else
  long int __tm_gmtoff;.. /* Seconds east of UTC. */
  __const char* __tm_zone;. / Timezone abbreviation. */
  #endif
  };
  </code></pre>
  
  <p>If you want to write this structure to a file, and you want your program to read it back and to have multi-arch support (ie 32 bit version writes it, 64 bit version reads it), you'll have to do some hacks to make sure that it's the same size for each system. Does anyone know of a better way to keep time stamps that are architecture independent? Eg, I want to be able to write some structure eg, time_t, or struct tm to a file and read it back for any architecture. Does anyone have any experience or advice for this? Is struct tm even the best way to store a time stamp in C/C++? I realize there's a fair bit of overhead using this structure.</p>
  
  <p>I currently have redefined the structure to the following:</p>
  
  <pre><code>//Force 32 bit format and space requirements
  struct tm32
  {
  int tm_sec;. . . /* Seconds..[0-60] (1 leap second) */
  int tm_min;. . . /* Minutes..[0-59] */
  int tm_hour;. . . /* Hours.. [0-23] */
  int tm_mday;. . . /* Day... [1-31] */
  int tm_mon;. . . /* Month.. [0-11] */
  int tm_year;. . . /* Year.- 1900. */
  int tm_wday;. . . /* Day of week..[0-6] */
  int tm_yday;. . . /* Days in year.[0-365].*/
  int tm_isdst;.. . /* DST... [-1/0/1]*/
  
  int tm_gmtoff; // this does nothing but hold space
  int tm_zone; // this does nothing but hold space
  };
  </code></pre>
  
  <p>Since the functions (mktime(), time(), gmtime(), strftime()) I'm using to manipulate the struct tm don't seem to even look at the last two fields in the struct tm structure, I simply use integers as place holders in their positions so that the size will always be the same. However, I'm still looking for a better solution to this...</p>
  
  <p>EDIT: The int types I used in the above fix could be int32_t or whatever fixed width type chosen. </p>