0

I use mktime64 to convert a clock time into a jiffies value.

// year, mon, day, hour, min, sec
unsigned long my_jiffies = mktime64(2020, 2, 24, 3, 2, 50);

The output from the above code is: 1582513370

  • How can I convert that jiffies value back to clock time?
div Mi
  • 13
  • 4
  • Does this answer your question? [Converting jiffies to milli seconds](https://stackoverflow.com/questions/2731463/converting-jiffies-to-milli-seconds) – RafsanJany Feb 24 '21 at 13:31
  • `mytime64` converts the parameter values to seconds since 1970-01-01 00:00:00, not jiffies. There are `HZ` jiffies per second, but the `jiffies` counter does not count time since 1970-01-01 00:00:00. – Ian Abbott Feb 24 '21 at 15:11
  • The `jiffies` counter counts system ticks and is set to some initial value during kernel boot that is unrelated to real world time. Besides, on 32-bit systems it wraps around about every 49710/HZ days although the first wraparound is usually set to occur within a few minutes. – Ian Abbott Feb 24 '21 at 15:23

1 Answers1

1

[This answer is for the Linux kernel since was tagged in the question.]

mktime64 is nothing to do with jiffies. It converts the date specified by its parameters to the number of seconds (ignoring leap seconds) since 1970-01-01 00:00:00 (Unix time since the epoch if the parameters are for GMT).

The returned time64_t value can be converted back to year, month, day, hours, minutes, seconds using the time64_to_tm function in the kernel. It has this prototype:

void time64_to_tm(time64_t totalsecs, int offset, struct tm *result);

The offset parameter is a local timezone offset in seconds (number of seconds east of GMT). It should be set to 0 to undo the conversion done by mktime64.

Note that the tm_year member is set to the calculated year minus 1900 and the tm_mon member is set to the calculated month minus 1, so you could implement an unmktime64 function as follows:

void unmktime64(time64_t totalsecs,
                int *year, unsigned int *month, unsigned int *day,
                unsigned int *hour, unsigned int *minute, unsigned int *second)
{
    struct tm tm;

    time64_to_tm(totalsecs, 0, &tm);
    *year = tm.tm_year + 1900;
    *month = tm.tm_mon + 1;
    *day = tm.tm_mday;
    *hour = tm.tm_hour;
    *minute = tm.tm_min;
    *second = tm.tm_sec;
}
Ian Abbott
  • 15,083
  • 19
  • 33
  • I've corrected the call to `time64_to_tm` in the above code because I missed out the `offset` argument. The offset needs to be set to 0 to undo the operation of `mktime64` because that does not use a local time offset. – Ian Abbott Feb 24 '21 at 18:32
  • *calculated year minus 1900* - The code add 1900, what is the right one? – div Mi Feb 26 '21 at 09:39
  • @divMi Both are correct.My phrase "_calculated year minus 1900_" refers to what the `time64_to_tm` does, not what my `unmktime64` function does. – Ian Abbott Feb 26 '21 at 10:06