Skip to content

Commit

Permalink
[libtime] add elapse api
Browse files Browse the repository at this point in the history
  • Loading branch information
gozfree committed Sep 19, 2021
1 parent 20b7250 commit 0312b59
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 71 deletions.
8 changes: 4 additions & 4 deletions gear-lib/librpc/librpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void print_packet(struct rpc_packet *pkt)
printf("header.uuid_src = 0x%08x\n", pkt->header.uuid_src);
printf("header.msg_id = 0x%08x\n", pkt->header.msg_id);
printf("header.timestamp = %" PRIu64 "(%s)\n", pkt->header.timestamp,
time_str_human_by_msec(pkt->header.timestamp, ts, sizeof(ts)));
time_str_format_by_msec(pkt->header.timestamp, ts, sizeof(ts)));
printf("header.payload_len = %d\n", pkt->header.payload_len);
printf("header.checksum = 0x%08x\n", pkt->header.checksum);
dump_packet(pkt);
Expand All @@ -110,7 +110,7 @@ void print_session(struct rpc_session *ss)
printf("session.uuid_src = 0x%08x\n", ss->uuid_src);
printf("session.msg_id = 0x%08x\n", ss->msg_id);
printf("session.timestamp = %" PRIu64 "(%s)\n", ss->timestamp,
time_str_human_by_msec(ss->timestamp, ts, sizeof(ts)));
time_str_format_by_msec(ss->timestamp, ts, sizeof(ts)));
printf("session.checksum = 0x%08lx\n", ss->cseq);
printf("================\n");
}
Expand Down Expand Up @@ -158,7 +158,7 @@ size_t pack_msg(struct rpc_packet *pkt, uint32_t uuid_dst, uint32_t uuid_src,
hdr->uuid_dst = uuid_dst;
hdr->uuid_src = uuid_src;
hdr->msg_id = msg_id;
time_info(&ti);
time_now_info(&ti);
hdr->timestamp = ti.utc_msec;

if (in_arg) {
Expand Down Expand Up @@ -508,7 +508,7 @@ static struct rpc_session *rpc_session_create(struct rpcs *s, int fd, uint32_t u
pkt.header.msg_id = 0;
pkt.header.payload_len = sizeof(uuid);
pkt.payload = &uuid;
time_info(&ti);
time_now_info(&ti);
pkt.header.timestamp = ti.utc_msec;

ret = rpc_send(&session->base, &pkt);
Expand Down
2 changes: 1 addition & 1 deletion gear-lib/librtsp/transport_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static void *send_thread(struct thread *t, void *ptr)
}
ms->is_active = true;
unsigned int ssrc = (unsigned int)rtp_ssrc();
uint64_t pts = time_msec();
uint64_t pts = time_now_msec();
unsigned int seq = ssrc;
logd("rtp send thread %s created\n", t->name);
while (t->run) {
Expand Down
65 changes: 29 additions & 36 deletions gear-lib/libtime/libtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
#include <libposix.h>
#include "libtime.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -30,12 +30,10 @@
#include <sys/timeb.h>
#endif

#include "libtime.h"

#define TIME_FORMAT "%Y%m%d%H%M%S"


uint64_t time_sec()
uint64_t time_now_sec()
{
time_t t;
t = time(NULL);
Expand All @@ -45,7 +43,7 @@ uint64_t time_sec()
return t;
}

char *time_sec_str()
char *time_now_sec_str()
{
time_t t;
struct tm *tm;
Expand All @@ -60,18 +58,18 @@ char *time_sec_str()
return asctime(tm);
}

char *time_str_human(char *str, int len)
char *time_now_str_format(char *str, int len)
{
struct time_info ti;
if (-1 == time_info(&ti)) {
if (-1 == time_now_info(&ti)) {
return NULL;
}
snprintf(str, len, "%04d%02d%02d%02d%02d%02d",
ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec);
return str;
}

char *time_str_human_by_utc(uint32_t utc, char *str, int len)
char *time_str_format_by_utc(uint32_t utc, char *str, int len)
{
struct time_info ti;
if (-1 == time_info_by_utc(utc, &ti)) {
Expand All @@ -82,7 +80,7 @@ char *time_str_human_by_utc(uint32_t utc, char *str, int len)
return str;
}

char *time_str_human_by_msec(uint64_t msec, char *str, int len)
char *time_str_format_by_msec(uint64_t msec, char *str, int len)
{
struct time_info ti;
if (-1 == time_info_by_msec(msec, &ti)) {
Expand All @@ -93,14 +91,13 @@ char *time_str_human_by_msec(uint64_t msec, char *str, int len)
return str;
}


char *time_str_human_by_timeval(struct timeval *tv, char *str, int len)
char *time_str_format_by_timeval(struct timeval *tv, char *str, int len)
{
uint32_t sec = time_usec(tv)/1000000;
return time_str_human_by_utc(sec, str, len);
uint32_t sec = time_now_usec(tv)/1000000;
return time_str_format_by_utc(sec, str, len);
}

uint64_t time_usec(struct timeval *val)
uint64_t time_now_usec(struct timeval *val)
{
struct timeval tv;
if (val == NULL) {
Expand All @@ -113,7 +110,7 @@ uint64_t time_usec(struct timeval *val)
return (uint64_t)(((uint64_t)tv.tv_sec)*1000*1000 + (uint64_t)tv.tv_usec);
}

uint64_t _time_clock_gettime(clockid_t clk_id)
static uint64_t _time_clock_gettime(clockid_t clk_id)
{
#if defined (OS_LINUX) || defined (OS_APPLE)
struct timespec ts;
Expand All @@ -127,22 +124,22 @@ uint64_t _time_clock_gettime(clockid_t clk_id)
#endif
}

uint64_t time_msec()
uint64_t time_now_msec()
{
return _time_clock_gettime(CLOCK_REALTIME)/1000;
return _time_clock_gettime(CLOCK_REALTIME)/(1000*1000);
}

uint64_t time_nsec()
uint64_t time_now_nsec()
{
return _time_clock_gettime(CLOCK_REALTIME);
}

uint64_t time_nsec_bootup()
uint64_t time_bootup_nsec()
{
return _time_clock_gettime(CLOCK_MONOTONIC);
}

char *time_msec_str(char *str, int len)
char *time_now_msec_str(char *str, int len)
{
char date_fmt[20];
char date_ms[4];
Expand Down Expand Up @@ -173,10 +170,15 @@ char *time_msec_str(char *str, int len)

int time_sleep_ms(uint64_t ms)
{
#if defined (OS_LINUX)
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = ms*1000;
return select(0, NULL, NULL, NULL, &tv);
#elif defined (OS_WINDOWS)
usleep(ms*1000);
return 0;
#endif
}

int time_info_by_utc(uint32_t utc, struct time_info *ti)
Expand Down Expand Up @@ -253,7 +255,7 @@ int time_info_by_msec(uint64_t msec, struct time_info *ti)
return 0;
}

int time_info(struct time_info *ti)
int time_now_info(struct time_info *ti)
{
time_t utc;
struct timeval tv;
Expand Down Expand Up @@ -319,20 +321,11 @@ int time_set_info(struct time_info *ti)
}
#endif

bool time_passed_sec(int sec)
uint64_t time_elapsed_ms(struct timeval *prev)
{
bool ret = false;
static uint64_t last_sec = 0;
static uint64_t now_sec = 0;
now_sec = time_sec();
if (last_sec == 0) {
last_sec = now_sec;
}
if (now_sec - last_sec >= (uint32_t)sec) {
ret = true;
last_sec = now_sec;
} else {
ret = false;
}
return ret;
struct timeval now, res;

gettimeofday(&now, NULL);
timersub(&now, prev, &res);
return (res.tv_sec)*1000 + (res.tv_usec)/1000;
}
31 changes: 14 additions & 17 deletions gear-lib/libtime/libtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
#ifndef LIBTIME_H
#define LIBTIME_H

#include <libposix.h>
#include <stdint.h>
#if defined (__linux__) || defined (__CYGWIN__)
#include <stdbool.h>
#endif
#include <time.h>

#define LIBTIME_VERSION "0.1.1"
Expand All @@ -51,37 +49,36 @@ struct time_info {
/*
* accuracy second
*/
uint64_t time_sec();
char *time_sec_str();
char *time_str_human(char *str, int len);
char *time_str_human_by_utc(uint32_t utc, char *str, int len);
char *time_str_human_by_msec(uint64_t msec, char *str, int len);
char *time_str_human_by_timeval(struct timeval *val, char *str, int len);
uint64_t time_now_sec();
char *time_now_sec_str();
char *time_now_format(char *str, int len);
char *time_str_format_by_utc(uint32_t utc, char *str, int len);
char *time_str_format_by_msec(uint64_t msec, char *str, int len);
char *time_str_format_by_timeval(struct timeval *val, char *str, int len);

/*
* accuracy milli second
*/
uint64_t time_msec();
char *time_msec_str(char *str, int len);
uint64_t time_now_msec();
char *time_now_msec_str(char *str, int len);
int time_sleep_ms(uint64_t ms);

/*
* accuracy micro second
*/
uint64_t time_usec(struct timeval *tv);
uint64_t time_now_usec(struct timeval *tv);

/*
* accuracy nano second
*/
uint64_t time_nsec();
uint64_t time_nsec_bootup();
uint64_t time_now_nsec();
uint64_t time_bootup_nsec();
char *time_nsec_to_str(uint64_t nsec);

int time_info(struct time_info *ti);
int time_now_info(struct time_info *ti);
int time_info_by_utc(uint32_t utc, struct time_info *ti);
int time_info_by_msec(uint64_t msec, struct time_info *ti);

bool time_passed_sec(int sec);
uint64_t time_elapsed_ms(struct timeval *tv);

#ifdef __cplusplus
}
Expand Down
26 changes: 13 additions & 13 deletions gear-lib/libtime/test_libtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ void foo()
struct time_info ti;
char ts[64];
char time[32];
printf("time_sec_str: %s", time_sec_str());
printf("time_msec_str: %s\n", time_msec_str(time, sizeof(time)));
printf("time_sec: %" PRIu64 "\n", time_sec());
printf("time_msec: %" PRIu64 "\n", time_msec());
printf("time_msec: %" PRIu64 "\n", time_usec(NULL)/1000);
printf("time_usec: %" PRIu64 "\n", time_usec(NULL));
printf("time_nsec: %" PRIu64 "\n", time_nsec());
printf("time_nsec_bootup: %" PRIu64 "\n", time_nsec_bootup());
time_info(&ti);
printf("time_info: %d-%d-%d %02d:%02d:%02d.%03d\n",
ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec, ti.msec);
printf("time_info: %s\n", ti.str);
printf("time_now_sec_str: %s", time_now_sec_str());
printf("time_now_msec_str: %s\n", time_now_msec_str(time, sizeof(time)));
printf("time_now_sec: %" PRIu64 "\n", time_now_sec());
printf("time_now_msec: %" PRIu64 "\n", time_now_msec());
printf("time_now_msec: %" PRIu64 "\n", time_now_usec(NULL)/1000);
printf("time_now_usec: %" PRIu64 "\n", time_now_usec(NULL));
printf("time_now_nsec: %" PRIu64 "\n", time_now_nsec());
printf("time_bootup_nsec: %" PRIu64 "\n", time_bootup_nsec());
time_now_info(&ti);
printf("time_now_info: %d-%02d-%02d %02d:%02d:%02d.%03d\n",
ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec, ti.msec);
printf("time_now_info.str: %s\n", ti.str);
memset(ts, 0, sizeof(ts));
printf("time_info: %s\n", time_str_human_by_msec(ti.utc_msec, ts, sizeof(ts)));
printf("time_str_by_msec: %s\n", time_str_format_by_msec(ti.utc_msec, ts, sizeof(ts)));
}

int main(int argc, char **argv)
Expand Down

0 comments on commit 0312b59

Please sign in to comment.