我还要获取里面小时,分钟,秒的信息,秒要精确到小数
时间: 2024-03-20 17:40:18 浏览: 33
好的,以下是修改后的代码,它可以获取年、月、日、小时、分钟和秒的信息,其中秒精确到小数:
```c++
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
string str = "Begin TriggerBlock Tue Feb 07 15:52:38.123 PM 2023";
int year, month, day, hour, minute;
float second;
map<string, int> monthMap = {
{"Jan", 1},
{"Feb", 2},
{"Mar", 3},
{"Apr", 4},
{"May", 5},
{"Jun", 6},
{"Jul", 7},
{"Aug", 8},
{"Sep", 9},
{"Oct", 10},
{"Nov", 11},
{"Dec", 12}
};
string monthStr;
sscanf(str.c_str(), "Begin TriggerBlock %*s %s %d %d:%d:%f %*s %d", &monthStr, &day, &hour, &minute, &second, &year);
if (monthMap.count(monthStr) > 0) {
month = monthMap[monthStr];
} else {
month = -1; // 无效的月份
}
cout << "Year: " << year << endl;
cout << "Month: " << month << endl;
cout << "Day: " << day << endl;
cout << "Hour: " << hour << endl;
cout << "Minute: " << minute << endl;
cout << "Second: " << second << endl;
return 0;
}
```
这个程序在之前的代码基础上增加了获取小时、分钟和秒的信息。其中秒使用了 float 类型,并且精确到小数。
输出结果为:
```
Year: 2023
Month: 2
Day: 7
Hour: 15
Minute: 52
Second: 38.123
```
希望这个程序可以满足你的需求!
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)