mac-can / PCBUSB-Library

macOS® User-Space Driver for PCAN-USB Interfaces (Binaries only)
https://mac-can.com
Other
29 stars 0 forks source link

Question: Epoch for CAN Message Timestamp #1

Closed willson556 closed 2 years ago

willson556 commented 3 years ago

Hi,

I'd like to convert the CAN message timestamps to real time (UTC). What epoch is being used for the timestamps?

On Windows, with the PCAN-Basic API, the timestamps are seconds since boot. However, on macOS, I tried using kern.boottime from sysctl() to get the boot time and add that to the timestamps. That didn't give correct times -- it seemed that the timestamps were using a later epoch (possibly first call into the library?).

Thanks in advance for any assistance!

mac-can commented 3 years ago
. . .
    if (usbTimestamp->firstTime)
    {
        host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &clock_ref);
        clock_get_time(clock_ref, &sys_time);
        usbTimestamp->startTime = ((int64_t)sys_time.tv_sec * 1000000LL) + (int64_t)(sys_time.tv_nsec / 1000);
        usbTimestamp->startWordValue = word;
        usbTimestamp->lastWordValue = 0;
        usbTimestamp->lastByteValue = 0;
        usbTimestamp->ticksCounter = word;
        usbTimestamp->old.ticksCounter = word;
        usbTimestamp->old.lastWordValue = word;
        usbTimestamp->firstTime = 0;

        pcan_log_x_printf("     ts(start) = %lluusec\n", usbTimestamp->startTime);
    }
. . .

It's been too long that I remember where I got this code. No documentation of host_get_clock_service available anymore.

I´m not happy both with Peak's as with my decision.

willson556 commented 3 years ago

Just getting back to this project. Is there any chance this might get changed (possibly via an option of some sort) so that the timestamps will use an epoch that doesn't depend on when the first message is received? Either that or somehow expose the usbTimestamp->startTime property.

Without one of those it doesn't seem possible to get an accurate absolute timestamp on macOS.

Thanks!

mac-can commented 3 years ago

My comment of 2/7/21 is missing a piece of information (I suspect it was a copy/paste error):

To convert a timestamp into a real time (UTC) you have to add the difference between the calendar clock and the system clock to the timestamp of a CAN message:

    clock_serv_t clock_ref;
    mach_timespec_t sys_time;
    mach_timespec_t cal_time;
    time_t offset;

    host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &clock_ref);
    clock_get_time(clock_ref, &sys_time);

    host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &clock_ref);
    clock_get_time(clock_ref, &cal_time);

    offset = (time_t)cal_time.tv_sec - (time_t)sys_time.tv_sec;

Remark: I have omitted the nanoseconds (field tv_nsec) in this example.

I hope that helps.

mac-can commented 2 years ago

Just getting back to this project. Is there any chance this might get changed (possibly via an option of some sort) so that the timestamps will use an epoch that doesn't depend on when the first message is received? Either that or somehow expose the usbTimestamp->startTime property.

Without one of those it doesn't seem possible to get an accurate absolute timestamp on macOS.

Thanks!

@willson556: Hi, can this Issue be closed?