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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) 2011-2018, Meituan Dianping. All Rights Reserved.
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CAT_TIME_UTIL_H
#define CAT_TIME_UTIL_H
#include "typedef.h"
#include "headers.h"
#define Sleep(ms) usleep((ms) * 1000);
/**
* Get current timestamp in 64 bit (milliseconds since midnight, Jan 1, 1970 UTC)
*/
static u_int64 inline GetTime64() {
u_int64 buf;
#if defined(WIN32)
struct __timeb64 timeBuf;
_ftime64_s(&timeBuf);
buf = (timeBuf.time * 1000) + (timeBuf.millitm);
#elif defined(__linux__) || defined(__APPLE__)
struct timeval tv;
gettimeofday(&tv, NULL);
buf = tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif
return buf;
}
char *GetDetailTimeString(u_int64 srcTime);
char *GetCatTimeString(u_int64 srcTime);
typedef struct _TimeInterval {
u_int64 time_s;
u_int64 time_e;
} TimeInterval;
static inline u_int8 TimeIntervalCompare(TimeInterval *resInterval, TimeInterval *desInterval) {
u_int8 rst = 0;
if (resInterval->time_e < desInterval->time_s) {
rst = 0;
} else {
//res.s > des.e no cross
if (resInterval->time_s > desInterval->time_e) {
rst = 0;
} else {
if (resInterval->time_s > desInterval->time_s) {
if (resInterval->time_e > desInterval->time_e) {
//res |________|
//des |__________|
rst = 1;
} else {
//res |__________|
//des |_________________|
rst = 2;
}
} else {
if (resInterval->time_e > desInterval->time_e) {
//res |____________|
//des |_______|
rst = 3;
} else {
//res |___________|
//des |_____________|
rst = 4;
}
}
}
}
return rst;
}
static inline unsigned long long catTrimToHour(unsigned long long timeMs) {
return timeMs / (3600 * 1000);
}
#endif // CAT_TIME_UTIL_H