-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path时间戳问题
185 lines (159 loc) · 4.43 KB
/
时间戳问题
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
将时间戳转换为string类型;
1 #include <stdio.h>
2 #include<iostream>
3 #include <sstream>
4 #include <time.h>
5 int main ()
6 {
7 time_t t;
8 struct tm * lt;
9 time (&t);//获取Unix时间戳。
10
11 std::cout << t << std::endl;
12
13
14 std::stringstream ss;
15 ss << t ;
16 std::string mytest = "abc"+ss.str();
17
18 std::cout << mytest << std::endl;
19 // lt = localtime (&t);//转为时间结构。
20 // printf ( "%d/%d/%d %d:%d:%d\n",lt->tm_year+1900,\
21 // lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//输出结果
22
23
24 return 0;
25 }
c语言将时间戳转换为字符串:struct timeval的两个属性分别是秒级和微秒级,都是long类型
头文件:
#include<sys/time.h>
char buf_temp[256];
struct timeval time;
gettimeoftoday(&time, NULL);
memset(buf_temp, sizeof(buf_temp),0x00);
sprintf(buf_temp, "%d", time.tv_sec);
// sprintf(buf_temp, "%d", time.tv_sec, tv.tv_usec / 1000);
该函数包含在stdio.h的头文件:
#include<stdio.h>
sprintd与printf函数的区别:二者功能相似,但是sprintf函数打印到字符串中,而printf函数输出在屏幕上.sprintf函数在完成其他数据转换成字符串类型的操作中应用广泛.
char str[20];
double f=14.309948;
sprintf(str,"%6.2f",f);
char str[20];
int a=20984,b=48090;
sprintf(str,"=m",a,b);
str[]="20984 48090";
测试完成的程序:
#include<stdio.h>
#include<sys/time.h>
#include<iostream>
#include<cstring>
#include<time.h>
using namespace std;
int main()
{
char buf_temp[256];
struct timeval time;
gettimeofday(&time, NULL);
memset(buf_temp, sizeof(buf_temp),0x00);
sprintf(buf_temp, "%ld%ld",time.tv_sec, time.tv_usec/1000);
printf("this is a :%s\n",buf_temp);
cout << buf_temp << endl;
return 0;
}
printf中输出的类型:
%c:字符
%f:浮点数(包括flaot和double)
%p:指针
%s:字符串
%i:有符号十进制数
%u:无符号十进制数
使用stringstream,输出时间戳,进行封装,测试过:s(10位);ms(13位);us(16位)
#include<stdio.h>
#include<sys/time.h>
#include<iostream>
#include<cstring>
#include<time.h>
#include <sstream>
using namespace std;
std::string timestampFuction()
{
struct timeval time;
gettimeofday(&time, NULL);
std::stringstream sss;
sss << time.tv_sec;
sss << time.tv_usec/1000;
std::string a = sss.str();
return a;
}
int main()
{
printf("++++++++++++++++++++");
std::string ss = timestampFuction();
printf("%s\n",ss.c_str());
return 0;
}
整理好的时间戳:
static std::string timestampFuction()
{
struct timeval time;
gettimeofday(&time, NULL);
std::stringstream systemTime;
systemTime << time.tv_sec;
systemTime << time.tv_usec/1000;
std::string timestamp = systemTime.str();
return timestamp;
}
C++对字符串清零.
std::string ackContent;
ackContent.clear();
ros中的时间戳函数:
string timestamp = std::to_string(ros::Time::now().toNSec()/1000000);
ackContent.clear();
数组转换为string
解1:首地址复制
string a;
char stringarray[100];
stringarray="changestring";
a=stringarray;
解2:元素按位赋值
string a;
char stringarray[100];
stringarray="changestring";
int i=0;
while(stringarray[i]!='\0'){
a[i]=stringarray[i];
i++;
}
a[i]='\0';
解3:根据string的连接特性,粘合性
string a;
char stringarray[100];
stringarray="changestring";
a="";
int i=0;
while(stringarray[i]!='\0'){
a+=stringarray[i];//或用a,append(stringarray[i]);
i++;
}
a[i]='\0';
解决时间戳输出12位问题
using namespace std;
static char buf_temp[256];
std::string timestampFuction()
{
char buf_temp[256];
struct timeval time;
unsigned long ul_ms;
gettimeofday(&time, NULL);
//std::stringstream sss;
//sss << time.tv_sec;
//sss << time.tv_usec/1000;
memset(buf_temp, sizeof(buf_temp),0x00);
ul_ms = time.tv_sec * 1000 + time.tv_usec/1000;
sprintf(buf_temp, "%ld",ul_ms);
// sprintf(buf_temp, "%ld%ld",time.tv_sec, time.tv_usec/1000);
// std::string a = sss.str();
std::string a = buf_temp;
return a;
}