summaryrefslogtreecommitdiff
path: root/src/common/time_helper.cpp
blob: 2545ba735a82e7961ec150911322862771a009a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
**********************************************************************************************
*	File: time_help.cpp
*   Description:
*	Authors: Liu WenTan <[email protected]>
*	Date:    2022-07-15
*   Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/

#include <sys/time.h>

#include "time_helper.h"

void get_current_timespec(struct timespec *tm)
{
    struct timeval now;
    if(gettimeofday(&now, NULL) == 0) {
        tm->tv_sec  = now.tv_sec;
        tm->tv_nsec = now.tv_usec * 1000L;
    }
}

int compare_timespec(struct timespec *left, struct timespec *right)
{
    if (left->tv_sec < right->tv_sec) {
        return -1;
    } else if (left->tv_sec > right->tv_sec) {
        return 1;
    } else {
        if (left->tv_nsec < right->tv_nsec) {
            return -1;
        } else if (left->tv_nsec > right->tv_nsec) {
            return 1;
        } else {
            return 0;
        }
    }
}

void copy_timespec(struct timespec *from, struct timespec *to)
{
    to->tv_sec = from->tv_sec;
    to->tv_nsec = from->tv_nsec;
}

uint64_t timespec_to_millisecond(const struct timespec* ts)
{
    return ts->tv_sec * 1000L + ts->tv_nsec / 1000000L;
}