forked from gnbdev/opengnb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnb_time.c
106 lines (61 loc) · 1.4 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
#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;
}