Skip to content

Commit

Permalink
Fix #429, add "assemble" routines for milli/microsecs
Browse files Browse the repository at this point in the history
Add OS_TimeAssembleFromMilliseconds and OS_TimeAssembleFromMicroseconds
for a complete set of conversion routines in both directions.
  • Loading branch information
jphickey committed Jan 7, 2021
1 parent 779d3e3 commit f09c57e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/os/inc/osapi-clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,55 @@ static inline OS_time_t OS_TimeAssembleFromNanoseconds(int64 seconds, uint32 nan
return result;
}

/*-------------------------------------------------------------------------------------*/
/**
* @brief Assemble/Convert a number of seconds + microseconds into an OS_time_t interval
*
* This creates an OS_time_t value using a whole number of seconds and a fractional
* part in units of microseconds. This is the inverse of OS_TimeGetTotalSeconds()
* and OS_TimeGetMicrosecondsPart(), and should recreate the original OS_time_t
* value from these separate values (aside from any potential conversion losses
* due to limited resolution of the data types/units).
*
* @sa OS_TimeGetTotalSeconds(), OS_TimeGetMicrosecondsPart()
*
* @param[in] seconds Whole number of seconds
* @param[in] microseconds Number of microseconds (fractional part only)
* @returns The input arguments represented as an OS_time_t interval
*/
static inline OS_time_t OS_TimeAssembleFromMicroseconds(int64 seconds, uint32 microseconds)
{
OS_time_t result;
result.seconds = seconds;
result.microsecs = microseconds;
return result;
}

/*-------------------------------------------------------------------------------------*/
/**
* @brief Assemble/Convert a number of seconds + milliseconds into an OS_time_t interval
*
* This creates an OS_time_t value using a whole number of seconds and a fractional
* part in units of milliseconds. This is the inverse of OS_TimeGetTotalSeconds()
* and OS_TimeGetMillisecondsPart(), and should recreate the original OS_time_t
* value from these separate values (aside from any potential conversion losses
* due to limited resolution of the data types/units).
*
* @sa OS_TimeGetTotalSeconds(), OS_TimeGetMillisecondsPart()
*
* @param[in] seconds Whole number of seconds
* @param[in] milliseconds Number of milliseconds (fractional part only)
* @returns The input arguments represented as an OS_time_t interval
*/
static inline OS_time_t OS_TimeAssembleFromMilliseconds(int64 seconds, uint32 milliseconds)
{
OS_time_t result;
result.seconds = seconds;
result.microsecs = milliseconds * 1000;
return result;
}


/*-------------------------------------------------------------------------------------*/
/**
* @brief Assemble/Convert a number of seconds + subseconds into an OS_time_t interval
Expand Down
25 changes: 25 additions & 0 deletions src/unit-test-coverage/shared/src/coveragetest-clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ void Test_OS_TimeAccessConversions(void)
* uint32 OS_TimeGetMicrosecondsPart(OS_time_t tm)
* uint32 OS_TimeGetNanosecondsPart(OS_time_t tm)
*
* OS_time_t OS_TimeAssembleFromMilliseconds(int64 seconds, uint32 milliseconds)
* OS_time_t OS_TimeAssembleFromMicroseconds(int64 seconds, uint32 microseconds)
* OS_time_t OS_TimeAssembleFromNanoseconds(int64 seconds, uint32 nanoseconds)
* OS_time_t OS_TimeAssembleFromSubseconds(int64 seconds, uint32 subseconds)
*
* OS_time_t OS_TimeAdd(OS_time_t time1, OS_time_t time2)
* OS_time_t OS_TimeSubtract(OS_time_t time1, OS_time_t time2)
*/
Expand All @@ -92,32 +95,54 @@ void Test_OS_TimeAccessConversions(void)
/* From base-2 32-bit fixed point: 0x87654321 / 0x100000000 ~= 0.528888888 s */
t2 = OS_TimeAssembleFromSubseconds(2,0x87654321);

