0

I'm building my own unix time to human readable conversion, and I'm stuck.

I can extract the year just fine, but the day is proving too tricky.

/*
   Converts a 32-bit number of seconds after 01-01-1970 to a _SYSTEMTIME structure
*/
static _SYSTEMTIME Convert(unsigned long a_UnixTime)
{   
    newtime.wMilliseconds = 0;
    newtime.wYear   = (unsigned long)((float)a_UnixTime / (364.24f * 24.f * 60.f * 60.f));
    newtime.wDay    = (a_UnixTime - (unsigned long)((float)newtime.wYear * 364.24f * 24.f * 60.f * 60.f)); // returns 65177

    return newtime;
}

Or is there a built-in function that I've overlooked?

Thanks in advance.

UPDATE:

Hmm... it seems Windows Mobile doesn't support strftime, time or localtime, so I'll still have to roll my own. :(

knight666
  • 1,599
  • 3
  • 22
  • 38
  • Even seconds will be hard if you take into account leap seconds. – Martin York Oct 29 '09 at 19:20
  • PS. There have been 24 since 1970 – Martin York Oct 29 '09 at 19:21
  • if Windows Mobile doesn't have strftime, I'd look for something else it might have... I suppose it's possible you'll really have to roll your own, but it strikes me as something they've surely provided *somehow*... no? (Note: I have zero experience with Windows Mobile, either as a user or as a dev.) – lindes Feb 23 '11 at 08:26

2 Answers2

2

Are you looking for gmtime?

struct tm * gmtime(const time_t *clock);

External declarations, as well as the tm structure definition, are contained in the <time.h> include file.  The tm structure includes at least the following fields:

       int tm_sec;     /* seconds (0 - 60) */
       int tm_min;     /* minutes (0 - 59) */
       int tm_hour;    /* hours (0 - 23) */
       int tm_mday;    /* day of month (1 - 31) */
       int tm_mon;     /* month of year (0 - 11) */
       int tm_year;    /* year - 1900 */
       int tm_wday;    /* day of week (Sunday = 0) */
       int tm_yday;    /* day of year (0 - 365) */
       int tm_isdst;   /* is summer time in effect? */
       char *tm_zone;  /* abbreviation of timezone name */
       long tm_gmtoff; /* offset from UTC in seconds */
diciu
  • 29,133
  • 4
  • 51
  • 68
1

If you want to format to print, you need strftime(), it's the standard solution, used together with e.g. localtime() to convert the raw timestamp to a more human-friendly format.

unwind
  • 391,730
  • 64
  • 469
  • 606