Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
map-controller
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
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Multi-AP
map-controller
Commits
2875ccf9
Commit
2875ccf9
authored
2 years ago
by
Filip Matusiak
Browse files
Options
Downloads
Patches
Plain Diff
Implement timestamp_to_time utility function
Signed-off-by:
Filip Matusiak
<
filip.matusiak@iopsys.eu
>
parent
29465724
Branches
Branches containing commit
No related tags found
1 merge request
!139
Issue initial channel scan
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/utils/utils.c
+52
-0
52 additions, 0 deletions
src/utils/utils.c
src/utils/utils.h
+3
-0
3 additions, 0 deletions
src/utils/utils.h
with
55 additions
and
0 deletions
src/utils/utils.c
+
52
−
0
View file @
2875ccf9
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
#include
<stdio.h>
#include
<stdio.h>
#include
<string.h>
#include
<string.h>
#include
<signal.h>
#include
<signal.h>
#define __USE_XOPEN
#include
<time.h>
#include
<time.h>
#include
<unistd.h>
#include
<unistd.h>
#include
<sys/types.h>
#include
<sys/types.h>
...
@@ -158,6 +159,7 @@ uint32_t timestamp_elapsed_sec(struct timespec *ts)
...
@@ -158,6 +159,7 @@ uint32_t timestamp_elapsed_sec(struct timespec *ts)
if
(
ts
->
tv_sec
==
0
)
if
(
ts
->
tv_sec
==
0
)
return
-
1
;
return
-
1
;
/* seconds and nanoseconds since the Epoch */
if
(
clock_gettime
(
CLOCK_REALTIME
,
&
now
)
<
0
)
if
(
clock_gettime
(
CLOCK_REALTIME
,
&
now
)
<
0
)
now
.
tv_sec
=
0
;
now
.
tv_sec
=
0
;
...
@@ -172,6 +174,7 @@ int timestamp_expired(struct timespec *a, unsigned int tmo_ms)
...
@@ -172,6 +174,7 @@ int timestamp_expired(struct timespec *a, unsigned int tmo_ms)
struct
timespec
now
;
struct
timespec
now
;
unsigned
long
diff_ns
=
0
,
diff_s
=
0
;
unsigned
long
diff_ns
=
0
,
diff_s
=
0
;
/* seconds and nanoseconds since the Epoch */
if
(
clock_gettime
(
CLOCK_REALTIME
,
&
now
)
<
0
)
if
(
clock_gettime
(
CLOCK_REALTIME
,
&
now
)
<
0
)
return
-
1
;
return
-
1
;
...
@@ -218,6 +221,55 @@ char *time_to_timestamp(time_t *t, char *tsp)
...
@@ -218,6 +221,55 @@ char *time_to_timestamp(time_t *t, char *tsp)
return
tsp
;
return
tsp
;
}
}
time_t
timestamp_to_time
(
const
char
*
tsp
)
{
struct
tm
tm_time
;
char
*
tzone
;
time_t
res
;
long
int
toff
,
sign
;
int
toff_hour
,
toff_min
;
/* Example timestamp: "2019-02-11T06:42:31-08:00" */
memset
(
&
tm_time
,
0
,
sizeof
(
tm_time
));
strptime
(
tsp
,
"%Y-%m-%dT%H:%M:%S"
,
&
tm_time
);
tzset
();
res
=
mktime
(
&
tm_time
);
tzone
=
strchr
(
tsp
,
'+'
);
if
(
!
tzone
)
{
tzone
=
strchr
(
tsp
,
'-'
);
sign
=
-
1L
;
}
else
{
sign
=
+
1L
;
}
if
(
tzone
)
{
sscanf
(
tzone
+
1
,
"%02d:%02d"
,
&
toff_hour
,
&
toff_min
);
toff
=
toff_hour
*
3600
+
toff_min
*
60
;
// seconds
toff
*=
-
sign
;
res
+=
toff
;
}
return
res
;
}
struct
timespec
time_to_timespec
(
time_t
t
)
{
struct
timespec
res
=
{};
res
.
tv_sec
=
t
;
res
.
tv_nsec
=
0
;
return
res
;
}
struct
timespec
tsp_to_timespec
(
const
char
*
tsp
)
{
return
time_to_timespec
(
timestamp_to_time
(
tsp
));
}
/** list utility functions */
/** list utility functions */
/**
/**
* list_num_entries - gets number of entries on the list
* list_num_entries - gets number of entries on the list
...
...
This diff is collapsed.
Click to expand it.
src/utils/utils.h
+
3
−
0
View file @
2875ccf9
...
@@ -67,6 +67,9 @@ static inline int timestamp_invalid(struct timespec *ts)
...
@@ -67,6 +67,9 @@ static inline int timestamp_invalid(struct timespec *ts)
uint32_t
timestamp_elapsed_sec
(
struct
timespec
*
ts
);
uint32_t
timestamp_elapsed_sec
(
struct
timespec
*
ts
);
int
timestamp_expired
(
struct
timespec
*
a
,
unsigned
int
tmo_ms
);
int
timestamp_expired
(
struct
timespec
*
a
,
unsigned
int
tmo_ms
);
char
*
time_to_timestamp
(
time_t
*
t
,
char
*
tsp
);
char
*
time_to_timestamp
(
time_t
*
t
,
char
*
tsp
);
time_t
timestamp_to_time
(
const
char
*
tsp
);
struct
timespec
time_to_timespec
(
time_t
*
t
);
struct
timespec
tsp_to_timespec
(
const
char
*
tsp
);
/* bytes from-to hexstring helper functions */
/* bytes from-to hexstring helper functions */
int
hex2byte
(
const
char
*
hex
);
int
hex2byte
(
const
char
*
hex
);
...
...
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