/* To base-2 32-bit fixed point: 0.045678 s * 0x100000000 ~= 0x0bb18dad */
t3 = OS_TimeAssembleFromMicroseconds(0,45678);

/* To base-2 32-bit fixed point: 0.901 s * 0x100000000 ~= 0xe6a7ef9e */
t4 = OS_TimeAssembleFromMilliseconds(1,901);

/* These functions only return the total (whole + fraction) in the requested units */
UtAssert_UINT32_EQ(OS_TimeGetTotalSeconds(t1), 1);
UtAssert_UINT32_EQ(OS_TimeGetTotalSeconds(t2), 2);
UtAssert_UINT32_EQ(OS_TimeGetTotalSeconds(t3), 0);
UtAssert_UINT32_EQ(OS_TimeGetTotalSeconds(t4), 1);

UtAssert_UINT32_EQ(OS_TimeGetTotalMilliseconds(t1), 1234);
UtAssert_UINT32_EQ(OS_TimeGetTotalMilliseconds(t2), 2528);
UtAssert_UINT32_EQ(OS_TimeGetTotalMilliseconds(t3), 45);
UtAssert_UINT32_EQ(OS_TimeGetTotalMilliseconds(t4), 1901);

UtAssert_UINT32_EQ(OS_TimeGetTotalMicroseconds(t1), 1234567);
UtAssert_UINT32_EQ(OS_TimeGetTotalMicroseconds(t2), 2528888);
UtAssert_UINT32_EQ(OS_TimeGetTotalMicroseconds(t3), 45678);
UtAssert_UINT32_EQ(OS_TimeGetTotalMicroseconds(t4), 1901000);

/* Note: Nanoseconds/Subseconds may not be exact due to limitations of OS_time_t resolution */
UtAssert_UINT32_EQ(OS_TimeGetTotalNanoseconds(t1), 1234567000);
UtAssert_UINT32_EQ(OS_TimeGetTotalNanoseconds(t2), 2528888000);
UtAssert_UINT32_EQ(OS_TimeGetTotalNanoseconds(t3), 45678000);
UtAssert_UINT32_EQ(OS_TimeGetTotalNanoseconds(t4), 1901000000);

/* These functions only return the fractional part, not the whole part */
UtAssert_UINT32_EQ(OS_TimeGetSubsecondsPart(t1), 0x3c0c953a);
UtAssert_UINT32_EQ(OS_TimeGetSubsecondsPart(t2), 0x87653438);
UtAssert_UINT32_EQ(OS_TimeGetSubsecondsPart(t3), 0x0bb18dad);
UtAssert_UINT32_EQ(OS_TimeGetSubsecondsPart(t4), 0xe6a7ef9e);

UtAssert_UINT32_EQ(OS_TimeGetMillisecondsPart(t1), 234);
UtAssert_UINT32_EQ(OS_TimeGetMillisecondsPart(t2), 528);
UtAssert_UINT32_EQ(OS_TimeGetMillisecondsPart(t3), 45);
UtAssert_UINT32_EQ(OS_TimeGetMillisecondsPart(t4), 901);

UtAssert_UINT32_EQ(OS_TimeGetMicrosecondsPart(t1), 234567);
UtAssert_UINT32_EQ(OS_TimeGetMicrosecondsPart(t2), 528888);
UtAssert_UINT32_EQ(OS_TimeGetMicrosecondsPart(t3), 45678);
UtAssert_UINT32_EQ(OS_TimeGetMicrosecondsPart(t4), 901000);

UtAssert_UINT32_EQ(OS_TimeGetNanosecondsPart(t1), 234567000);
UtAssert_UINT32_EQ(OS_TimeGetNanosecondsPart(t2), 528888000);
UtAssert_UINT32_EQ(OS_TimeGetNanosecondsPart(t3), 45678000);
UtAssert_UINT32_EQ(OS_TimeGetNanosecondsPart(t4), 901000000);

/* Simple Add/Subtract */
t3 = OS_TimeAdd(t1, t2);
Expand Down

0 comments on commit f09c57e

Please sign in to comment.