用boost检索当前世纪
Retrieve current century with boost
我可以通过调用
来检索当前年份:boost::posix_time::second_clock::local_time().date().year();
但是我如何用boost函数从那一年提取世纪数呢?
一个世纪在所有地区的定义都是100年。然而,问题是本世纪何时开始。(你是从0开始数还是从1开始数?)假设是格里高利历,或者是一个与教皇格雷格一致的关于何时开始计数的日历:
#include <iostream>
int yearToCentury(int year)
{
return (year + 99) / 100;
}
int main()
{
std::cout << 1999 << ": " << yearToCentury(1999) << std::endl;
std::cout << 2000 << ": " << yearToCentury(2000) << std::endl;
std::cout << 2001 << ": " << yearToCentury(2001) << std::endl;
return 0;
}
产生如下结果:
1999: 20
2000: 20
2001: 21
但是,我在标准库或boost中找不到任何函数为您完成此计算的证据。
除以100有什么不对?
首先使用boost获取当前日期时间:
boost::posix_time::ptime timeLocal = boost::posix_time::second_clock::local_time();
auto century = GetCentury(timeLocal.date().year());
获取当前世纪的方法:
/// <summary>Gets century from year.</summary>
/// <param name="year">The year.</param>
/// <returns>The century as int value.</returns>
int GetCentury(int year)
{
if (year % 100 == 0)
return year / 100;
else
return year / 100 + 1;
}
相关文章:
- 如何从boost :: multi_index_container检索元素
- 如何在Boost Serialization中检索多个对象值
- 使用Boost序列化保存和检索多个对象
- boost asio async_receive_from()在连续发送帧时缺少udp帧数据检索
- boost::p osix_time:使用夏令时检索时间
- boost属性树解析json后无法检索记录
- decltype 和 boost::variant - 检索当前值
- ArgumentError 在 Boost.Python 中的类中存储和检索指针:错误的类型
- 使用 Boost.Locale 库检索代码点
- 如何发送http请求并检索json响应C++Boost
- 在C++/boost中,用*nix检索时间的最高效的方法是什么
- 从boost::optional中检索对象
- 使用lambda从boost::线程检索返回值
- 通过连续两次调用boost::asio::read来检索正确的数据
- 使用boost::bind与boost::function:检索绑定变量类型
- 用boost检索当前世纪
- 使用c++ Boost或STL和Mysql存储和检索图像
- Boost协程如何在coro_t完成后检索函数返回值
- 是否可以将数据保存在boost线程中并检索它?
- Boost::asio::streambuf通过https检索XML数据