Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RIOT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cm-projects
RIOT
Commits
ea48b48d
Commit
ea48b48d
authored
9 years ago
by
Martine Lenders
Browse files
Options
Downloads
Patches
Plain Diff
timex: make timex_to_str more efficient
parent
6d9b9288
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
sys/include/timex.h
+11
-24
11 additions, 24 deletions
sys/include/timex.h
sys/shell/commands/sc_ipv6_nc.c
+1
-0
1 addition, 0 deletions
sys/shell/commands/sc_ipv6_nc.c
sys/timex/timex_to_str.c
+65
-0
65 additions, 0 deletions
sys/timex/timex_to_str.c
with
77 additions
and
24 deletions
sys/include/timex.h
+
11
−
24
View file @
ea48b48d
...
@@ -22,22 +22,12 @@
...
@@ -22,22 +22,12 @@
#define __TIMEX_H
#define __TIMEX_H
#include
<stdint.h>
#include
<stdint.h>
#include
<stdio.h>
#include
<inttypes.h>
#include
<inttypes.h>
#ifdef __cplusplus
#ifdef __cplusplus
extern
"C"
{
extern
"C"
{
#endif
#endif
/**
* @brief Formater for unsigned 32 bit values
*
* mspgcc bug : PRIxxx macros not defined before mid-2011 versions
*/
#ifndef PRIu32
#define PRIu32 "lu"
#endif
/**
/**
* @brief The number of microseconds per second
* @brief The number of microseconds per second
*/
*/
...
@@ -56,7 +46,14 @@ extern "C" {
...
@@ -56,7 +46,14 @@ extern "C" {
/**
/**
* @brief The maximum length of the string representation of a timex timestamp
* @brief The maximum length of the string representation of a timex timestamp
*/
*/
#define TIMEX_MAX_STR_LEN (18)
#define TIMEX_MAX_STR_LEN (20)
/* 20 =
* + 10 byte: 2^32-1 for seconds
* + 1 byte: decimal point
* + 6 byte: microseconds (normalized)
* + 2 byte: " s" (unit)
* + 1 byte: '\0'
*/
/**
/**
* @brief A timex timestamp
* @brief A timex timestamp
...
@@ -166,26 +163,16 @@ static inline timex_t timex_from_uint64(const uint64_t timestamp)
...
@@ -166,26 +163,16 @@ static inline timex_t timex_from_uint64(const uint64_t timestamp)
/**
/**
* @brief Converts a timex timestamp to a string
* @brief Converts a timex timestamp to a string
*
*
* @pre memory at timestamp >= TIMEX_MAX_STR_LEN
*
* @param[in] t The timestamp to convert
* @param[in] t The timestamp to convert
* @param[out] timestamp The output char buffer for the converted timestamp
* @param[out] timestamp The output char buffer for the converted timestamp
*
*
* @note The timestamp will be normalized
* @note The timestamp will be normalized
* @note The buffer must have a size of TIMEX_MAX_STR_LEN characters
*
*
* @return A pointer to the string representation of the timestamp
* @return A pointer to the string representation of the timestamp
*
* @todo replace call to snprintf by something more efficient
*/
*/
static
inline
const
char
*
timex_to_str
(
timex_t
t
,
char
*
timestamp
)
const
char
*
timex_to_str
(
timex_t
t
,
char
*
timestamp
);
{
timex_normalize
(
&
t
);
/* 2^32 seconds have maximum 10 digits, microseconds are always < 1000000
* in a normalized timestamp, plus two chars for the point and terminator
* => 10 + 6 + 2 = 20 */
snprintf
(
timestamp
,
TIMEX_MAX_STR_LEN
,
"%"
PRIu32
".%06"
PRIu32
" s"
,
t
.
seconds
,
t
.
microseconds
);
return
timestamp
;
}
#ifdef __cplusplus
#ifdef __cplusplus
}
}
...
...
This diff is collapsed.
Click to expand it.
sys/shell/commands/sc_ipv6_nc.c
+
1
−
0
View file @
ea48b48d
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
*/
#include
<stdio.h>
#include
<string.h>
#include
<string.h>
#include
"kernel_types.h"
#include
"kernel_types.h"
...
...
This diff is collapsed.
Click to expand it.
sys/timex/timex_to_str.c
0 → 100644
+
65
−
0
View file @
ea48b48d
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
*/
#include
<stdint.h>
#include
"timex.h"
#define RADIX (10)
static
unsigned
int
u32_to_str
(
char
*
str
,
uint32_t
u32
)
{
int
len
;
int
i
=
0
;
if
(
u32
==
0
)
{
str
[
i
]
=
'0'
;
return
1
;
}
for
(;
u32
>
0
;
u32
/=
RADIX
)
{
char
digit
=
(
char
)(
u32
%
RADIX
);
str
[
i
++
]
=
'0'
+
digit
;
}
len
=
i
;
i
--
;
/* go back to last character */
for
(
int
j
=
0
;
j
<
i
;
j
++
,
i
--
)
{
char
tmp
=
str
[
i
];
str
[
i
]
=
str
[
j
];
str
[
j
]
=
tmp
;
}
return
len
;
}
const
char
*
timex_to_str
(
timex_t
t
,
char
*
timestamp
)
{
timex_normalize
(
&
t
);
char
*
dec
;
uint8_t
offset
=
6
;
/* get seconds and set terminating '\0' byte */
dec
=
&
timestamp
[
u32_to_str
(
timestamp
,
t
.
seconds
)];
*
(
dec
++
)
=
'.'
;
/* set decimal point */
for
(
uint32_t
i
=
100000
;
i
>
1
;
i
/=
10
)
{
/* timex_normalize() ensures that i < 1000000 */
if
(
t
.
microseconds
<
i
)
{
/* pad with 0's */
*
(
dec
++
)
=
'0'
;
offset
--
;
}
}
u32_to_str
(
dec
,
t
.
microseconds
);
/* convert microseconds */
dec
+=
offset
;
*
(
dec
++
)
=
' '
;
/* append unit */
*
(
dec
++
)
=
's'
;
*
(
dec
)
=
'\0'
;
return
timestamp
;
}
/** @} */
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment