Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6904685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:05:30+00:00 2026-05-27T08:05:30+00:00

From /usr/include/time.h: /* Used by other time functions. */ struct tm { int tm_sec;.

  • 0

From /usr/include/time.h:

/* 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
};

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.

I currently have redefined the structure to the following:

//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
};

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…

EDIT: The int types I used in the above fix could be int32_t or whatever fixed width type chosen.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:05:31+00:00Added an answer on May 27, 2026 at 8:05 am

    Ensuring that data saved is written in a way that can be read back on the desired platforms would not qualify as a “hack”, in my opinion. Blindly saving out a (probably packed) structure in binary format and expecting to read it back easily and portably would, however.

    You need to handle each field separately, since the compiler might add padding between the fields which would appear in a “binary dump” of the struct, but that is completely compiler-dependent and thus very bad for interoperability between platforms.

    I would probably decide on a reasonable precision for the native int fields of the structure, say 32 bits, and write something like this:

    void tm_serialize(FILE *out, const struct tm *tm)
    {
      int32_serialize(out, tm->tm_sec);
      int32_serialize(out, tm->tm_min);
      /* and so on */
    }
    
    struct tm tm_deserialize(FILE *in)
    {
      struct tm tm;
      tm.tm_sec = int32_deserialize(in);
      tm.tm_min = int32_deserialize(in);
      /* and so on */
      return tm;
    }
    

    Where, of course, int32_serialize() and int32_deserialize() simply write (and read) a binary representation of the 32-bit integer in a known (such as big-endian) format which is well-defined and easy to read back on any platform.

    UPDATE: Serializing strings can of course be done in the exact same way, one popular format is the same as C’s in-memory layout, i.e. 0-terminated char array. Then you’d just have:

    void string_serialize(FILE *out, const char* string);
    int  string_deserialize(FILE *in, char *string, size_t max);
    

    Here, the string_deserialize() function must have a buffer size limit so it doesn’t overflow anything by readin too much. I imagined the return value to indicate success/failure, so the calling code can take whatever measures it wants to.

    In the above, I wasn’t aiming for a space-minimized serialization, as a commenter pointed out many of the fields that are int at run-time don’t really need all that precision, so they could be serialized into something smaller. If you wanted to do that, it would of course be trivial to invent appropriate functions like int8_serialize() and so on, and use the right one for each field.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is :: From a string like /usr/folder1/folder2/filename.ext I have to extract out
So I call this PHP script from the command line: /usr/bin/php /var/www/bims/index.php projects/output and
I have installed pylons based application from egg, so it sits somewhere under /usr/lib/python2.5/site-packages.
I am converting a linux script from http://www.perlmonks.org/index.pl?node_id=217166 specifically this: #!/usr/bin/perl -w use strict;
From time to time I see an enum like the following: [Flags] public enum
I am currently recompiling gtk+ and dependencies from source (I have no other choices).
the syntax for netif_napi_add is netif_napi_add(struct net_device *dev, struct napi_struct *napi,int (*poll)(struct napi_struct *,
i get message during archive validation. i've tried most of solutions from other questions.
I need to make a conversion from a general angular_velocity to degrees/second. To illustrate
I'm trying (for the first time) to use capifony to deploy my site from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.