解析日期字符串以从时间戳中删除小时、分钟和秒
Parse date string to remove hours, minutes and seconds from timestamp
假设我从Web服务器收到一个要解析的字符串。此字符串包含格式为 YYYY-MM-DD
的日期。我想要的是将其转换为代表当天开始的时间戳,因此我不想要秒、分钟和小时。
举个虚拟示例,我正在尝试提取当天的时间戳,一旦转换为YYYY-MM-DD
格式。代码如下:
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
int main()
{
// Current time at GMT
std::time_t now = std::time(0);
std::tm *now_tm = std::gmtime(&now);
std::ostringstream oss;
// Extract yyyy-mm-dd = %F
oss << std::put_time(now_tm, "%F");
// Use oss to get a date without seconds from
// current time at gmt
std::tm tm;
std::istringstream ss(oss.str());
ss >> std::get_time(&tm, "%F");
std::time_t current_date = std::mktime(&tm);
std::cout << oss.str() << std::endl;
std::cout << "cd: " << current_date << std::endl;
return 0;
}
输出为:
2017-10-19
cd: 1908337984324104
提取的时间戳显然是错误的。使用 %F
格式解析2017-10-19
字符串的问题在哪里?
通过使用 Howard Hinnant 的免费、开源、仅标题时间扩展库,您可以在不离开chrono
类型系统安全的情况下执行此操作。
#include "date/date.h"
#include <iostream>
int
main()
{
std::istringstream ss{"2017-10-19"};
date::sys_seconds tp;
ss >> date::parse("%F", tp);
std::cout << date::format("%Fn", tp);
using date::operator<<;
std::cout << "cd: " << tp.time_since_epoch() << 'n';
}
date::sys_seconds
是一个在 Unix Time 中计算chrono::seconds
std::chrono::time_point
。 您可以使用 %F
直接解析到其中。 您还可以使用相同的格式字符串 (%F
) 对其进行格式化,还可以检查chrono::seconds
的基础计数。 此程序输出:
2017-10-19
cd: 1508371200s
std::get_time 的文档没有列出转换说明符 %F。当检查流标志(你总是应该这样做!)时,它也会告诉转换失败,至少在我的编译器中是这样。
因此,通过将其替换为 %Y-%m-%d,转换成功。最后,您默认构造了 tm 变量而不将其归零(例如,通过值初始化)。修复此问题时,代码按预期工作:
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
int main()
{
// Current time at GMT
std::time_t now = std::time(0);
std::tm *now_tm = std::gmtime(&now);
std::ostringstream oss;
// Extract yyyy-mm-dd = %F
oss << std::put_time(now_tm, "%Y-%m-%d");
// Use oss to get a date without seconds from
// current time at gmt
std::tm tm{ }; // value-initialize!
std::istringstream ss(oss.str());
ss >> std::get_time(&tm, "%Y-%m-%d");
if(!ss) std::cout << "conversion errorn";
else {
std::time_t current_date = std::mktime(&tm);
std::cout << current_date << 'n';
std::cout << "cd: " << current_date << 'n';
}
return 0;
}
http://coliru.stacked-crooked.com/a/d86aa1e1d890a14d
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 删除指向指针的指针是运行时错误吗
- 将指针设置为"nullptr"并不能防止双重删除?
- 为什么示例代码访问IUnknown中已删除的内存
- 为什么在读取文件大小时文件IO速度会发生变化
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- bad_alloc::'标量删除析构函数'(无符号整数)当我尝试创建矢量 470MB 大小时
- 解析日期字符串以从时间戳中删除小时、分钟和秒