-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathgnb_time.c
128 lines (73 loc) · 2.06 KB
/
gnb_time.c
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
/*
Copyright (C) gnbdev
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#define _POSIX_C_SOURCE 1
//使得 localtime_r 等函数有效
#endif
#include <time.h>
#include <sys/time.h>
#include "gnb_time.h"
uint64_t gnb_timestamp_sec(){
int ret;
struct timeval cur_time;
uint64_t u64;
ret = gettimeofday(&cur_time,NULL);
if (0!=ret) {
return 0;
}
u64 = (uint64_t)cur_time.tv_sec;
return u64;
}
uint64_t gnb_timestamp_usec(){
int ret;
struct timeval cur_time;
uint64_t u64;
ret = gettimeofday(&cur_time,NULL);
if (0!=ret) {
return 0;
}
u64 = (uint64_t)cur_time.tv_sec*1000000 + cur_time.tv_usec;
return u64;
}
void gnb_now_timef(const char *format, char *buffer, size_t buffer_size){
time_t t;
struct tm ltm;
time (&t);
localtime_r(&t, <m);
strftime (buffer,buffer_size,format,<m);
}
void gnb_timef(const char *format, time_t t, char *buffer, size_t buffer_size){
struct tm ltm;
localtime_r(&t, <m);
strftime (buffer,buffer_size,format,<m);
}
int gnb_now_mday(){
time_t t;
struct tm ltm;
time (&t);
localtime_r(&t, <m);
return ltm.tm_mday;
}
int gnb_now_yday(){
time_t t;
struct tm ltm;
time (&t);
localtime_r(&t, <m);
return ltm.tm_yday;
}