Defines | Typedefs | Functions

C-Functions for Time Handling

Types and Functions for Time Handling in Cognitive Automobiles. More...

Defines

#define _const
 constant argument, but irrelevant in C
#define KOGMO_TIMESTAMP_TICKSPERSECOND   1000000000
 Ticks per Second. Value, by which the result from kogmo_timestamp_now() will be incremented every second.
#define KOGMO_TIMESTAMP_NANOSECONDSPERTICK   (1000000000/KOGMO_TIMESTAMP_TICKSPERSECOND)
#define KOGMO_TIMESTAMP_STRINGLENGTH   30
 Length of the Output of kogmo_timestamp_to_string().

Typedefs

typedef int64_t kogmo_timestamp_t
 Absolute Timestamp (in Ticks since the "Epoch").
typedef char kogmo_timestamp_string_t [KOGMO_TIMESTAMP_STRINGLENGTH]
 Type of a pre-allocated String to receive the Output of kogmo_timestamp_to_string().

Functions

kogmo_timestamp_t kogmo_timestamp_now (void)
 Get absolute Timestamp for current Time.
int64_t kogmo_timestamp_diff_ns (kogmo_timestamp_t ts_begin, kogmo_timestamp_t ts_end)
 Calcute difference between two Timestamps (in Nanoseconds).
double kogmo_timestamp_diff_secs (kogmo_timestamp_t ts_begin, kogmo_timestamp_t ts_end)
 Calcute difference between two Timestamps (in Seconds, floating point).
kogmo_timestamp_t kogmo_timestamp_add_ns (kogmo_timestamp_t ts, int64_t ns)
 Add time offset to Timestamps (in Nanoseconds).
kogmo_timestamp_t kogmo_timestamp_add_secs (kogmo_timestamp_t ts, double secs)
 Add time offset to Timestamps (in Seconds, floating point).
int kogmo_timestamp_to_string (kogmo_timestamp_t ts, kogmo_timestamp_string_t str)
 Convert an Absolute Timestamp to a String, formated according to the ISO-Standard.
kogmo_timestamp_t kogmo_timestamp_from_string (_const char *str)
 Convert an ISO-Time-String to an Absolute Timestamp.

Detailed Description

Types and Functions for Time Handling in Cognitive Automobiles.


Define Documentation

#define _const

constant argument, but irrelevant in C

Definition at line 29 of file kogmo_time.h.

#define KOGMO_TIMESTAMP_NANOSECONDSPERTICK   (1000000000/KOGMO_TIMESTAMP_TICKSPERSECOND)

Definition at line 90 of file kogmo_time.h.

#define KOGMO_TIMESTAMP_STRINGLENGTH   30

Length of the Output of kogmo_timestamp_to_string().

Definition at line 147 of file kogmo_time.h.

#define KOGMO_TIMESTAMP_TICKSPERSECOND   1000000000

Ticks per Second. Value, by which the result from kogmo_timestamp_now() will be incremented every second.

As we use Nanoseconds, its equal to 1000000000.
But *PLEASE* dont hardcode 1000000000 anywere, use this constant if necessary!
Better use the following functions to handle timestamps.

Definition at line 89 of file kogmo_time.h.


Typedef Documentation

typedef char kogmo_timestamp_string_t[KOGMO_TIMESTAMP_STRINGLENGTH]

Type of a pre-allocated String to receive the Output of kogmo_timestamp_to_string().

Definition at line 152 of file kogmo_time.h.

typedef int64_t kogmo_timestamp_t

Absolute Timestamp (in Ticks since the "Epoch").

This is the reference type that to be used for all timestamps.
It can represent points in time from the year 1970 until 2262 with a nanosecond resolution.

Background:

