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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* 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.
*/
#include "cat_time_util.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef WIN32
#define THREADLOCAL __declspec(thread)
#elif defined(__linux__) || defined(__APPLE__)
#define THREADLOCAL __thread
#else
#define THREADLOCAL
#endif
char *GetTimeString(u_int64 srcTime) {
/**
* Explanation of struct tm:
*
* struct tm {
* int tm_sec; // second - range [0, 60)
* int tm_min; // minute - range [0, 60)
* int tm_hour; // hour - range [0, 24)
* int tm_mday; // day of month - range [0, 31]
* int tm_mon; // month - range [0, 12)
* int tm_year; // year - range [1900, +∞)
* int tm_wday; // weekday - range [0, 7), 0 represents Sunday.
* int tm_yday; // day of year - range [0, 365], 0 represents January 1st.
* int tm_isdst; // is summer time (is daylight saving time).
* long int tm_gmtoff; // The difference in seconds from UTC - range [-12 * 3600, 14 * 3600]
* const char *tm_zone; // current time zone. (related to env variable TZ)
* };
*
*/
time_t t = 0;
if (srcTime == 0) {
t = time(0);
} else {
t = srcTime / 1000;
}
static THREADLOCAL char *tmp = NULL;
if (tmp == NULL) {
tmp = (char *) malloc(128);
}
#pragma warning( push )
#pragma warning( disable : 4996 )
strftime(tmp, 64, "%Y-%m-%d %H-%M-%S", localtime(&t));
#pragma warning( pop )
return tmp;
}
char *GetDetailTimeString(u_int64 srcTime) {
time_t t = 0;
#if defined(WIN32)
struct __timeb64 timeBuf;
if (srcTime == 0)
{
t = time(0);
_ftime64_s(&timeBuf);
}
else
{
t = srcTime / 1000;
timeBuf.millitm = srcTime % 1000;
}
#elif defined(__linux__) || defined(__APPLE__)
struct timeval tv;
if (srcTime == 0) {
gettimeofday(&tv, NULL);
} else {
t = srcTime / 1000;
tv.tv_usec = (srcTime % 1000) * 1000;
}
#endif
static THREADLOCAL char *tmp = NULL;
if (tmp == NULL) {
tmp = (char *) malloc(128);
}
#pragma warning( push )
#pragma warning( disable : 4996 )
strftime(tmp, 128, "%Y-%m-%d %H-%M-%S", localtime(&t));
#pragma warning( pop )
size_t timeBufLen = strlen(tmp);
#if defined(WIN32)
sprintf_s(tmp + timeBufLen, 128 - timeBufLen, "-%03d", timeBuf.millitm);
#else
snprintf(tmp + timeBufLen, sizeof(tmp) - timeBufLen, "-%03d", (int) (tv.tv_usec / 1000));
#endif
return tmp;
}
char *GetCatTimeString(u_int64 srcTime) {
time_t t = 0;
#if defined(WIN32)
struct __timeb64 timeBuf;
if (srcTime == 0)
{
t = time(0);
_ftime64_s(&timeBuf);
}
else
{
t = srcTime / 1000;
timeBuf.millitm = srcTime % 1000;
}
#elif defined(__linux__) || defined(__APPLE__)
struct timeval tv;
if (srcTime == 0) {
gettimeofday(&tv, NULL);
} else {
t = srcTime / 1000;
tv.tv_usec = (srcTime % 1000) * 1000;
}
#endif
static THREADLOCAL char *tmp = NULL;
if (tmp == NULL) {
tmp = (char *) malloc(128);
}
#pragma warning( push )
#pragma warning( disable : 4996 )
strftime(tmp, 128, "%Y-%m-%d %H:%M:%S", localtime(&t));
#pragma warning( pop )
size_t timeBufLen = strlen(tmp);
#if defined(WIN32)
sprintf_s(tmp + timeBufLen, 128 - timeBufLen, ".%03d", timeBuf.millitm);
#else
snprintf(tmp + timeBufLen, sizeof(tmp) - timeBufLen, ".%03d", (int) (tv.tv_usec / 1000));
#endif
return tmp;
}