我想從 C 中的日期時間格式獲取時間戳。我寫了 C 風格的解決方案,但最后this->cache_time沒有承諾\0,因為它是std::string_view.
std::time_t client::get_cache_time() {
struct tm time;
if(strptime(this->cache_time.data(), "%a, %d %b %Y %X %Z", &time) != NULL) {
return timelocal(&time);
}
return 0;
}
c 中是否有 strptime() 替代方案可以使用std::string_view?
謝謝你的幫助。
uj5u.com熱心網友回復:
我不知道這個解決方案是否干凈且記憶體高效,但它確實有效。
std::time_t client::get_cache_time() {
std::tm time;
std::istringstream buffer(std::string(this->cache_time));
buffer >> std::get_time(&time, "%a, %d %b %Y %X %Z");
if(!buffer.fail()) {
return timelocal(&time);
}
return 0;
}
std::string_view很好,但并非所有地方都支持。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/447520.html
上一篇:應用腳本步進月份和日期格式