The Epoch starts 00:00:00 UTC, January 1, 1970, see also (POSIX.1 and time(2), http://de.wikipedia.org/wiki/Epoch).
This is a signed 64-Bit value that will overflow in the year 2262.
( 2^63/1000000000/60/60/24/365 = 292.471.. )

Why a 64-Bit value value? Aren't 32 Bits enough?
A 32-Bit (unsigned) value would overflow after

  • less than 5 seconds with a nanosecond resolution ( 2^32/1000/1000/10e00=4.294... )
  • less than 2 hours with a microsecond resolution ( 2^32/1000/60/60/1000=1.193... )
  • less than 50 days with millisecond resolution ( 2^32/1000/60/60/24 = 49.710.. )

So only a millisecond granularity would be usefull, but

  • we can use it only for a relative time with a defined begin (e.g. system boot)
  • We have to remember this start time (and possibly save it in out logs, maybe communicate it to other vehicles)
  • We wouldn't win a "Around the World in Eighty Days" race ;-)

So please

  • use this timestamp type
  • get the timestamp via kogmo_timestamp_now()
  • work with the timestamps using kogmo_timestamp_difference_in_seconds() and kogmo_timestamp_add_seconds()
  • use KOGMO_TIMESTAMP_TICKSPERSECOND just for your information
  • DO NOT implement own calculations with nanoseconds

There are convenience functions with float-times in seconds, these can also be used in calculations.
For exchanging times you should use kogmo_time_t.

For any questions, please contact me! Matthias Goebl <matthias.goebl*goebl.net>

Definition at line 78 of file kogmo_time.h.


Function Documentation

kogmo_timestamp_t kogmo_timestamp_add_ns ( kogmo_timestamp_t  ts,
int64_t  ns 
)

Add time offset to Timestamps (in Nanoseconds).

Parameters:
ts Timestamp
ns Nanoseconds to add, can be negative (64-Bit integer value)
Returns:
New Timestamp
kogmo_timestamp_t kogmo_timestamp_add_secs ( kogmo_timestamp_t  ts,
double  secs 
)

Add time offset to Timestamps (in Seconds, floating point).

Parameters:
ts Timestamp
secs Seconds to add, can be negative (floating point value)
Returns:
New Timestamp
int64_t kogmo_timestamp_diff_ns ( kogmo_timestamp_t  ts_begin,
kogmo_timestamp_t  ts_end 
)

Calcute difference between two Timestamps (in Nanoseconds).

Parameters:
ts_begin First Timestamp, begin of the interval
ts_end Second Timestamp, end of the interval
Returns:
Length of the interval in nanoseconds (64-Bit integer value)
double kogmo_timestamp_diff_secs ( kogmo_timestamp_t  ts_begin,
kogmo_timestamp_t  ts_end 
)

Calcute difference between two Timestamps (in Seconds, floating point).

This may simplify your calculations.
For exchanging timestamps you should use kogmo_timestamp_t instead.

Parameters:
ts_begin First Timestamp, begin of the interval
ts_end Second Timestamp, end of the interval
Returns:
Length of the interval in Seconds (floating point value)
kogmo_timestamp_t kogmo_timestamp_from_string ( _const char *  str  ) 

Convert an ISO-Time-String to an Absolute Timestamp.

Parameters:
str Time as String, see kogmo_timestamp_to_string() for format
Returns:
Timestamp, zero on errors

For a incomplete ISO8601-Time the missing elements will be assumed to 0.
For your comfort, str can also be a numeric timestamp in a string (>9999).

kogmo_timestamp_t kogmo_timestamp_now ( void   ) 

Get absolute Timestamp for current Time.

Returns:
zero on errors
int kogmo_timestamp_to_string ( kogmo_timestamp_t  ts,
kogmo_timestamp_string_t  str 
)

Convert an Absolute Timestamp to a String, formated according to the ISO-Standard.

Format: 2006-04-26 23:54:27.832241000 (YYYY-MM-DD hh:mm:ss.sssssssss)

Parameters:
ts Timestamp
str Pre-Allocated String for the Output
Returns:
non-zero on errors

The format follows ISO 8601 "Data elements and interchange formats, Information interchange, Representation of dates and times", with the addition of subseconds as ".sssssssss".
See http://en.wikipedia.org/wiki/ISO_8601 and http://hydracen.com/dx/iso8601.htm.


Generated for KogMo-RTDB by Matthias.Goebl (mattias.goebl*kogmo-rtdb.de) - all rights reserved.