0

For an error-logbook I want to create a timestamp with date and time, including microseconds. It should have the form 2016.07.19 13:59:31:123.456

I found a lot of examples of time_t, but the resolution is only seconds...

dbush
  • 205,898
  • 23
  • 218
  • 273
nobody
  • 23
  • 3
  • 1
    The microchip tag is likely misplaced. This question is unrelated to microchip pic microcontrollers. – Mathieu L. Jul 19 '16 at 12:57
  • You're looking for a _high resolution timer_. Recommend taking a look at: http://stackoverflow.com/questions/6749621/how-to-create-a-high-resolution-timer-in-linux-to-measure-program-performance – Jonathon Ogden Jul 19 '16 at 14:58

1 Answers1

1

You can use gettimeofday:

#include <time.h>
#include <sys/time.h>
....
struct timeval tv;
gettimeofday(&tv, NULL);

Where struct timeval is defined as:

struct timeval {
    time_t      tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};

You can then use gmtime to split the seconds part:

struct tm *ts = gmtime(&tv.tv_sec);

Where struct tm is defined as:

          struct tm {
              int tm_sec;         /* seconds */
              int tm_min;         /* minutes */
              int tm_hour;        /* hours */
              int tm_mday;        /* day of the month */
              int tm_mon;         /* month */
              int tm_year;        /* year */
              int tm_wday;        /* day of the week */
              int tm_yday;        /* day in the year */
              int tm_isdst;       /* daylight saving time */
          };

The members of the tm structure are:

  • tm_sec The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds.

  • tm_min The number of minutes after the hour, in the range 0 to 59.

  • tm_hour The number of hours past midnight, in the range 0 to 23.

  • tm_mday The day of the month, in the range 1 to 31.

  • tm_mon The number of months since January, in the range 0 to 11.

  • tm_year The number of years since 1900.

  • tm_wday The number of days since Sunday, in the range 0 to 6.

  • tm_yday The number of days since January 1, in the range 0 to 365.

  • tm_isdst A flag that indicates whether daylight saving time is in effect at the time described. The value is positive if daylight saving time is in effect, zero if it is not, and negative if the infor- mation is not available.

EDIT:

Windows doesn't have gettimeofday. Here's an implementation you can use:

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
    const unsigned __int64 epoch_diff = 11644473600000000;
    unsigned __int64 tmp;
    FILETIME t;

    if (tv) {
        GetSystemTimeAsFileTime(&t);

        tmp = 0;
        tmp |= t.dwHighDateTime;
        tmp <<= 32;
        tmp |= t.dwLowDateTime;

        tmp /= 10;
        tmp -= epoch_diff;
        tv->tv_sec = (long)(tmp / 1000000);
        tv->tv_usec = (long)(tmp % 1000000);
    }

    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • "The members of the tm structure are:" describes the _minimum_ set of members. The C spec uses "The tm structure shall contain at least the following members ..." `gettimeofday()` is not a standard library function. C does have `timespec_get()`, yet I am uncertain of its popularity. – chux - Reinstate Monica Jul 19 '16 at 14:05
  • Thanks for your reply. – nobody Jul 25 '16 at 11:18
  • @nobody Glad I could help. Feel free to [accept this answer](http://stackoverflow.com/help/accepted-answer) if you found it useful. – dbush Jul 25 '16 at 11:19
  • Sorry, I pressed the Enter buttom and my comment was sent already. I wanted to write: Thanks for your reply. I'm using a Windows notebook and a XC16 compiler. I don't have the . Where can I get it and how to install it? – nobody Jul 25 '16 at 11:23
  • @nobody Windows doesn't have `gettimeofday`, however I included an implementation you can use. – dbush Jul 25 '16 at 15:43