NanoTime

Utilities for handling time periods at nanosecond resolution.

namespace NanoTime

Typedefs

template<Unit unit>
using UnitTickRatio = std::ratio<unitTicks[unit].num, unitTicks[unit].den>

Class template to define tick std::ratio type.

Template Parameters:

unit

Retval std::ratio:

Ticks per second

Enums

enum Unit

Identify units for a scalar quantity of time.

Note

Ordered in increasing unit size, e.g. days > seconds

Values:

enumerator Nanoseconds
enumerator Microseconds
enumerator Milliseconds
enumerator Seconds
enumerator Minutes
enumerator Hours
enumerator Days
enumerator UnitMax

Functions

const char *unitToString(Unit unit)

Get a string identifying the given time units, e.g. “ns”.

const char *unitToLongString(Unit unit)

Get a long string identifying the given time units, e.g. “seconds”.

template<uint64_t time, Unit unitsFrom, Unit unitsTo, typename R = std::ratio_divide<UnitTickRatio<unitsTo>, UnitTickRatio<unitsFrom>>>
constexpr uint64_t convert()

Function template to convert a constant time quantity from one unit to another.

Note

example:

uint32_t micros = convert<50, Milliseconds, Microseconds>();

Template Parameters:
  • time – The time to convert

  • unitsFrom – Units for time parameter

  • unitsTo – Units for return value

Return values:

TimeType – Converted time

template<typename TimeType>
TimeType convert(const TimeType &time, Unit unitsFrom, Unit unitsTo)

Function template to convert a time quantity from one unit to another.

Template Parameters:

TimeType – Variable type to use for calculation

Parameters:
  • time – The time to convert

  • unitsFrom – Units for time parameter

  • unitsTo – Units for return value

Return values:

TimeType – Converted time, returns TimeType(-1) if calculation overflowed

template<typename T>
Time<T> time(Unit unit, T value)

Helper function to create a Time and deduce the type.

template<Unit unitsFrom, Unit unitsTo, typename TimeType>
TimeType convert(const TimeType &time)

Function template to convert a time quantity from one unit to another.

Template Parameters:
  • unitsFrom – Units for time parameter

  • unitsTo – Units for return value

  • TimeType – Variable type to use for calculation

Parameters:

time – The time to convert

Return values:

TimeType – Converted time, returns TimeType(-1) if calculation overflowed

Variables

constexpr BasicRatio32 unitTicks[UnitMax + 1] = {{1000000000, 1}, {1000000, 1}, {1000, 1}, {1, 1}, {1, 60}, {1, 60 * 60}, {1, 24 * 60 * 60},}

List of clock ticks for each supported unit of time.

struct Frequency
#include <NanoTime.h>

Class to represent a frequency.

template<class Clock_, Unit unit_, uint64_t time_>
struct TimeConst
#include <NanoTime.h>

Class template to represent a fixed time value for a specific Clock.

Note

Includes compile-time range checking. Time is taken as reference for conversions.

Template Parameters:
  • Clock_

  • unit_

  • time_

template<class Clock_, uint64_t ticks_>
struct TicksConst
#include <NanoTime.h>

Class template representing a fixed clock tick count.

Note

Includes compile-time range checking

Template Parameters:
  • Source_

  • ticks_

template<class Clock_, Unit unit_, typename TimeType_>
struct TimeSource : public Clock_
#include <NanoTime.h>

Class template for accessing a Clock in specific time units.

Note

Includes compile-time range checking. Time is taken as reference for conversions.

Template Parameters:
  • Clock_

  • units_

  • TimeType_ – Limits range of calculations

Subclassed by PolledTimer::Timer< NanoTime::Microseconds >, PolledTimer::Timer< NanoTime::Milliseconds >, PolledTimer::Timer< NanoTime::Seconds >, PolledTimer::Timer< NanoTime::Nanoseconds >

template<typename T>
struct Time
#include <NanoTime.h>

Class to handle a simple time value with associated unit.

template<typename Clock_, typename T>
struct Ticks
#include <NanoTime.h>

Class to handle a tick value associated with a clock.

template<typename ClockDef, uint32_t frequency_, typename TickType_, TickType_ maxTicks_>
struct Clock
#include <NanoTime.h>

Class template representing a physical Clock with fixed timing characteristics.

See also

Use TimeSource to work with a Clock in specific time units

Note

Physical clocks are implemented using this as a base. The provided frequency accounts for any prescaler setting in force. Fixing this at compile time helps to avoid expensive runtime calculations and permits static range checks.

Template Parameters:
  • ClockDef – The actual Clock being constructed (so we can query its properties)

  • frequency_Clock frequency in Hz

  • TickType_ – Variable type representing the clock’s tick value

  • maxTicks_ – Maximum count value for the clock

Subclassed by NanoTime::TimeSource< Clock, unit_, TimeType >

struct TimeValue
#include <NanoTime.h>

A time time broken into its constituent elements.

Note

Useful for analysing and printing time values