3

MISRA and AUTOSAR do not allow us to use the time() function of <ctime> (and <time.h>).

Rule 18-0-4: The time handling functions of library <ctime> shall not be used.

Is there an alternative to the time() function in C++?

There is the time library <chrono>. But there I didn't find a function that returns the current time like time(). Or am I missing something?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
KplnMoon
  • 151
  • 1
  • 10
  • 4
    [`std::chrono::system_clock::now()`](https://en.cppreference.com/w/cpp/chrono/system_clock/now) – Drew Dormann May 20 '22 at 14:16
  • @DrewDormann Wow! Very fast answer! Thanks! Do I need to convert it to type time_t to get the same effect as time()? – KplnMoon May 20 '22 at 14:19
  • 2
    [`std::chrono::system_clock::to_time_t()`](https://en.cppreference.com/w/cpp/chrono/system_clock/to_time_t) – Drew Dormann May 20 '22 at 14:20
  • @DrewDormann I know the function to_time_t, but my idea is correct, right? Thanks anyway! – KplnMoon May 20 '22 at 14:22
  • 1
    Since the C function `time()` returns a `time_t` type, if you need that same effect (getting a time that's likely **much** lower-resolution) `to_time_t()` will produce that value. – Drew Dormann May 20 '22 at 14:25

1 Answers1

3

Using the <chrono> header requires a little more involvement than the time function.

You can create a time_point using std::chrono::steady_clock::now(). Subtracting two time_point instances creates a std::duration which reflects the difference in time between the two instances. You can then use a duration_cast to cast this duration to a scale of your choice (microsecond, millisecond etc.) and then use the count member function to get the actual time in unsigned type.

TL;DR You can measure elapsed time between two events by doing this:

const auto start = std::chrono::steady_clock::now(); // event 1
// something happens in between...
const auto end = std::chrono::steady_clock::now(); // event 2
const auto time_elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();

Notes:

  1. time_elapsed is not a floating-point variable,
  2. std::chrono::steady_clock should be preferred to std::chrono::system_clock because the former is a monotonic clock i.e. it will not be adjusted by the OS.
Nitin Malapally
  • 534
  • 2
  • 10
  • 2
    [AUTOSAR says](https://www.autosar.org/fileadmin/user_upload/standards/adaptive/18-10/AUTOSAR_RS_CPP14Guidelines.pdf): "Note: Facilities from `` shall be used instead." – Howard Hinnant May 20 '22 at 15:52
  • @Nitin_Malapally Thanks! Wouldn't the following two lines be equivalent: (1) time(nullptr); and (2) system_clock::to_time_t(system_clock::now()); ? I don't understand exactly why I should also use duration_cast? – KplnMoon May 20 '22 at 17:05
  • 1
    Video tutorial on C++11/14/17 ``: https://www.youtube.com/watch?v=P32hvk8b13M It will cost you an hour, but hopefully will be worth it. – Howard Hinnant May 20 '22 at 18:02
  • 2
    @KplnMoon You could do that as well. But like i said, the system clock may not necessarily be monotonic i.e. it could be reset. This means that you may find irregularities between timestamps. – Nitin Malapally May 20 '22 at 18:09